{"id":538,"title":"curl\u8bbe\u7f6e\u8d85\u65f6\u4e0d\u8d77\u4f5c\u7528(CURLOPT_TIMEOUT\u548cCURLOPT_CONNECTTIMEOUT\u7684\u533a\u522b)","good":0,"bad":0,"hit":7474,"created_at":"2021-08-04 22:49:32","content":"
\u6700\u8fd1\u5ba2\u6237\u53d1\u9988\u4e00\u4e2abug,\u62c9\u53d6\u6570\u636e\u4e0e\u7ed9\u7684\u4e0d\u4e00\u81f4\uff0c\u5c11\u4e86\u4e00\u4e9b\uff0c\u636e\u8bf4\u5b58\u5728\u4e86\u4e00\u5e74\u591a\u4e86\u4e00\u76f4\u6ca1\u89e3\u51b3\uff1b<\/p>
\u67e5\u65e5\u5fd7\u53d1\u73b0\u6709\u4e9bcurl\u8bf7\u6c42\u8d85\u8fc73\u79d2\u5c31\u8d85\u65f6\u4e86\uff0c\u4ee3\u7801\u4e2d\u8bbe\u7f6e\u4e86CURLOPT_TIMEOUT\u4e3a30\uff08\u79d2\uff09\uff0c\u4f46\u5b9e\u9645\u4e0a\u4e0d\u8d77\u4f5c\u7528\uff0c\u53ea\u8981\u8d85\u8fc73\u79d2\u8fd8\u662f\u4f1a\u8d85\u65f6\uff0c\u6392\u67e5\u53d1\u73b0\u662f\u6240\u7528\u7684\u5305\u6709\u4e2a\u9ed8\u8ba4\u914d\u7f6e\uff1aCURLOPT_CONNECTTIMEOUT\u4e3a3\uff0c\u800c\u76ee\u524d\u7684\u4ee3\u7801\u4e2d\u53ea\u8bbe\u7f6e\u4e86CURLOPT_TIMEOUT\u4e3a30<\/p>
private $curlOPt = [\n CURLOPT_CONNECTTIMEOUT=>3,\n CURLOPT_TIMEOUT=>10,\n CURLOPT_AUTOREFERER=>true,\n CURLOPT_USERAGENT=>"Mozilla\/4.0 (compatible; MSIE 8.0; Windows NT 6.1; WOW64; Trident\/4.0; SLCC2; .NET CLR 2.0.50727; .NET CLR 3.5.30729; .NET CLR 3.0.30729; .NET4.0C; .NET4.0E)",\n CURLOPT_FOLLOWLOCATION=>true,\n CURLOPT_RETURNTRANSFER=>true,\n CURLOPT_SSL_VERIFYPEER=>false,\n CURLOPT_SSL_VERIFYHOST=>false,\n CURLOPT_HEADER=>true,\n];<\/pre>\u76ee\u524d\u7684\u4ee3\u7801\uff1a<\/p>
$option = [<\/p>
CURLOPT_TIMEOUT => 30,<\/p>
];<\/p>
\u4fee\u6539\u540e\u7684\u4ee3\u7801\uff1a<\/p>
$option = [<\/p>
CURLOPT_TIMEOUT => 30,<\/p>
CURLOPT_CONNECTTIMEOUT => 25,\/\/CURLOPT_CONNECTTIMEOUT\u6709\u9ed8\u8ba4\u503c3\uff0c\u4e0a\u9762\u7684CURLOPT_TIMEOUT\u8bbe\u7684\u518d\u5927\u4e5f\u6ca1\u7528<\/p>
];<\/p>
<\/p>\u7ecf\u4e2a\u4eba\u5728\u672c\u5730\u6d4b\u8bd5CURLOPT_CONNECTTIMEOUT\u4f18\u5148\u8d77\u4f5c\u7528\uff0c\u4ed6\u6307\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff0c\u4e0d\u7ba1\u4f60CURLOPT_TIMEOUT\u8bbe\u7f6e\u591a\u5927\uff0c\u53ea\u8981\u8fbe\u5230\u4e86CURL_CONNECTTIMEOUT\u7684\u8d85\u65f6\u65f6\u95f4\uff0c\u8bf7\u6c42\u5c31\u4f1a\u62a5\u8d85\u65f6\uff1b\u4e00\u822c\u60c5\u51b5\u4e0bCURL_CONNECTTIMEOUT\u8981\u8bbe\u7f6e\u5f97\u6bd4CURLOPT_TIMEOUT\u5c0f\u4e00\u4e9b\uff0c\u4ece\u9ed8\u8ba4\u914d\u7f6e\u4e2d\u4e5f\u53ef\u4ee5\u770b\u51fa\u6765\uff1b<\/p>
<\/p>\u533a\u522b<\/p>
CURLOPT_CONNECTTIMEOUT\uff1a\u8fde\u63a5\u5bf9\u65b9\u4e3b\u673a\u65f6\u7684\u6700\u957f\u7b49\u5f85\u65f6\u95f4\uff0c\u6b64\u8bbe\u7f6e\u9650\u5236\u7684\u662f\u5efa\u7acb\u8fde\u63a5\u8fc7\u7a0b\u7684\u65f6\u95f4\uff0c\u5176\u5b83\u8fc7\u7a0b\u7684\u65f6\u95f4\u4e0d\u5728\u63a7\u5236\u8303\u56f4<\/p>
CURLOPT_TIMEOUT\uff1a\u6574\u4e2acURL\u51fd\u6570\u6267\u884c\u8fc7\u7a0b\u7684\u6700\u957f\u7b49\u5f85\u65f6\u95f4\uff0c\u4e5f\u5c31\u662f\u8bf4\uff0c\u8fd9\u4e2a\u65f6\u95f4\u662f\u5305\u542b\u8fde\u63a5\u7b49\u5f85\u65f6\u95f4\u7684<\/p>
\u56e0\u6b64\uff0c\u4e24\u8005\u662f\u5305\u542b\u548c\u88ab\u5305\u542b\u5173\u7cfb\uff0cCURLOPT_CONNECTTIMEOUT\u662fCURLOPT_TIMEOUT\u7684\u5176\u4e2d\u4e00\u90e8\u5206\uff0c\u5f53\u4e24\u8005\u90fd\u6709\u8bbe\u7f6e\u7684\u65f6\u5019\uff0cCURLOPT_TIMEOUT\u7684\u503c\u5e94\u6bd4CURLOPT_CONNECTTIMEOUT\u5927\u3002<\/p>
CURLOPT_CONNECTTIMEOUT \u7528\u6765\u544a\u8bc9 PHP \u5728\u6210\u529f\u8fde\u63a5\u670d\u52a1\u5668\u524d\u7b49\u5f85\u591a\u4e45\uff08\u8fde\u63a5\u6210\u529f\u4e4b\u540e\u5c31\u4f1a\u5f00\u59cb\u7f13\u51b2\u8f93\u51fa\uff09\uff0c\u8fd9\u4e2a\u53c2\u6570\u662f\u4e3a\u4e86\u5e94\u5bf9\u76ee\u6807\u670d\u52a1\u5668\u7684\u8fc7\u8f7d\uff0c\u4e0b\u7ebf\uff0c\u6216\u8005\u5d29\u6e83\u7b49\u53ef\u80fd\u72b6\u51b5\u3002<\/p>
CURLOPT_TIMEOUT \u7528\u6765\u544a\u8bc9\u6210\u529f PHP \u4ece\u670d\u52a1\u5668\u63a5\u6536\u7f13\u51b2\u5b8c\u6210\u524d\u9700\u8981\u7b49\u5f85\u591a\u957f\u65f6\u95f4\uff0c\u5982\u679c\u76ee\u6807\u662f\u4e2a\u5de8\u5927\u7684\u6587\u4ef6\uff0c\u751f\u6210\u5185\u5bb9\u901f\u5ea6\u8fc7\u6162\u6216\u8005\u94fe\u8def\u901f\u5ea6\u8fc7\u6162\uff0c\u8fd9\u4e2a\u53c2\u6570\u5c31\u4f1a\u5f88\u6709\u7528\u3002<\/p>
<\/p>\u7b80\u5355\u6765\u8bb2\u5c31\u662fCURLOPT_CONNECTTIMEOUT\u53ea\u7ba1\u8fde\u63a5\u65f6\u95f4\uff0c\u800c\u8fde\u63a5\u5b8c\u6210\u4f60\u7684\u7a0b\u5e8f\u8fd8\u5728\u5176\u5b83\u7684\u4e8b\u5e72\u4e5f\u8981\u82b1\u8d39\u65f6\u95f4\uff0c\u8fde\u63a5+\u8bf7\u6c42\u7684\u7a0b\u5e8f\uff08http\u63a5\u53e3\uff09\u5e72\u6d3b\u7684\u65f6\u95f4\u5c31\u4e0d\u80fd\u8d85\u8fc7CURLOPT_TIMEOUT<\/p>
<\/p>\u82f1\u6587\u8bf4\u660e\uff1a<\/p>
CURLOPT_TIMEOUT - set maximum time the request is allowed to take(\u8bbe\u7f6e\u5141\u8bb8\u8bf7\u6c42\u7684\u6700\u957f\u65f6\u95f4)<\/span><\/p>
CURLOPT_CONNECTTIMEOUT - timeout for the connect phase(\u8fde\u63a5\u9636\u6bb5\u8d85\u65f6\u65f6\u95f4<\/span>)<\/span><\/span><\/p>
https:\/\/curl.se\/libcurl\/c\/CURLOPT_TIMEOUT.html<\/a><\/span><\/span><\/p>