{"id":47757,"date":"2024-12-02T16:22:23","date_gmt":"2024-12-02T08:22:23","guid":{"rendered":"https:\/\/fwq.ai\/blog\/47757\/"},"modified":"2024-12-02T16:22:23","modified_gmt":"2024-12-02T08:22:23","slug":"php-%e5%87%bd%e6%95%b0%e5%a6%82%e4%bd%95%e8%bf%94%e5%9b%9e-json","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/47757\/","title":{"rendered":"PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON"},"content":{"rendered":"<p><b><\/b> <\/p>\n<h1>PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON<\/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\u5982\u4f55\u8fd4\u56de JSON\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 \u901a\u8fc7 json_encode() \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\uff0c\u5b83\u63a5\u53d7\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de JSON \u5b57\u7b26\u4e32\u3002\u4f7f\u7528 json_encode() \u51fd\u6570\u548c\u8bbe\u7f6e\u9002\u5f53\u7684 HTTP \u5934\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 JSON \u6570\u636e\u4f5c\u4e3a\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u6216\u4f7f\u7528\u5176\u4ed6\u9009\u9879\uff0c\u4f8b\u5982 json_decode() \u548c json_exit() \u51fd\u6570\uff0c\u4f7f\u7528 JSONP \u56de\u8c03\u6216\u5229\u7528\u7b2c\u4e09\u65b9 JSON \u5e93\u3002<\/p>\n<p><img decoding=\"async\" src=\"https:\/\/www.17golang.com\/uploads\/20241026\/1729934674671cb552ddaac.jpg\" class=\"aligncenter\" title=\"PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON\u63d2\u56fe\" alt=\"PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON\u63d2\u56fe\" \/><\/p>\n<p><strong>PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON<\/strong><\/p>\n<p>JSON (JavaScript Object Notation) \u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u53ef\u4ee5\u5728 PHP \u4e2d\u8f7b\u677e\u4f7f\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u3002<\/p>\n<p><strong>json_encode() \u51fd\u6570<\/strong><\/p>\n<p><code>json_encode()<\/code> \u51fd\u6570\u5c06\u4e00\u4e2a PHP \u503c\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b JSON \u8868\u793a\u7684\u5b57\u7b26\u4e32\u3002<\/p>\n<pre>&lt;?php\n$array = array(\"name\" =&gt; \"John Doe\", \"age\" =&gt; 30);\n\n$json = json_encode($array);\n\necho $json; \/\/ \u8f93\u51fa: {\"name\":\"John Doe\",\"age\":30}\n?&gt;<\/pre>\n<p><strong>\u5b9e\u6218\u6848\u4f8b\uff1a\u8fd4\u56de JSON \u54cd\u5e94<\/strong><\/p>\n<p>\u5047\u8bbe\u4f60\u6709\u4e00\u4e2a PHP API \u7aef\u70b9\uff0c\u9700\u8981\u5411\u5ba2\u6237\u7aef\u8fd4\u56de JSON \u54cd\u5e94\u3002\u4f60\u53ef\u4ee5\u4f7f\u7528\u4ee5\u4e0b\u6b65\u9aa4\u6765\u5b9e\u73b0\uff1a<\/p>\n<ol>\n<li>\u4f7f\u7528 <code>json_encode()<\/code> \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002<\/li>\n<li>\u8bbe\u7f6e HTTP \u5934\uff0c\u8868\u660e\u54cd\u5e94\u7c7b\u578b\u4e3a JSON\uff1a<\/li>\n<\/ol>\n<pre>&lt;?php\n$data = array(\"status\" =&gt; \"success\", \"message\" =&gt; \"Hello world!\");\n\n$json = json_encode($data);\n\nheader('Content-Type: application\/json');\n\necho $json;\n?&gt;<\/pre>\n<p>\u8fd9\u5c06\u5411\u5ba2\u6237\u7aef\u8fd4\u56de\u4ee5\u4e0b JSON \u54cd\u5e94\uff1a<\/p>\n<pre>{\n  \"status\": \"success\",\n  \"message\": \"Hello world!\"\n}<\/pre>\n<p><strong>\u5176\u4ed6\u9009\u9879<\/strong><\/p>\n<p>\u9664\u4e86 <code>json_encode()<\/code> \u51fd\u6570\u5916\uff0c\u8fd8\u6709\u5176\u4ed6\u65b9\u6cd5\u53ef\u4ee5\u8fd4\u56de JSON\uff1a<\/p>\n<ul>\n<li><strong>\u4f7f\u7528 <code>json_decode()<\/code> \u548c <code>json_exit()<\/code> \u51fd\u6570<\/strong>\uff1a\u8fd9\u5141\u8bb8\u4f60\u624b\u52a8\u521b\u5efa JSON \u5b57\u7b26\u4e32\uff0c\u5e76\u4f7f\u7528 <code>json_exit()<\/code> \u51fd\u6570\u76f4\u63a5\u5411\u5ba2\u6237\u7aef\u8f93\u51fa\u3002<\/li>\n<li><strong>\u4f7f\u7528 JSONP \u56de\u8c03<\/strong>\uff1a\u8fd9\u5141\u8bb8\u4f60\u4e3a\u8de8\u57df\u8bf7\u6c42\u8fd4\u56de JSON \u6570\u636e\u3002<\/li>\n<li><strong>\u4f7f\u7528\u7b2c\u4e09\u65b9 JSON \u5e93<\/strong>\uff1a\u4f8b\u5982 Guzzle \u6216 Symfony \u7684 HTTPFoundations \u7ec4\u4ef6\u63d0\u4f9b\u4e86\u65b9\u4fbf\u7684\u65b9\u6cd5\u6765\u5904\u7406 JSON \u54cd\u5e94\u3002<\/li>\n<\/ul>\n<p>\u4eca\u5929\u5173\u4e8e\u300aPHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON\u300b\u7684\u5185\u5bb9\u5c31\u4ecb\u7ecd\u5230\u8fd9\u91cc\u4e86\uff0c\u662f\u4e0d\u662f\u5b66\u8d77\u6765\u4e00\u76ee\u4e86\u7136\uff01\u60f3\u8981\u4e86\u89e3\u66f4\u591a\u5173\u4e8ephp,JSON\u7684\u5185\u5bb9\u8bf7\u5173\u6ce8\u7c73\u4e91\u516c\u4f17\u53f7\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON \u6587\u7ae0\u4e0d\u77e5\u9053\u5927\u5bb6\u662f\u5426\u719f\u6089\uff1f\u4eca\u5929\u6211\u5c06\u7ed9\u5927\u5bb6\u4ecb\u7ecd\u300aPHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON\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 \u901a\u8fc7 json_encode() \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\uff0c\u5b83\u63a5\u53d7\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de JSON \u5b57\u7b26\u4e32\u3002\u4f7f\u7528 json_encode() \u51fd\u6570\u548c\u8bbe\u7f6e\u9002\u5f53\u7684 HTTP \u5934\uff0c\u4f60\u53ef\u4ee5\u8f7b\u677e\u5730\u5c06 JSON \u6570\u636e\u4f5c\u4e3a\u54cd\u5e94\u8fd4\u56de\u7ed9\u5ba2\u6237\u7aef\uff0c\u6216\u4f7f\u7528\u5176\u4ed6\u9009\u9879\uff0c\u4f8b\u5982 json_decode() \u548c json_exit() \u51fd\u6570\uff0c\u4f7f\u7528 JSONP \u56de\u8c03\u6216\u5229\u7528\u7b2c\u4e09\u65b9 JSON \u5e93\u3002 PHP \u51fd\u6570\u5982\u4f55\u8fd4\u56de JSON JSON (JavaScript Object Notation) \u662f\u4e00\u79cd\u6d41\u884c\u7684\u6570\u636e\u4ea4\u6362\u683c\u5f0f\uff0c\u53ef\u4ee5\u5728 PHP \u4e2d\u8f7b\u677e\u4f7f\u7528\u3002\u672c\u6587\u5c06\u4ecb\u7ecd\u5982\u4f55\u4f7f\u7528 PHP \u51fd\u6570\u5c06\u6570\u636e\u8f6c\u6362\u4e3a JSON \u683c\u5f0f\u3002 json_encode() \u51fd\u6570 json_encode() \u51fd\u6570\u5c06\u4e00\u4e2a PHP \u503c\u8f6c\u6362\u4e3a JSON \u5b57\u7b26\u4e32\u3002\u8be5\u51fd\u6570\u63a5\u53d7\u4e00\u4e2a\u53d8\u91cf\u4f5c\u4e3a\u53c2\u6570\u5e76\u8fd4\u56de\u4e00\u4e2a\u5305\u542b JSON \u8868\u793a\u7684\u5b57\u7b26\u4e32\u3002 &lt;?php $array = [&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-47757","post","type-post","status-publish","format-standard","hentry","category-16"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47757","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=47757"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/47757\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=47757"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=47757"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=47757"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}