常用php脚本向接口发送参数数据
//签名函数
function getSignString($dataArr, $token) {
$sign = '';
ksort($dataArr);
foreach ($dataArr as $k => $v) {
//特殊字符处理
$sign .= $k . '=' . $v;
}
$sign = md5($sign . $token);
return sign;
}
//发送数据方法
function curlPost($url, $data = array(), $header = array(),
$cookie = '', $timeout = 1, &$curlInfo = array()) {
$post_string = is_array($data) ? http_build_query($data) : $data;
$https = stripos($url, 'https://') === 0 ? true : false;
$curl = curl_init();
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_string);
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, $timeout);
curl_setopt($curl, CURLINFO_HEADER_OUT, true);
curl_setopt($curl, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
curl_setopt($curl, CURLOPT_HTTP_HEADER, $header);
if ($https) {
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, false);
}
//设置连接结束后,保存cookie文件
curl_setopt($curl, CURLOPT_COOKIE, $cookie);
$result = curl_exec($curl);
curl_close($curl);
return $result;
}
调用
$dataArr = array(
'params_a' => a,
'params_b' => b,
'params_name' => name
);
//获取签名文件,带在数组中
$signString = ''; //一组随机数
$dataArr['sign'] = getSignString($dataArr, $signString);
$return_info = curlPost('http://192.168.85.130/test/insertInfo', $dataArr);
//可以查看服务器端如果有var_dump错误提示,可以查看错误提示。
var_dump($return_info);