当前位置:网站首页>PHP curl post x-www-form-urlencoded

PHP curl post x-www-form-urlencoded

2022-07-25 11:56:00 hexiaoniao

Interface in postman The test inside was successful . however PHP curl post This The interface failed

I figured it out later . Reason for request header . The request header of this interface is x-www-form-urlencoded. therefore curl post It should also be changed to this

if($url == "" || $timeout <= 0){
    return false;
}
$post_data = http_build_query($post_data);// a key 
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HEADER, 0);// Don't grab header information . Just return data 
curl_setopt($curl, CURLOPT_TIMEOUT, (int)$timeout);// timeout 
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);//1 Means not to return bool value 
curl_setopt($curl, CURLOPT_POST, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Content-Type: application/x-www-form-urlencoded'));// a key 
curl_setopt($curl, CURLOPT_POSTFIELDS, $post_data);
$data = curl_exec($curl);
if (curl_errno($curl)) {
    return curl_error($curl);
}
curl_close($curl);
原网站

版权声明
本文为[hexiaoniao]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/206/202207251102042018.html