CURL解析超时的解决方案
admin
2023-01-28 03:53:37
0

背景:项目中需要在抓取纷享销客CRM图片上传到OSS,调用OssClient.php时,容易发生解析超时(多重试几次就ok)。

错误提示:

[2019-04-08 19:41:01] lumen.DEBUG: 出错文件:/home/zrj/www/admin/yundou-admin/vendor/aliyuncs/oss-sdk-php/src/OSS/OssClient.php  
[2019-04-08 19:41:01] lumen.DEBUG: 出错编码:0  
[2019-04-08 19:41:01] lumen.DEBUG: 出错行号:2187  
[2019-04-08 19:41:01] lumen.DEBUG: 出错信息:RequestCoreException: cURL resource: Resource id #371; cURL error: Resolving timed out after 10521 milliseconds (28) 

Resolving timed out after 10521 milliseconds (28)
解析超时

源码分析:

try {
            $ossClient = new OssClient(self::$accessKeyId, self::$accessKeySecret, self::$endpoint);
            $ossClient->uploadFile(self::$bucket, $ossFileName, $localhostFileName);//上传文件
            $ossClient->putBucketAcl(self::$bucket, OssClient::OSS_ACL_TYPE_PUBLIC_READ);
        } catch (OssException $e) {
            self::debugException($e);
            throw new \Exception("上传oss失败:" . $e->getMessage(), $e->getCode());
        }

1.实例化OssClient客户端后,调用uploadFile方法。
2.uploadFile调用了auth,验证并且执行请求,按照OSS Api协议,执行操作。

    /**
     * 上传本地文件
     *
     * @param string $bucket bucket名称
     * @param string $object object名称
     * @param string $file 本地文件路径
     * @param array $options
     * @return null
     * @throws OssException
     */
    public function uploadFile($bucket, $object, $file, $options = NULL)
    {
        ......
        $response = $this->auth($options);
        $result = new PutSetDeleteResult($response);
        return $result->getData();
    }

3.auth中调用RequestCore类创建请求

    /**
     * 验证并且执行请求,按照OSS Api协议,执行操作
     *
     * @param array $options
     * @return ResponseCore
     * @throws OssException
     * @throws RequestCore_Exception
     */
    private function auth($options)
    {
            ......
        //创建请求
        $request = new RequestCore($this->requestUrl, $this->requestProxy);
        $request->set_useragent($this->generateUserAgent());
                ......
                try {
            $request->send_request();
        } catch (RequestCore_Exception $e) {
            throw(new OssException('RequestCoreException: ' . $e->getMessage()));
        }
    }

4.OSS\Http\RequestCore类中send_request方法通过CURL发送请求(调用了prep_request准备请求方法)

 /**
     * Sends the request, calling necessary utility functions to update built-in properties.
     *
     * @param boolean $parse (Optional) Whether to parse the response with ResponseCore or not.
     * @return string The resulting unparsed data from the request.
     */
    public function send_request($parse = false)
    {
        set_time_limit(0);

        $curl_handle = $this->prep_request();
        $this->response = curl_exec($curl_handle);

        if ($this->response === false) {
            throw new RequestCore_Exception('cURL resource: ' . (string)$curl_handle . '; cURL error: ' . curl_error($curl_handle) . ' (' . curl_errno($curl_handle) . ')');
        }

        $parsed_response = $this->process_response($curl_handle, $this->response);

        curl_close($curl_handle);

        if ($parse) {
            return $parsed_response;
        }

        return $this->response;
    }

5.最终可以推导出问题出在RequestCore中的CURL。

解决方案:
curl有一个CURLOPT_IPRESOLVE选项,作用是指定使用那种IP协议(IPV4/IPV6)。

CURLOPT_IPRESOLVE - specify which IP protocol version to use

选项:
CURL_IPRESOLVE_WHATEVER

Default, resolves addresses to all IP versions that your system allows.

CURL_IPRESOLVE_V4

Resolve to IPv4 addresses.

CURL_IPRESOLVE_V6

Resolve to IPv6 addresses.

这里要注意:默认值(curl没有主动设置CURLOPT_IPRESOLVE选项时,则系统会使用默认值)。

Default, resolves addresses to all IP versions that your system allows.

含义为:默认使用服务器系统允许的所有IP版本来解析地址。
而服务器系统可能存在同时支持IPV4/IPV6,CURL会挨个去试。当本地网络不支持其中任何一种IP版本协议时,会出现超时。

所以,最终的解决方案为显式指定CURL的CURLOPT_IPRESOLVE选项。

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

参考官方CURLOPT_IPRESOLVE

相关内容

热门资讯

“外省结婚交1500元”的奇葩... 澎湃特约评论员 方圆红星新闻 图近日,云南临沧一村庄贴出的告示,被网友吐槽“太奇葩”。告示内容称:“...
【今日要闻】“乐乐四川麻将.到... 有 亲,根据资深记者爆料乐乐四川麻将是可以开挂的,确实有挂(咨询软件无需...
终于了解“新蜜瓜拼十.究竟有挂... 网上科普关于“新蜜瓜拼十有没有挂”话题很是火热,小编也是针对新蜜瓜拼十作*弊开挂的方法以及开挂对应的...
云南:将二孩、三孩育儿补助统一... 【大河财立方消息】12月17日,云南省政府印发《关于加快完善生育支持政策体系推动建设生育友好型社会的...
机构:脑机接口行业加速创新 来源:市场资讯 (来源:证券时报) 今天,中国科学院脑科学与智能技术卓越创新中心发布该中心与国内科研...
【第一财经】“新道游牛牛.可以... 有 亲,根据资深记者爆料新道游牛牛是可以开挂的,确实有挂(咨询软件无需打...
广东亚健康检测仪十大品牌排行:... 现代生活节奏加快,亚健康状态已成为都市人群的隐形困扰。在广东这片经济活跃的热土上,健康检测设备市场呈...
【第一资讯】“牛魔王牛牛.可以... 您好:牛魔王牛牛这款游戏可以开挂,确实是有挂的,需要了解加客服微信【4282891】很多玩家在这款游...
今日重大通报“天府麻舍.辅助器... 网上科普关于“天府麻舍有没有挂”话题很是火热,小编也是针对天府麻舍作*弊开挂的方法以及开挂对应的知识...
玩家攻略科普“花城牌舍.开挂器... 您好:花城牌舍这款游戏可以开挂,确实是有挂的,需要了解加客服微信【9752949】很多玩家在这款游戏...