CURL解析超時的解決方案

背景:項目中需要在抓取紛享銷客CRM圖片上傳到OSS,調(diào)用OssClient.php時,容易發(fā)生解析超時(多重試幾次就ok)。

專注于為中小企業(yè)提供成都網(wǎng)站建設(shè)、成都網(wǎng)站制作服務(wù),電腦端+手機(jī)端+微信端的三站合一,更高效的管理,為中小企業(yè)羅湖免費做網(wǎng)站提供優(yōu)質(zhì)的服務(wù)。我們立足成都,凝聚了一批互聯(lián)網(wǎng)行業(yè)人才,有力地推動了千余家企業(yè)的穩(wěn)健成長,幫助中小企業(yè)通過網(wǎng)站建設(shè)實現(xiàn)規(guī)模擴(kuò)充和轉(zhuǎn)變。

錯誤提示:

[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客戶端后,調(diào)用uploadFile方法。
2.uploadFile調(diào)用了auth,驗證并且執(zhí)行請求,按照OSS Api協(xié)議,執(zhí)行操作。

    /**
     * 上傳本地文件
     *
     * @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中調(diào)用RequestCore類創(chuàng)建請求

    /**
     * 驗證并且執(zhí)行請求,按照OSS Api協(xié)議,執(zhí)行操作
     *
     * @param array $options
     * @return ResponseCore
     * @throws OssException
     * @throws RequestCore_Exception
     */
    private function auth($options)
    {
            ......
        //創(chuàng)建請求
        $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發(fā)送請求(調(diào)用了prep_request準(zhǔn)備請求方法)

 /**
     * 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.最終可以推導(dǎo)出問題出在RequestCore中的CURL。

解決方案:
curl有一個CURLOPT_IPRESOLVE選項,作用是指定使用那種IP協(xié)議(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.

這里要注意:默認(rèn)值(curl沒有主動設(shè)置CURLOPT_IPRESOLVE選項時,則系統(tǒng)會使用默認(rèn)值)。

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

含義為:默認(rèn)使用服務(wù)器系統(tǒng)允許的所有IP版本來解析地址。
而服務(wù)器系統(tǒng)可能存在同時支持IPV4/IPV6,CURL會挨個去試。當(dāng)本地網(wǎng)絡(luò)不支持其中任何一種IP版本協(xié)議時,會出現(xiàn)超時。

所以,最終的解決方案為顯式指定CURL的CURLOPT_IPRESOLVE選項。

curl_setopt($ch, CURLOPT_IPRESOLVE, CURL_IPRESOLVE_V4);

參考官方CURLOPT_IPRESOLVE

網(wǎng)站欄目:CURL解析超時的解決方案
轉(zhuǎn)載來于:http://bm7419.com/article0/gejgio.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供移動網(wǎng)站建設(shè)面包屑導(dǎo)航、電子商務(wù)、Google、全網(wǎng)營銷推廣關(guān)鍵詞優(yōu)化

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

成都seo排名網(wǎng)站優(yōu)化