如何捕获PHP中的curl错误
问题描述:
我使用PHP的 curl
函数将数据从本地计算机发布到Web服务器。我的代码如下:
I am using PHP's curl
functions to post data to the web server from my local machine. My code is as follows:
$c = curl_init();
curl_setopt($c, CURLOPT_URL, $url);
curl_setopt($c, CURLOPT_RETURNTRANSFER,true);
curl_setopt($c, CURLOPT_POST, true);
curl_setopt($c, CURLOPT_POSTFIELDS, $data);
$result=curl_exec ($c);
if(curl_exec($c) === false) {
echo "ok";
}
else {
echo "error";
}
curl_close ($c);
很遗憾,我无法捕获404,500或网络故障等错误。
Unfortunately I am not able to catch any errors like 404, 500 or network failure. So how will I get to know that data was not posted to or retrieved from the remote?
答
您可以使用 curl_error()
函数知道是否有错误,例如:
You can use the curl_error()
function to know if there was some error, example:
if(curl_error($c))
{
echo 'error:' . curl_error($c);
}