{"id":1826,"date":"2026-03-03T09:54:42","date_gmt":"2026-03-03T01:54:42","guid":{"rendered":"https:\/\/www.52runoob.com\/?p=1826"},"modified":"2026-03-03T09:54:42","modified_gmt":"2026-03-03T01:54:42","slug":"php%e4%b8%ad%e6%ad%a3%e7%a1%ae%e5%a4%84%e7%90%86http%e5%93%8d%e5%ba%94%e5%b9%b6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b0%e7%bb%84%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97","status":"publish","type":"post","link":"https:\/\/www.52runoob.com\/index.php\/2026\/03\/03\/php%e4%b8%ad%e6%ad%a3%e7%a1%ae%e5%a4%84%e7%90%86http%e5%93%8d%e5%ba%94%e5%b9%b6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b0%e7%bb%84%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97\/","title":{"rendered":"PHP\u4e2d\u6b63\u786e\u5904\u7406HTTP\u54cd\u5e94\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\u7684\u5b8c\u6574\u6307\u5357"},"content":{"rendered":"\n<p>\u5728 PHP \u4e2d\u6b63\u786e\u5904\u7406 HTTP \u54cd\u5e94\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6838\u5fc3\u662f\u505a\u597d\u8fd9 5 \u4ef6\u4e8b\uff1a<\/p>\n\n\n\n<ol class=\"wp-block-list\">\n<li>\u6b63\u786e\u53d1\u8d77\u8bf7\u6c42\uff08\u8bbe\u7f6e\u8d85\u65f6 \/ Header \/ SSL\uff09<\/li>\n\n\n\n<li>\u6b63\u786e\u5224\u65ad HTTP \u72b6\u6001\u7801<\/li>\n\n\n\n<li>\u6b63\u786e\u5224\u65ad Content-Type<\/li>\n\n\n\n<li>\u6b63\u786e\u89e3\u6790 JSON<\/li>\n\n\n\n<li>\u6b63\u786e\u5904\u7406\u5f02\u5e38\u4e0e\u9519\u8bef<\/li>\n<\/ol>\n\n\n\n<p>\u4e0b\u9762\u7ed9\u4f60\u4e00\u4efd <strong>\u751f\u4ea7\u7ea7\u5b8c\u6574\u6307\u5357<\/strong>\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e00\u3001\u63a8\u8350\u65b9\u5f0f\uff1a\u4f7f\u7528 cURL\uff08\u6700\u7a33\u5b9a\uff09<\/h1>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 \u6807\u51c6 GET \u8bf7\u6c42\u793a\u4f8b<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction httpGet(string $url, array $headers = &#x5B;]): array\n{\n    $ch = curl_init($url);\n\n    curl_setopt_array($ch, &#x5B;\n        CURLOPT_RETURNTRANSFER =&gt; true,\n        CURLOPT_TIMEOUT =&gt; 10,\n        CURLOPT_CONNECTTIMEOUT =&gt; 5,\n        CURLOPT_HTTPHEADER =&gt; $headers,\n        CURLOPT_FOLLOWLOCATION =&gt; true,\n        CURLOPT_SSL_VERIFYPEER =&gt; true,\n        CURLOPT_SSL_VERIFYHOST =&gt; 2,\n    ]);\n\n    $response = curl_exec($ch);\n\n    if ($response === false) {\n        throw new Exception(&#039;Curl Error: &#039; . curl_error($ch));\n    }\n\n    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n    $contentType = curl_getinfo($ch, CURLINFO_CONTENT_TYPE);\n\n    curl_close($ch);\n\n    if ($statusCode !== 200) {\n        throw new Exception(&quot;HTTP Error: {$statusCode}&quot;);\n    }\n\n    if (strpos($contentType, &#039;application\/json&#039;) === false) {\n        throw new Exception(&quot;Invalid Content-Type: {$contentType}&quot;);\n    }\n\n    $data = json_decode($response, true);\n\n    if (json_last_error() !== JSON_ERROR_NONE) {\n        throw new Exception(&#039;JSON Decode Error: &#039; . json_last_error_msg());\n    }\n\n    return $data;\n}\n\n<\/pre><\/div>\n\n\n<p>\u8c03\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$data = httpGet(&quot;https:\/\/api.example.com\/user&quot;);\nprint_r($data);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e8c\u3001POST \u8bf7\u6c42\uff08\u53d1\u9001 JSON\uff09<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfunction httpPost(string $url, array $payload): array\n{\n    $ch = curl_init($url);\n\n    $jsonData = json_encode($payload);\n\n    curl_setopt_array($ch, &#x5B;\n        CURLOPT_RETURNTRANSFER =&gt; true,\n        CURLOPT_POST =&gt; true,\n        CURLOPT_POSTFIELDS =&gt; $jsonData,\n        CURLOPT_HTTPHEADER =&gt; &#x5B;\n            &#039;Content-Type: application\/json&#039;,\n            &#039;Content-Length: &#039; . strlen($jsonData),\n        ],\n        CURLOPT_TIMEOUT =&gt; 10,\n        CURLOPT_CONNECTTIMEOUT =&gt; 5,\n    ]);\n\n    $response = curl_exec($ch);\n\n    if ($response === false) {\n        throw new Exception(&#039;Curl Error: &#039; . curl_error($ch));\n    }\n\n    $statusCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);\n\n    curl_close($ch);\n\n    if ($statusCode !== 200) {\n        throw new Exception(&quot;HTTP Error: {$statusCode}&quot;);\n    }\n\n    return json_decode($response, true);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e09\u3001\u4f7f\u7528 file_get_contents\uff08\u7b80\u5355\u4f46\u4e0d\u63a8\u8350\u751f\u4ea7\uff09<\/h1>\n\n\n\n<p>\u5fc5\u987b\u8bbe\u7f6e\u8d85\u65f6\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$options = &#x5B;\n    &#039;http&#039; =&gt; &#x5B;\n        &#039;method&#039;  =&gt; &#039;GET&#039;,\n        &#039;timeout&#039; =&gt; 5,\n        &#039;header&#039;  =&gt; &quot;Accept: application\/json\\r\\n&quot;\n    ]\n];\n\n$context = stream_context_create($options);\n\n$response = file_get_contents($url, false, $context);\n\n$data = json_decode($response, true);\n\n<\/pre><\/div>\n\n\n<p>\u26a0\ufe0f \u9ed8\u8ba4\u65e0\u8d85\u65f6\uff0c\u5bb9\u6613\u5361\u6b7b\u63a5\u53e3\u3002<\/p>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u56db\u3001JSON \u8f6c\u6570\u7ec4\u7684\u6b63\u786e\u65b9\u5f0f<\/h1>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$data = json_decode($response, true);\n\n<\/pre><\/div>\n\n\n<p>\u7b2c\u4e8c\u4e2a\u53c2\u6570\u5fc5\u987b\u662f <code>true<\/code>\uff0c\u5426\u5219\u8fd4\u56de\u5bf9\u8c61\u3002<\/p>\n\n\n\n<p>\u9519\u8bef\u5904\u7406\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (json_last_error() !== JSON_ERROR_NONE) {\n    echo json_last_error_msg();\n}\n\n<\/pre><\/div>\n\n\n<p>PHP 8 \u63a8\u8350\u65b9\u5f0f\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$data = json_decode($response, true, 512, JSON_THROW_ON_ERROR);\n\n<\/pre><\/div>\n\n\n<p>\u914d\u5408\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ntry {\n    $data = json_decode($response, true, 512, JSON_THROW_ON_ERROR);\n} catch (JsonException $e) {\n    echo $e-&gt;getMessage();\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e94\u3001\u5904\u7406\u5e38\u89c1\u5f02\u5e38\u60c5\u51b5<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">1\ufe0f\u20e3 HTTP \u72b6\u6001\u7801\u4e0d\u662f 200<\/h2>\n\n\n\n<p>\u4e0d\u8981\u76f4\u63a5\u89e3\u6790 JSON\u3002<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif ($statusCode &gt;= 400) {\n    throw new Exception(&quot;Client\/Server Error&quot;);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">2\ufe0f\u20e3 \u8fd4\u56de HTML \u800c\u4e0d\u662f JSON<\/h2>\n\n\n\n<p>\u5f88\u591a\u63a5\u53e3\u62a5\u9519\u65f6\u8fd4\u56de HTML\u3002<\/p>\n\n\n\n<p>\u5224\u65ad\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (strpos($contentType, &#039;application\/json&#039;) === false) {\n    throw new Exception(&quot;Response is not JSON&quot;);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">3\ufe0f\u20e3 \u7a7a\u54cd\u5e94<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nif (empty($response)) {\n    throw new Exception(&quot;Empty response&quot;);\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">4\ufe0f\u20e3 \u8d85\u65f6<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nCURLOPT_TIMEOUT =&gt; 10\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516d\u3001\u751f\u4ea7\u73af\u5883\u63a8\u8350\u5c01\u88c5\u7ed3\u6784<\/h1>\n\n\n\n<p>\u5efa\u8bae\u5c01\u88c5\u7edf\u4e00\u8bf7\u6c42\u7c7b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nclass HttpClient\n{\n    public static function request(string $url, string $method = &#039;GET&#039;, array $data = &#x5B;]): array\n    {\n        $ch = curl_init($url);\n\n        $options = &#x5B;\n            CURLOPT_RETURNTRANSFER =&gt; true,\n            CURLOPT_TIMEOUT =&gt; 10,\n            CURLOPT_CONNECTTIMEOUT =&gt; 5,\n        ];\n\n        if ($method === &#039;POST&#039;) {\n            $options&#x5B;CURLOPT_POST] = true;\n            $options&#x5B;CURLOPT_POSTFIELDS] = json_encode($data);\n            $options&#x5B;CURLOPT_HTTPHEADER] = &#x5B;&#039;Content-Type: application\/json&#039;];\n        }\n\n        curl_setopt_array($ch, $options);\n\n        $response = curl_exec($ch);\n\n        if ($response === false) {\n            throw new Exception(curl_error($ch));\n        }\n\n        curl_close($ch);\n\n        return json_decode($response, true, 512, JSON_THROW_ON_ERROR);\n    }\n}\n\n<\/pre><\/div>\n\n\n<p>\u8c03\u7528\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$result = HttpClient::request(&#039;https:\/\/api.example.com&#039;, &#039;POST&#039;, &#x5B;&#039;id&#039; =&gt; 1]);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e03\u3001\u9ad8\u7ea7\u4f18\u5316\u5efa\u8bae<\/h1>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 1. \u91cd\u8bd5\u673a\u5236<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nfor ($i = 0; $i &amp;lt; 3; $i++) {\n    try {\n        return httpGet($url);\n    } catch (Exception $e) {\n        sleep(1);\n    }\n}\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 2. \u65e5\u5fd7\u8bb0\u5f55<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\nerror_log($response);\n\n<\/pre><\/div>\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h2 class=\"wp-block-heading\">\u2705 3. \u4f7f\u7528 Guzzle\uff08\u63a8\u8350\u5927\u578b\u9879\u76ee\uff09<\/h2>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\ncomposer require guzzlehttp\/guzzle\n\n<\/pre><\/div>\n\n\n<p>\u793a\u4f8b\uff1a<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: plain; title: ; notranslate\" title=\"\">\n$client = new \\GuzzleHttp\\Client();\n\n$response = $client-&gt;request(&#039;GET&#039;, $url);\n\n$data = json_decode($response-&gt;getBody(), true);\n\n<\/pre><\/div>\n\n\n<p>\u4f18\u52bf\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u81ea\u52a8\u5904\u7406\u5f02\u5e38<\/li>\n\n\n\n<li>\u652f\u6301\u4e2d\u95f4\u4ef6<\/li>\n\n\n\n<li>\u652f\u6301\u5e76\u53d1<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u516b\u3001\u5e38\u89c1\u9519\u8bef\u603b\u7ed3<\/h1>\n\n\n\n<figure class=\"wp-block-table\"><table class=\"has-fixed-layout\"><thead><tr><th>\u9519\u8bef<\/th><th>\u539f\u56e0<\/th><\/tr><\/thead><tbody><tr><td>json_decode \u8fd4\u56de null<\/td><td>JSON \u683c\u5f0f\u9519\u8bef<\/td><\/tr><tr><td>\u4e00\u76f4\u5361\u4f4f<\/td><td>\u6ca1\u8bbe\u7f6e timeout<\/td><\/tr><tr><td>\u89e3\u6790\u62a5\u9519<\/td><td>\u8fd4\u56de HTML<\/td><\/tr><tr><td>502<\/td><td>\u540e\u7aef\u63a5\u53e3\u6302\u4e86<\/td><\/tr><tr><td>SSL \u9519\u8bef<\/td><td>\u8bc1\u4e66\u9a8c\u8bc1\u5931\u8d25<\/td><\/tr><\/tbody><\/table><\/figure>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<h1 class=\"wp-block-heading\">\u4e5d\u3001\u6700\u4f73\u5b9e\u8df5\u603b\u7ed3<\/h1>\n\n\n\n<p>\u751f\u4ea7\u73af\u5883\u5fc5\u987b\u505a\u5230\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>\u8bbe\u7f6e\u8d85\u65f6<\/li>\n\n\n\n<li>\u9a8c\u8bc1 HTTP \u72b6\u6001\u7801<\/li>\n\n\n\n<li>\u9a8c\u8bc1 Content-Type<\/li>\n\n\n\n<li>\u6355\u83b7 JSON \u5f02\u5e38<\/li>\n\n\n\n<li>\u8bb0\u5f55\u9519\u8bef\u65e5\u5fd7<\/li>\n\n\n\n<li>\u4e0d\u4fe1\u4efb\u5916\u90e8\u63a5\u53e3<\/li>\n<\/ul>\n\n\n\n<hr class=\"wp-block-separator has-alpha-channel-opacity\"\/>\n\n\n\n<p>\u5982\u679c\u4f60\u544a\u8bc9\u6211\uff1a<\/p>\n\n\n\n<ul class=\"wp-block-list\">\n<li>PHP \u7248\u672c\uff1f<\/li>\n\n\n\n<li>\u662f\u5bf9\u63a5\u7b2c\u4e09\u65b9 API \u8fd8\u662f\u5185\u90e8\u5fae\u670d\u52a1\uff1f<\/li>\n\n\n\n<li>\u6709\u6ca1\u6709\u9047\u5230\u5177\u4f53\u62a5\u9519\uff1f<\/li>\n<\/ul>\n\n\n\n<p>\u6211\u53ef\u4ee5\u7ed9\u4f60\u505a\u4e00\u5957\u66f4\u8d34\u5408\u4f60\u5f53\u524d\u9879\u76ee\u7684\u6700\u4f73\u5b9e\u8df5\u65b9\u6848\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5728 PHP \u4e2d\u6b63\u786e\u5904\u7406 HTTP \u54cd\u5e94\u5e76\u8f6c\u6362\u4e3a\u6570\u7ec4\uff0c\u6838\u5fc3\u662f\u505a\u597d\u8fd9 5 \u4ef6\u4e8b\uff1a \u4e0b&#8230; <a class=\"more-link\" href=\"https:\/\/www.52runoob.com\/index.php\/2026\/03\/03\/php%e4%b8%ad%e6%ad%a3%e7%a1%ae%e5%a4%84%e7%90%86http%e5%93%8d%e5%ba%94%e5%b9%b6%e8%bd%ac%e6%8d%a2%e4%b8%ba%e6%95%b0%e7%bb%84%e7%9a%84%e5%ae%8c%e6%95%b4%e6%8c%87%e5%8d%97\/\">Continue Reading &rarr;<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[23],"tags":[],"class_list":["post-1826","post","type-post","status-publish","format-standard","hentry","category-php"],"amp_enabled":true,"_links":{"self":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1826","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/comments?post=1826"}],"version-history":[{"count":1,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1826\/revisions"}],"predecessor-version":[{"id":1827,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/posts\/1826\/revisions\/1827"}],"wp:attachment":[{"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/media?parent=1826"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/categories?post=1826"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.52runoob.com\/index.php\/wp-json\/wp\/v2\/tags?post=1826"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}