问题描述:
php文件中,通过curl,header头,body参数,验证通过后,接收对方的数据。传参有两种方式。
解决办法:
function post_http($array='',$url){
$ch = curl_init();
$header = array('Content-Type: application/json; charset=utf-8','Accept: application/json','secretKey:xxxxxxxx','signKey:xxxxxxxxxxx12');
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,'POST');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
//https //curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 2);
//curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 2);
// post数据
curl_setopt($ch, CURLOPT_POST, 1);
// post设置头
// curl_setopt($ch, CURLOPT_PROXY, '127.0.0.1:8080');//设置代理服务器
//启用时会将头文件的信息作为数据流输出
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
// post的变量
$arr = json_encode($array);
curl_setopt($ch, CURLOPT_POSTFIELDS, $arr);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
curl_close($ch);
//打印获得的数据
return json_decode($result,true);
}
function post_http($url){
$body = "key=val&key1=val2";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST,true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $body);
curl_setopt($ch, CURLOPT_TIMEOUT, 30);
$result = curl_exec($ch);
if (curl_errno($ch)) {
return curl_error($ch);
}
if (!curl_errno($curl)) {
echo json_encode(['code'=>1001,'msg'=>'网络或者URL出错!','data'=>curl_errno($curl)]); exit();
}
//关闭cURL资源,并且释放系统资源
curl_close($ch);
//打印获得的数据
return json_decode($result,true);
}