{"id":289,"date":"2024-09-18T15:17:44","date_gmt":"2024-09-18T07:17:44","guid":{"rendered":"https:\/\/znl.pub\/?p=289"},"modified":"2024-09-18T15:25:51","modified_gmt":"2024-09-18T07:25:51","slug":"%e3%80%90php%e4%bb%a3%e7%a0%81%e7%89%87%e6%ae%b5%e3%80%91%e5%b0%81%e8%a3%85curl%e6%9e%84%e9%80%a0get%e5%87%bd%e6%95%b0","status":"publish","type":"post","link":"https:\/\/znl.pub\/?p=289","title":{"rendered":"\u3010PHP\u4ee3\u7801\u7247\u6bb5\u3011\u5c01\u88c5CURL\u6784\u9020get\u51fd\u6570"},"content":{"rendered":"\n<p>\u5b9e\u9645\u4e0a\u6211\u4eec\u8ba9\u5927\u6a21\u578b\u751f\u6210\u4e00\u6bb5\u4ee3\u7801\u4e0d\u662f\u4e0d\u884c\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u9645\u4e5f\u662f\u5927\u6a21\u578b\u751f\u6210\u7684\uff0c\u7136\u540e\u6211\u518d\u4fee\u6539\u7684\u3002\u8bb0\u4e0b\u6765\u4e3b\u8981\u662f\u65b9\u4fbf\u81ea\u5df1\u4f7f\u7528\u7684\u65f6\u5019\u597d\u627e\uff0c\u4e5f\u662f\u4e3a\u4e86\u79ef\u7d2f\u7ecf\u9a8c\u3002<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function _curl_get($url) {\n    $ch = curl_init();\n    \n    \/\/ \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\n    $headers = &#91;\n        \"User-Agent: Mozilla\/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit\/537.36 (KHTML, like Gecko) Chrome\/58.0.3029.110 Safari\/537.3\", \/\/ \u81ea\u5b9a\u4e49 User-Agent \uff0c\u8fd8\u6709\u53e6\u4e00\u79cd\u505a\u6cd5\u5c31\u662f\u8bbe\u7f6e CURLOPT_USERAGENT \n        \"X-Requested-With: XMLHttpRequest\", \/\/ \u6807\u8bc6\u662f\u6765\u81ea XHR \u8bf7\u6c42\n        \"Accept: application\/json\", \/\/ \u6839\u636e\u9700\u8981\u914d\u7f6e\n    ];\n    \n    \/\/ \u8bbe\u7f6e URL\n    curl_setopt($ch, CURLOPT_URL, $url);\n    \n    \/\/ \u8fd4\u56de\u7ed3\u679c\u800c\u4e0d\u662f\u76f4\u63a5\u8f93\u51fa\n    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n    \n    \/\/ \u8ddf\u968f\u91cd\u5b9a\u5411\n    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);\n    \n    \/\/ \u8bbe\u7f6e\u8fde\u63a5\u8d85\u65f6\u65f6\u95f4\uff08\u79d2\uff09\n    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 10); \/\/ \u8fde\u63a5\u8d85\u65f6\uff0c10\u79d2,\u5177\u4f53\u6765\u8bf4,\u8fd9\u4e2a\u8d85\u65f6\u6307\u7684\u662f\u4ee5\u4e0b\u8fc7\u7a0b\u7684\u65f6\u95f4\u9650\u5236:DNS \u89e3\u6790\u548cTCP \u4e09\u6b21\u63e1\u624b\u7684TCP\u8fde\u63a5\u5efa\u7acb\u8d85\u65f6\u3002\n    \n    \/\/ \u8bbe\u7f6e\u8bf7\u6c42\u8d85\u65f6\u65f6\u95f4\uff08\u79d2\uff09\n    curl_setopt($ch, CURLOPT_TIMEOUT, 30); \/\/ \u8bf7\u6c42\u8d85\u65f6\uff0c30\u79d2\uff0c\u5177\u4f53\u6765\u8bf4,\u8fd9\u4e2a\u8d85\u65f6\u63a7\u5236\u6574\u4e2aHTTP\u8bf7\u6c42\u7684\u65f6\u95f4\uff0c\u5305\u62ec\u8fde\u63a5\u65f6\u95f4\u3001\u6570\u636e\u4f20\u8f93\u65f6\u95f4\u3002\n    \n    \/\/ \u542f\u7528\u8fde\u63a5\u590d\u7528\n    curl_setopt($ch, CURLOPT_FORBID_REUSE, false);\n    curl_setopt($ch, CURLOPT_FRESH_CONNECT, false);\n    \n    \/\/ \u542f\u7528\u538b\u7f29\n    curl_setopt($ch, CURLOPT_ENCODING, 'gzip, deflate');\n    \n    \/\/ \u542f\u7528 HTTP\/2\n    curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);\n    \n    \/\/ SSL\/TLS \u5b89\u5168\u8bbe\u7f6e\n    curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); \/\/ \u4e0d\u9a8c\u8bc1\u8bc1\u4e66\u5408\u6cd5\u6027\n    curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false); \/\/ \u4e0d\u9a8c\u8bc1\u4e3b\u673a\u540d\n    \n    \/\/ \u8bbe\u7f6e\u81ea\u5b9a\u4e49\u8bf7\u6c42\u5934\n    curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);\n    \n    \/\/ \u6570\u636e\u4f20\u8f93\u4f18\u5316\n    curl_setopt($ch, CURLOPT_BUFFERSIZE, 128000);\/\/ \u8bbe\u7f6e\u7f13\u51b2\u533a\u5927\u5c0f\u4e3a 128KB\n    \n    $response = curl_exec($ch);\n    \n    if ($error = curl_error($ch)) {\n        curl_close($ch);\n        throw new Exception(\"cURL error: \" . $error);\n    }\n    \n    $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n    curl_close($ch);\n    \n    if ($httpCode !== 200) {\n        throw new Exception(\"HTTP request failed with status code $httpCode\");\n    }\n    \n    return $response;\n}<\/code><\/pre>\n\n\n\n<p>\u4e3a\u4ec0\u4e48\u8981\u5c01\u88c5\u8fd9\u4e2aCURL\u5462\uff0c\u5b9e\u9645\u4e0a\u662f\u56e0\u4e3aPHP\u63d0\u4f9b\u7684\u8fd9\u4e00\u6a21\u5757\u5b9e\u5728\u96be\u4ee5\u8bb0\u5fc6\uff0c\u770b\u4e0a\u53bb\u5f88\u7075\u6d3b\uff0c\u5b9e\u5219\u4e0d\u7136\uff0c\u4f60\u4e0d\u6bcf\u6b21\u67e5\u8bcd\u5178\u5b8c\u5168\u4e0d\u4f1a\u4f7f\u7528\u3002\u6211\u4eec\u901a\u5e38\u4f1a\u5ffd\u7565ssl\u8ba4\u8bc1\uff0c\u5e76\u4e14\u9700\u8981\u8bbe\u7f6e\u8d85\u65f6\uff0c\u9700\u8981\u4fee\u6539user-agent\u3002\u4ee5\u4e0a\u51fd\u6570\u7684\u7528\u6cd5\uff1a<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>try {\n    $url = \"https:\/\/example.com\";\n    $response = _curl_get($url);\n    echo \"Response:\\n$response\\n\";\n} catch (Exception $e) {\n    echo \"Error: \" . $e-&gt;getMessage();\n}<\/code><\/pre>\n\n\n\n<p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b9e\u9645\u4e0a\u6211\u4eec\u8ba9\u5927\u6a21\u578b\u751f\u6210\u4e00\u6bb5\u4ee3\u7801\u4e0d\u662f\u4e0d\u884c\uff0c\u4ee5\u4e0b\u4ee3\u7801\u5b9e\u9645\u4e5f\u662f\u5927\u6a21\u578b\u751f\u6210\u7684\uff0c\u7136\u540e\u6211\u518d\u4fee\u6539\u7684\u3002\u8bb0\u4e0b\u6765\u4e3b\u8981\u662f\u65b9\u4fbf\u81ea\u5df1\u4f7f\u7528&#8230;<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[21,22],"tags":[25,26],"class_list":["post-289","post","type-post","status-publish","format-standard","hentry","category-php","category-22","tag-25","tag-26"],"_links":{"self":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/289","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=289"}],"version-history":[{"count":3,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions"}],"predecessor-version":[{"id":292,"href":"https:\/\/znl.pub\/index.php?rest_route=\/wp\/v2\/posts\/289\/revisions\/292"}],"wp:attachment":[{"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=289"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=289"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/znl.pub\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=289"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}