{"id":29158,"date":"2024-11-25T08:04:57","date_gmt":"2024-11-25T00:04:57","guid":{"rendered":"https:\/\/fwq.ai\/blog\/29158\/"},"modified":"2024-11-25T08:04:57","modified_gmt":"2024-11-25T00:04:57","slug":"wordpress-json-%e5%a4%84%e7%90%86%e7%9b%b8%e5%85%b3%e5%87%bd%e6%95%b0%e5%a4%a7%e5%85%a8","status":"publish","type":"post","link":"https:\/\/fwq.ai\/blog\/29158\/","title":{"rendered":"WordPress JSON \u5904\u7406\u76f8\u5173\u51fd\u6570\u5927\u5168"},"content":{"rendered":"<p>json\u5904\u7406\u662f\u5f00\u53d1\u4e2d\u7684\u7ecf\u5e38\u9700\u8981\u5904\u7406\u7684\u4e00\u9879\u5de5\u4f5c\uff0c\u4e3a\u6b64wordpress \u5b9a\u4e49\u4e86\u4e00\u5806json\u5904\u7406\u7684\u51fd\u6570\uff0c\u4e0b\u9762\u7531\u680f\u76ee\u7ed9\u5927\u5bb6\u505a\u4e00\u4e0b\u7edf\u4e00\u7684\u4ecb\u7ecd\u3002<\/p>\n<p style=\"text-align: center\"><img decoding=\"async\" src=\"https:\/\/img.php.cn\/upload\/article\/000\/000\/020\/5d8444716a806112.jpg\" class=\"aligncenter\" title=\"WordPress JSON \u5904\u7406\u76f8\u5173\u51fd\u6570\u5927\u5168\u63d2\u56fe\" alt=\"WordPress JSON \u5904\u7406\u76f8\u5173\u51fd\u6570\u5927\u5168\u63d2\u56fe\" \/><\/p>\n<p><strong>wp_json_encode<\/strong><\/p>\n<p>\u7f16\u7801\u6210 JSON\uff0c\u5e76\u4e14\u8fdb\u884c\u4e00\u4e9b\u5b8c\u6574\u6027\u68c0\u67e5\u3002<\/p>\n<pre>wp_json_encode(&nbsp;$data,&nbsp;$options&nbsp;=&nbsp;0,&nbsp;$depth&nbsp;=&nbsp;512&nbsp;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u7b80\u5355\u8bf4\u4e0b WordPress \u5f15\u8fdb\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u56e0\uff1a<\/p>\n<p>&nbsp;<\/p>\n<p>\u9996\u5148\u4e0d\u540c PHP \u7248\u672c json_encode \u51fd\u6570\u652f\u6301\u4e0d\u540c\u7684\u53c2\u6570\u3002PHP 5.3 \u4e4b\u524d\u53ea\u652f\u6301 \u4e00\u4e2a\u53c2\u6570 $data\uff0cPHP 5.3 \u5f15\u8fdb\u4e86 $options \u53c2\u6570\uff0cPHP 5.5 \u53c8\u5f15\u8fdb\u4e86 $depth \u53c2\u6570\u3002\u6240\u4ee5 WordPress \u9002\u914d\u4e0d\u540c\u7248\u672c\u7684 PHP\uff0cwp_json_encode \u90fd\u652f\u6301\u4e09\u4e2a\u53c2\u6570\uff0c\u517c\u5bb9\u4e0d\u540c\u7248\u672c\u7684 PHP\u3002<\/p>\n<p>\u5728 json_encode \u4e4b\u524d\uff0cwp_json_encode \u4f7f\u7528\u51fd\u6570 _wp_json_prepare_data \u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u7406\uff0c\u5982\u679c boolean\uff0cinteger\uff0cdouble\uff0cstring\uff0cNULL \u8fd9\u4e9b\u7c7b\u578b\uff0c\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u6570\u7ec4\uff0c\u7ee7\u7eed\u4f7f\u7528 _wp_json_prepare_data \u51fd\u6570\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u6e05\u7406\uff0c\u5982\u679c\u662f\u5bf9\u8c61\uff0c\u5982\u679c\u8be5\u5bf9\u8c61\u7684 class \u5b9e\u73b0\u4e86 JsonSerializable \u63a5\u53e3\uff0c\u5219\u8fd4\u56de $data = $data-&gt;jsonSerialize()\uff0c\u5426\u5219\uff0c\u7ee7\u7eed\u4f7f\u7528 _wp_json_prepare_data \u5bf9\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5c5e\u6027\u8fdb\u884c\u6e05\u7406\u3002<\/p>\n<p>\u7136\u540e\u4f7f\u7528 json_encode \u8fdb\u884c\u7f16\u7801\uff0c\u5982\u679c\u4e0d\u6210\u529f\uff0c\u5219\u4f7f\u7528 _wp_json_sanity_check \u5bf9\u6570\u636e\u8fdb\u884c\u5b8c\u6574\u6027\u5904\u7406\uff0c\u6700\u540e\u518d\u4f7f\u7528 json_encode \u8fdb\u884c\u7f16\u7801\u3002_wp_json_sanity_check \u4e3b\u8981\u4f7f\u7528\u51fd\u6570 _wp_json_convert_string \u5bf9\u6570\u636e\u8fdb\u884c\u6df1\u5ea6 UTF-8 \u68c0\u6d4b\u548c\u8f6c\u6362\u3002<\/p>\n<p>\u6240\u4ee5\u5efa\u8bae\u4f7f\u7528 wp_json_encode \u5bf9\u53d8\u91cf\u8fdb\u884c JSON \u7f16\u7801\uff0c\u5b83\u7684\u53ef\u9760\u6027\u66f4\u9ad8\u3002<\/p>\n<p><strong>wpjam_json_encode<\/strong><\/p>\n<pre>wp_json_encode(&nbsp;$data,&nbsp;$options&nbsp;=&nbsp;JSON_UNESCAPED_UNICODE,&nbsp;$depth&nbsp;=&nbsp;512&nbsp;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>PHP5.4 JSON \u65b0\u589e\u4e86\u4e00\u4e2a\u9009\u9879\uff1aJSON_UNESCAPED_UNICODE\uff0c\u6545\u540d\u601d\u8bae\uff1a\u4e0d\u8981\u7f16\u7801\u6210 Unicode\uff0c\u8ba9\u4e2d\u6587\u66f4\u53ef\u8bfb\u3002<\/p>\n<p>\u6240\u4ee5\u6211\u4eec\u5199\u4e86\u4e00\u4e2a wpjam_json_encode \u51fd\u6570\uff0c\u76f8\u6bd4 wp_json_encode\uff0c\u5c31\u662f\u5c06 $options \u53c2\u6570\u9ed8\u8ba4\u503c\u8bbe\u7f6e\u4e3a JSON_UNESCAPED_UNICODE\uff0c\u8fd9\u6837\u76f4\u63a5\u4f7f\u7528 wpjam_json_encode($data)\uff0c\u4e2d\u6587\u5c31\u4e0d\u4f1a\u88ab\u7f16\u7801\u6210 unicode\uff0c\u66f4\u53ef\u8bfb\u3002<\/p>\n<p>\u53ea\u8981\u4f60\u5b89\u88c5 WPJAM Basic \u63d2\u4ef6\uff0c\u4f60\u7684 WordPress \u5c31\u4f1a\u6709\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<p><strong>wp_send_json<\/strong><\/p>\n<p>\u76f4\u63a5\u53d1\u9001 JSON \u6570\u636e\u3002<\/p>\n<pre>wp_send_json(&nbsp;$response,&nbsp;$status_code&nbsp;=&nbsp;null&nbsp;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u4ed6\u9996\u5148\u8f93\u51fa application\/json \u7684 Content-Type \u5934\uff0c\u5982\u679c $status_code \u4e0d\u4e3a\u7a7a\uff0c\u5219\u8f93\u51fa $status_code \u7684\u72b6\u6001\u7801\u3002<\/p>\n<p>\u7136\u540e\u8c03\u7528 wp_json_encode \u5bf9\u6570\u636e\u8fdb\u884c\u7f16\u7801\u3002<\/p>\n<p><strong>wpjam_send_json<\/strong><\/p>\n<pre>wpjam_send_json(&nbsp;$response,&nbsp;$status_code&nbsp;=&nbsp;null&nbsp;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>\u540c\u6837\u4e3a\u4e86\u80fd\u591f JSON \u7f16\u7801\u4e4b\u540e\uff0c\u4e2d\u6587\u66f4\u53ef\u8bfb\uff0c\u6211\u4eec\u4e5f\u5199\u4e86 wpjam_send_json \u51fd\u6570\uff0c\u5b83\u548c wp_send_json \u51e0\u4e4e\u4e00\u81f4\uff0c\u53ea\u662f\u8c03\u7528\u5bf9\u6570\u636e\u8fdb\u884c\u7f16\u7801\u7684\u51fd\u6570\u662f wpjam_json_encode\u3002<\/p>\n<p>\u53e6\u5916\u5982\u679c\u4f20\u5165\u7684\u6570\u636e\u662f WP_Error \u7684\u5b9e\u4f8b\uff0c\u90a3\u4e48 wpjam_send_json \u76f4\u63a5\u8f93\u51fa errcode \u548c errmsg JSON\u3002\u5982\u679c\u6ca1\u6709\u8bbe\u7f6e errcode\uff0c\u5219 wpjam_send_json \u81ea\u52a8\u52a0\u4e0a errcode=&gt;0<\/p>\n<p>\u5b89\u88c5 WPJAM Basic \u63d2\u4ef6\uff0c\u4f60\u7684 WordPress \u5c31\u4f1a\u6709\u8fd9\u4e2a\u51fd\u6570\u3002<\/p>\n<p><strong>wp_send_json_success \u548c wp_send_json_error<\/strong><\/p>\n<p>WordPress \u8fd8\u63d0\u4f9b wp_send_json_success \u548c wp_send_json_error \u4e24\u4e2a\u51fd\u6570\uff1a<\/p>\n<pre>wp_send_json_success(&nbsp;$data&nbsp;=&nbsp;null,&nbsp;$status_code&nbsp;=&nbsp;null&nbsp;)\nwp_send_json_error(&nbsp;$data&nbsp;=&nbsp;null,&nbsp;$status_code&nbsp;=&nbsp;null&nbsp;)<\/pre>\n<p> \u767b\u5f55\u540e\u590d\u5236 <\/p>\n<p>wp_send_json_success \u5148\u8f93\u51fa success \u4e3a true\uff0c\u7136\u540e\u628a\u6570\u636e $data \u653e\u5230 data \u91cc\u9762\u8f93\u51fa\u3002wp_send_json_error \u5219\u4f1a\u5224\u65ad $data \u662f\u5426\u4e3a WP_Error \u5b9e\u4f8b\uff0c\u5982\u679c\u662f\uff0c\u5219\u8f93\u51fa code \u548c message \u7684\u6570\u7ec4\u3002\u6211\u4eec\u5b9e\u73b0\u7684 wpjam_send_json \u5df2\u7ecf\u80fd\u591f\u81ea\u52a8\u5316\u5904\u7406\u8fd9\u4e9b\u9519\u8bef\u4e86\u3002<\/p>\n<p><strong>wp_is_json_request<\/strong><\/p>\n<p>\u5224\u65ad\u5f53\u524d\u8bf7\u6c42\u662f\u4e0d\u662f JSON \u8bf7\u6c42\uff0c\u6216\u8005\u8fd4\u56de JSON \u7ed3\u679c\uff0c\u8fd9\u4e2a\u51fd\u6570\u6ca1\u6709\u53c2\u6570\uff0c\u76f4\u63a5\u4f7f\u7528\uff1a<\/p>\n<p><strong>wp_is_json_request()<\/strong><\/p>\n<p>\u5b83\u901a\u8fc7\u5224\u65ad $_SERVER[&#8216;HTTP_ACCEPT&#8217;] \u4e2d\u542b\u6709 application\/json\uff0c\u6216\u8005 $_SERVER[&#8216;CONTENT_TYPE&#8217;] \u7b49\u4e8e application\/json\u3002<\/p>\n<p><strong>wp_is_jsonp_request<\/strong><\/p>\n<p>\u5224\u65ad\u5f53\u524d\u8bf7\u6c42\u662f\u4e0d\u662f JSONP \u8bf7\u6c42\uff0c\u6216\u8005\u8fd4\u56de JSONP \u7ed3\u679c\uff0c\u8fd9\u4e2a\u51fd\u6570\u6ca1\u6709\u53c2\u6570\uff0c\u76f4\u63a5\u4f7f\u7528\uff1a<\/p>\n<p><strong>wp_is_jsonp_request()<\/strong><\/p>\n<p>\u5b83\u9996\u5148\u5224\u65ad $_GET[&#8216;_jsonp&#8217;] \uff0c\u662f\u5426\u5b58\u5728\uff0c\u7136\u540e\u901a\u8fc7\u51fd\u6570 wp_check_jsonp_callback \u5224\u65ad\u5b83\u7684\u503c\u662f\u5426\u5408\u6cd5\u3002<\/p>\n<p><strong>wp_check_jsonp_callback<\/strong><\/p>\n<p>\u5224\u65ad JSONP callback \u662f\u4e0d\u662f\u4e00\u4e2a\u4e00\u4e2a\u5408\u6cd5\u7684 JavaScript callback \u51fd\u6570\uff1a<\/p>\n<p><strong>wp_check_jsonp_callback( $callback )<\/strong><\/p>\n<p>\u5408\u6cd5\u7684 JavaScript callback \u51fd\u6570\u53ea\u80fd\u6570\u5b57\u52a0\u5b57\u7b26\uff0c\u4ee5\u53ca\u82f1\u6587\u70b9\u53f7\u3002<\/p>\n<p>\u4ee5\u4e0a\u5c31\u662fWordPress JSON \u5904\u7406\u76f8\u5173\u51fd\u6570\u5927\u5168\u7684\u8be6\u7ec6\u5185\u5bb9\uff0c\u66f4\u591a\u8bf7\u5173\u6ce8\u7c73\u4e91\u5176\u5b83\u76f8\u5173\u6587\u7ae0\uff01<\/p>\n","protected":false},"excerpt":{"rendered":"<p>json\u5904\u7406\u662f\u5f00\u53d1\u4e2d\u7684\u7ecf\u5e38\u9700\u8981\u5904\u7406\u7684\u4e00\u9879\u5de5\u4f5c\uff0c\u4e3a\u6b64wordpress \u5b9a\u4e49\u4e86\u4e00\u5806json\u5904\u7406\u7684\u51fd\u6570\uff0c\u4e0b\u9762\u7531\u680f\u76ee\u7ed9\u5927\u5bb6\u505a\u4e00\u4e0b\u7edf\u4e00\u7684\u4ecb\u7ecd\u3002 wp_json_encode \u7f16\u7801\u6210 JSON\uff0c\u5e76\u4e14\u8fdb\u884c\u4e00\u4e9b\u5b8c\u6574\u6027\u68c0\u67e5\u3002 wp_json_encode(&nbsp;$data,&nbsp;$options&nbsp;=&nbsp;0,&nbsp;$depth&nbsp;=&nbsp;512&nbsp;) \u767b\u5f55\u540e\u590d\u5236 \u7b80\u5355\u8bf4\u4e0b WordPress \u5f15\u8fdb\u8fd9\u4e2a\u51fd\u6570\u7684\u539f\u56e0\uff1a &nbsp; \u9996\u5148\u4e0d\u540c PHP \u7248\u672c json_encode \u51fd\u6570\u652f\u6301\u4e0d\u540c\u7684\u53c2\u6570\u3002PHP 5.3 \u4e4b\u524d\u53ea\u652f\u6301 \u4e00\u4e2a\u53c2\u6570 $data\uff0cPHP 5.3 \u5f15\u8fdb\u4e86 $options \u53c2\u6570\uff0cPHP 5.5 \u53c8\u5f15\u8fdb\u4e86 $depth \u53c2\u6570\u3002\u6240\u4ee5 WordPress \u9002\u914d\u4e0d\u540c\u7248\u672c\u7684 PHP\uff0cwp_json_encode \u90fd\u652f\u6301\u4e09\u4e2a\u53c2\u6570\uff0c\u517c\u5bb9\u4e0d\u540c\u7248\u672c\u7684 PHP\u3002 \u5728 json_encode \u4e4b\u524d\uff0cwp_json_encode \u4f7f\u7528\u51fd\u6570 _wp_json_prepare_data \u5bf9\u6570\u636e\u8fdb\u884c\u6e05\u7406\uff0c\u5982\u679c boolean\uff0cinteger\uff0cdouble\uff0cstring\uff0cNULL \u8fd9\u4e9b\u7c7b\u578b\uff0c\u76f4\u63a5\u8fd4\u56de\uff0c\u5982\u679c\u6570\u7ec4\uff0c\u7ee7\u7eed\u4f7f\u7528 _wp_json_prepare_data \u51fd\u6570\u5bf9\u6570\u7ec4\u4e2d\u7684\u6bcf\u4e2a\u5143\u7d20\u8fdb\u884c\u6e05\u7406\uff0c\u5982\u679c\u662f\u5bf9\u8c61\uff0c\u5982\u679c\u8be5\u5bf9\u8c61\u7684 class \u5b9e\u73b0\u4e86 JsonSerializable \u63a5\u53e3\uff0c\u5219\u8fd4\u56de $data = $data-&gt;jsonSerialize()\uff0c\u5426\u5219\uff0c\u7ee7\u7eed\u4f7f\u7528 _wp_json_prepare_data \u5bf9\u5bf9\u8c61\u4e2d\u7684\u6bcf\u4e2a\u5c5e\u6027\u8fdb\u884c\u6e05\u7406\u3002 \u7136\u540e\u4f7f\u7528 json_encode \u8fdb\u884c\u7f16\u7801\uff0c\u5982\u679c\u4e0d\u6210\u529f\uff0c\u5219\u4f7f\u7528 [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-29158","post","type-post","status-publish","format-standard","hentry","category-cms"],"_links":{"self":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29158","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=29158"}],"version-history":[{"count":0,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/posts\/29158\/revisions"}],"wp:attachment":[{"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/media?parent=29158"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/categories?post=29158"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/fwq.ai\/blog\/wp-json\/wp\/v2\/tags?post=29158"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}