php curl请求超时,设置毫秒级解决

问题背景

 由于百度接口的超时设置很大,导致队列经常堵塞,因此将超时时间缩短为毫秒级别,代码如下:

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 300);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 500);

问题现象

所有调用此代码的接口都响应超时,并且响应时间极短

问题解决经过

在网上查资料说要设置这个毫秒级别的超时才会生效:

curl_setopt ( $ch, CURLOPT_NOSIGNAL, true); 

于是本地测试,发现毫无影响:

然后直接在test01上把超时时间加大为一秒以上,发现响应成功
然后改回来,加上那行代码,响应成功

问题结论

如果要支持毫秒级别的超时设置必须加
curl_setopt ( $ch, CURLOPT_NOSIGNAL, true);
如果只是

curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, 300);
curl_setopt($ch, CURLOPT_TIMEOUT_MS, 500);

将直接返回超时【但是, 我们却发现, 在我们的CentOS服务器上, 

当你设置了小于1000ms的超时以后, curl不会发起任何请求,
而直接返回超时错误(Timeout reached 28)】,这是PHP的坑,资深者直接阅读这个 http://www.laruence.com/2014/01/21/2939.html
timeout支持毫秒数在cURL 7.16.2中被加入,从PHP 5.2.3起可使用。

项目代码

 /**
     * 服务器通过get请求获得内容
     * @author duke
     * @param string $url 请求的url,拼接后的
     * @return string           请求返回的内容
     */


    private static function get_contents($url, $param)
    {
        $baidu_key = self::getOneKey();
        $param['ak'] = $baidu_key;
        $param['output'] = 'json';
        $url = $url . '?' . http_build_query($param, '', '&');
//echo $url;
        $retry = 2;
        $result = array();

        while ($retry > 0) {
            $result = json_decode(self::curl_get($url), true);

            if (!empty($result) && isset($result['status']) && $result['status'] == 0) {
                return $result;
            }
            if (!empty($result) && isset($result['status'])) {
                $status = $result['status'];
            } else {
                $status = ' http_error:';
            }
            EdjLog::info("baidu_response retry status is" . $status.'params'.json_encode($param));
            $retry--;
        }
        EdjLog::warning('request to baidu lbs api failed after retries');

        return $result;
    }
private static function curl_get($url, $milliseconds = 300)
    {
        $start_time = microtime(true);

        $ch = curl_init();
        //这个参数很重要,设置这个才可以支持毫秒级的超时设置
        curl_setopt($ch, CURLOPT_NOSIGNAL, 1);
        curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($ch, CURLOPT_USERAGENT, self::$useragent);
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($ch, CURLOPT_FAILONERROR, 0);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT_MS, self::$connecttimeout);
        curl_setopt($ch, CURLOPT_TIMEOUT_MS, $milliseconds);


        curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
        curl_setopt($ch, CURLOPT_URL, $url);
        $response = curl_exec($ch);
        $http_error_code = curl_errno($ch);
        curl_close($ch);

        EdjLog::info("request to baidu lbs api, url is $url, cost time:" . (microtime(true) - $start_time));
        EdjLog::info("baidu http_error:$http_error_code response is " . str_replace(PHP_EOL, '', $response));
        return $response;
    }