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

相关内容

热门资讯

“镐山”之后美再威胁袭伊基础设... 截至7月22日,美军已连续11晚对伊朗发动袭击,伊方对美中东地区军事基地的反击也未停止。与此同时,美...
美国“痛失”杨植麟,这是历史性... 杨植麟的月之暗面创造了Kimi K3,轰动整个世界。他是近年来第一个放弃了美国机会、回中国创业,并且...
铭显光电取得LED显示屏加工夹... 国家知识产权局信息显示,上海铭显光电科技有限公司取得一项名为“一种LED显示屏加工夹具”的专利,授权...
大盘承压依旧登顶,vivo拿下... 7月22日消息,市场调研机构Omdia发布2026年第二季度印度智能市场报告,数据显示本季度印度手机...
白宫发布《科学:新的黄金时代》... 据白宫7月21日消息,白宫科技政策办公室、管理与预算办公室联合发布《科学:新的黄金时代》报告。本次备...
民营商业火箭首次从长三角东海海... 北京时间7月22日10时54分,我国太原卫星发射中心在上海东部海域使用引力一号遥四运载火箭,将搭载的...
美威胁袭伊基础设施,伊朗军方:... △当地时间21日,船只在霍尔木兹海峡航行伊朗武装部队哈塔姆安比亚中央总部当地时间22日发布声明称,美...
扑翼机器人问世:像潜水鸟一样在... 潜鸟、海鸥、海鹦和海燕,是自然界中约100种既能飞行又能游泳的鸟类。这些潜水鸟能够俯冲入水追逐猎物,...
当科学家走进西部脑谷:这里有望... 7月22日,2026天府科学家创新发展大会暨成都科产融合对接活动锦江经开区脑机接口专场在锦江区成功举...
女子身份证丢失后名下出现“吸毒... 四川籍的庞女士定居浙江宁波多年,她怎么也没想到,因丢失身份证一事,竟让她陷入长达7年“自证清白”的困...