背景:项目中需要在抓取纷享销客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
总结以上是内存溢出为你收集整理的CURL解析超时的解决方案全部内容,希望文章能够帮你解决CURL解析超时的解决方案所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)