关于php curl 求助
php 用crul访问一个接口,一直提示失败,用fidder抓包发现CURLOPT_POSTFIELDS 发送的数据不全,最后几位丢失了。在网页用echo打印是完整的。post数据是个数组,使用json_encode转成json发送的。https://attach.52pojie.cn//forum/202004/19/223746t88o1iitdf4olit3.png?lhttps://attach.52pojie.cn//forum/202004/19/223748msniun3ui63sdd13.png?l最后的id看不见 public function postCurl($url,$data,$header,$method="POST"){
// $data = json_encode($data);
$ch = curl_init(); //初始化CURL句柄
curl_setopt($ch, CURLOPT_URL, $url); //设置请求的URL
curl_setopt($ch,CURLOPT_HEADER,1);
curl_setopt($ch, CURLOPT_HTTPHEADER, array("header: {'Content-Type': 'application/x-www-form-urlencoded'}"));
// curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); //设为TRUE把curl_exec()结果转化为字串,而不是直接输出
curl_setopt($ch, CURLOPT_CUSTOMREQUEST,"POST"); //设置请求方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);//设置提交的字符串
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);// 从证书中检查SSL加密算法是否存在
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);// 对认证证书来源的检查
$output = curl_exec($ch);
$status = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return $status;
}
function SendDataByCurl($url,$data=array()){
//对空格进行转义
$url = str_replace(' ','+',$url);
$ch = curl_init();
//设置选项,包括URL
curl_setopt($ch, CURLOPT_URL, "$url");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch,CURLOPT_TIMEOUT,3);//定义超时3秒钟
// POST数据
curl_setopt($ch, CURLOPT_POST, 1);
// 把post的变量加上
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data)); //所需传的数组用http_bulid_query()函数处理一下
//执行并获取url地址的内容
$output = curl_exec($ch);
$errorCode = curl_errno($ch);
//释放curl句柄
curl_close($ch);
if(0 !== $errorCode) {
return false;
}
return $output;
}
页:
[1]