douqian8238 2015-05-02 15:54
浏览 81
已采纳

如何捕获此PHP连接错误?

Here's the Problem:

On some websites, my code that connects to Twitter is generating a fatal error that stops PHP from running for the rest of the page load. It creates a total page kill. How can I catch this error so that I can simply display zeroes or use one of the tweet counts from the most previous successful connections?

Presumably, this is because a site that has this code on it has hit the Twitter API limits. But in the rare cases where that does happen, I need it to fail gracefully so that I can just use my previously fetched tweet count.

Here's the code:

If the user has the Twitter Button active, we fetch the share count:

$social = new shareCount($url);
if( $options['twitter'] ) $tweets = $social->get_tweets();

This is where that function request gets passed inside the shareCount class:

function get_tweets() { 
    $json_string = file_get_contents_curl('https://urls.api.twitter.com/1/urls/count.json?url=' . $this->url);
    $json = json_decode($json_string, true);
    return isset($json['count'])?intval($json['count']):0;
}

And this is the file_get_contents_curl() function which is, I believe where the error is occurring and is where we need to catch the error.

function file_get_contents_curl($url){
    $ch=curl_init();
    curl_setopt($ch, CURLOPT_URL, $url);
    curl_setopt($ch, CURLOPT_USERAGENT, $_SERVER['HTTP_USER_AGENT']);
    curl_setopt($ch, CURLOPT_FAILONERROR, 0);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER,1);
    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
    curl_setopt($ch, CURLOPT_TIMEOUT, 5);
    curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);
    $cont = curl_exec($ch);
    if(curl_error($ch))
    {
        die(curl_error($ch));
    }
    return $cont;
}

This is the error that is being generated:

connect() timed out!

This is the requested solution:

So the question is, how do I catch that error and have the file_get_contents_curl() function either return a zero or return a specific code that I can look for indicating quietly to my script that it failed to connect?

  • 写回答

2条回答 默认 最新

  • dongshuan8722 2015-05-02 15:57
    关注

    There are two timeout settings for cURL:

    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);        // Timeout in seconds
    curl_setopt($ch, CURLOPT_TIMEOUT, 4);               // Timeout in seconds
    

    Set them both and cURL will fail gracefully on a timeout of either type.

    However, slow DNS resolutions will trigger SIGALRM which cURL interprets as the timeout alarm. Add this to quiet that alarm, and any alarms that will halt the PHP execution:

    curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
    

    You should continue to be checking for cURL errors and dealing with them, not suppressing them, like this:

    $curl_errno = curl_errno($ch);
    if ($curl_errno > 0) {
        // Deal with error
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 有兄弟姐妹会用word插图功能制作类似citespace的图片吗?
  • ¥200 uniapp长期运行卡死问题解决
  • ¥15 请教:如何用postman调用本地虚拟机区块链接上的合约?
  • ¥15 为什么使用javacv转封装rtsp为rtmp时出现如下问题:[h264 @ 000000004faf7500]no frame?
  • ¥15 乘性高斯噪声在深度学习网络中的应用
  • ¥15 关于docker部署flink集成hadoop的yarn,请教个问题 flink启动yarn-session.sh连不上hadoop,这个整了好几天一直不行,求帮忙看一下怎么解决
  • ¥15 深度学习根据CNN网络模型,搭建BP模型并训练MNIST数据集
  • ¥15 C++ 头文件/宏冲突问题解决
  • ¥15 用comsol模拟大气湍流通过底部加热(温度不同)的腔体
  • ¥50 安卓adb backup备份子用户应用数据失败