{"id":47096,"date":"2024-12-02T17:04:00","date_gmt":"2024-12-02T09:04:00","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47096\/"},"modified":"2024-12-02T17:04:00","modified_gmt":"2024-12-02T09:04:00","slug":"php-%e5%87%bd%e6%95%b0%e6%80%8e%e4%b9%88%e5%a4%84%e7%90%86-json-%e6%95%b0%e6%8d%ae","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47096\/","title":{"rendered":"PHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e<\/h1>\n<p><span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u6587\u7ae0<\/span>\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\">\u300aPHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e\u300b<\/span>\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230<span style=\"color: #FF6600;, Helvetica, Arial, sans-serif;font-size: 14px;background-color: #FFFFFF\"><\/span>\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01<\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u5904\u7406 JSON \u6570\u636e\u7684\u51fd\u6570\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u8f7b\u677e\u5730\u7f16\u7801\u548c\u89e3\u7801 JSON \u6570\u636e\u30021. \u7f16\u7801 JSON \u6570\u636e\uff1ajson_encode() \u5c06 PHP \u53d8\u91cf\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u30022. \u89e3\u7801 JSON \u6570\u636e\uff1ajson_decode() \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u7801\u4e3a PHP \u53d8\u91cf\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u53ef\u4ee5\u4ece API \u83b7\u53d6 JSON \u6570\u636e\u5e76\u5229\u7528\u8fd9\u4e9b\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a PHP \u6570\u7ec4\uff0c\u4ece\u800c\u7b80\u5316 JSON \u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241025\/1729865857671ba8813d0d0.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u5904\u7406 JSON \u6570\u636e<\/strong><\/p>\n<p>PHP \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u7528\u4e8e\u5904\u7406 JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u6570\u636e\u3002\u8fd9\u4e9b\u51fd\u6570\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u8f7b\u677e\u5730\u5c06 JSON \u6570\u636e\u7f16\u7801\u4e3a PHP \u6570\u7ec4\u3001\u5bf9\u8c61\u6216\u5b57\u7b26\u4e32\uff0c\u4ee5\u53ca\u5c06 PHP \u6570\u7ec4\u3001\u5bf9\u8c61\u6216\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a JSON \u6570\u636e\u3002<\/p>\n<p><strong>\u7f16\u7801 JSON \u6570\u636e<\/strong><\/p>\n<ul>\n<li><strong>json_encode()\uff1a<\/strong>\u5c06 PHP \u53d8\u91cf\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<\/ul>\n<pre>$php_array = ['name' =&gt; 'John Doe', 'age' =&gt; 30];\n$json_string = json_encode($php_array);\n\necho $json_string; \/\/ \u8f93\u51fa\uff1a{\"name\":\"John Doe\",\"age\":30}<\/pre>\n<p><strong>\u89e3\u7801 JSON \u6570\u636e<\/strong><\/p>\n<ul>\n<li><strong>json_decode()\uff1a<\/strong>\u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u7801\u4e3a PHP \u53d8\u91cf\u3002<\/li>\n<\/ul>\n<pre>$json_string = '{\"name\":\"John Doe\",\"age\":30}';\n$php_array = json_decode($json_string);\n\nprint_r($php_array); \/\/ \u8f93\u51fa\uff1aArray ( [name] =&gt; John Doe [age] =&gt; 30 )<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u4ece API \u83b7\u53d6\u6570\u636e<\/strong><\/p>\n<p>\u8003\u8651\u4e00\u4e2a\u4f7f\u7528 cURL \u4ece API \u83b7\u53d6 JSON \u6570\u636e\u7684\u793a\u4f8b\uff1a<\/p>\n<pre>\/\/ \u4f7f\u7528 cURL \u4ece API \u83b7\u53d6 JSON \u6570\u636e\n$url = 'https:\/\/example.com\/api\/v1\/users';\n$ch = curl_init($url);\ncurl_setopt($ch, CURLOPT_RETURNTRANSFER, true);\n$response = curl_exec($ch);\ncurl_close($ch);\n\n\/\/ \u5c06 JSON \u6570\u636e\u89e3\u7801\u4e3a PHP \u6570\u7ec4\n$data = json_decode($response);\n\n\/\/ \u904d\u5386\u6570\u636e\u5e76\u6253\u5370\u7528\u6237\u540d\nforeach ($data-&gt;users as $user) {\n    echo $user-&gt;name . \"\\n\";\n}<\/pre>\n<p>\u901a\u8fc7\u4f7f\u7528 PHP \u5185\u7f6e\u7684 JSON \u51fd\u6570\uff0c\u5f00\u53d1\u4eba\u5458\u53ef\u4ee5\u8f7b\u677e\u5730\u5904\u7406\u6765\u81ea API \u6216\u5176\u4ed6\u6765\u6e90\u7684 JSON \u6570\u636e\uff0c\u5e76\u5c06\u5176\u8f6c\u6362\u4e3a PHP \u53d8\u91cf\u6216\u5b57\u7b26\u4e32\u3002\u8fd9\u4f7f\u5f97\u5f00\u53d1 JSON \u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u53d8\u5f97\u7b80\u5355\u65b9\u4fbf\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662f\u672c\u6587\u7684\u5168\u90e8\u5185\u5bb9\u4e86\uff0c\u662f\u5426\u6709\u987a\u5229\u5e2e\u52a9\u4f60\u89e3\u51b3\u95ee\u9898\uff1f\u82e5\u662f\u80fd\u7ed9\u4f60\u5e26\u6765\u5b66\u4e60\u4e0a\u7684\u5e2e\u52a9\uff0c\u8bf7\u5927\u5bb6\u591a\u591a\u652f\u6301\u7c73\u4e91\uff01\u66f4\u591a\u5173\u4e8e\u6587\u7ae0\u7684\u76f8\u5173\u77e5\u8bc6\uff0c\u4e5f\u53ef\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u600e\u4e48\u5904\u7406 JSON \u6570\u636e\u300b\uff0c\u8fd9\u7bc7\u6587\u7ae0\u4e3b\u8981\u4f1a\u8bb2\u5230\u7b49\u7b49\u77e5\u8bc6\u70b9\uff0c\u5982\u679c\u4f60\u5728\u770b\u5b8c\u672c\u7bc7\u6587\u7ae0\u540e\uff0c\u6709\u66f4\u597d\u7684\u5efa\u8bae\u6216\u8005\u53d1\u73b0\u54ea\u91cc\u6709\u95ee\u9898\uff0c\u5e0c\u671b\u5927\u5bb6\u90fd\u80fd\u79ef\u6781\u8bc4\u8bba\u6307\u51fa\uff0c\u8c22\u8c22\uff01\u5e0c\u671b\u6211\u4eec\u80fd\u4e00\u8d77\u52a0\u6cb9\u8fdb\u6b65\uff01 PHP \u63d0\u4f9b\u4e86\u5904\u7406 JSON \u6570\u636e\u7684\u51fd\u6570\uff0c\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u8f7b\u677e\u5730\u7f16\u7801\u548c\u89e3\u7801 JSON \u6570\u636e\u30021. \u7f16\u7801 JSON \u6570\u636e\uff1ajson_encode() \u5c06 PHP \u53d8\u91cf\u7f16\u7801\u4e3a JSON \u5b57\u7b26\u4e32\u30022. \u89e3\u7801 JSON \u6570\u636e\uff1ajson_decode() \u5c06 JSON \u5b57\u7b26\u4e32\u89e3\u7801\u4e3a PHP \u53d8\u91cf\u3002\u5b9e\u6218\u6848\u4f8b\u4e2d\uff0c\u53ef\u4ee5\u4ece API \u83b7\u53d6 JSON \u6570\u636e\u5e76\u5229\u7528\u8fd9\u4e9b\u51fd\u6570\u5c06\u5176\u8f6c\u6362\u4e3a PHP \u6570\u7ec4\uff0c\u4ece\u800c\u7b80\u5316 JSON \u9a71\u52a8\u7684\u5e94\u7528\u7a0b\u5e8f\u7684\u5f00\u53d1\u3002 PHP \u5904\u7406 JSON \u6570\u636e PHP \u63d0\u4f9b\u4e86\u8bb8\u591a\u5185\u7f6e\u51fd\u6570\u7528\u4e8e\u5904\u7406 JSON\uff08JavaScript \u5bf9\u8c61\u8868\u793a\u6cd5\uff09\u6570\u636e\u3002\u8fd9\u4e9b\u51fd\u6570\u4f7f\u5f00\u53d1\u4eba\u5458\u80fd\u591f\u8f7b\u677e\u5730\u5c06 JSON \u6570\u636e\u7f16\u7801\u4e3a PHP \u6570\u7ec4\u3001\u5bf9\u8c61\u6216\u5b57\u7b26\u4e32\uff0c\u4ee5\u53ca\u5c06 PHP \u6570\u7ec4\u3001\u5bf9\u8c61\u6216\u5b57\u7b26\u4e32\u89e3\u7801\u4e3a JSON \u6570\u636e\u3002 \u7f16\u7801 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[],"class_list":["post-47096","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47096","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/comments?post=47096"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47096\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47096"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47096"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47096"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}