如何捕获PHP中的curl错误 [英] How to catch curl errors in PHP

查看:845
本文介绍了如何捕获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);
}

查看curl错误代码的说明

这篇关于如何捕获PHP中的curl错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆