{"id":56195,"date":"2024-10-10T09:33:19","date_gmt":"2024-10-10T01:33:19","guid":{"rendered":"https:\/\/server.hk\/cnblog\/56195\/"},"modified":"2024-10-10T09:33:19","modified_gmt":"2024-10-10T01:33:19","slug":"wordpress-%e6%95%99%e7%a8%8b%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%b3%e8%ab%8b%e5%8f%8a%e9%85%8d%e7%bd%ae-google-index-api-%e5%af%a6%e7%8f%be%e8%b0%b7%e6%ad%8c%e5%bf%ab%e9%80%9f%e6%94%b6%e9%8c%84","status":"publish","type":"post","link":"https:\/\/server.hk\/cnblog\/56195\/","title":{"rendered":"WordPress \u6559\u7a0b\uff1a\u5982\u4f55\u7533\u8acb\u53ca\u914d\u7f6e Google Index API \u5be6\u73fe\u8c37\u6b4c\u5feb\u901f\u6536\u9304"},"content":{"rendered":"<h1 id=\"wordpress-%e6%95%99%e7%a8%8b%ef%bc%9a%e5%a6%82%e4%bd%95%e7%94%b3%e8%ab%8b%e5%8f%8a%e9%85%8d%e7%bd%ae-google-index-api-%e5%af%a6%e7%8f%be%e8%b0%b7%e6%ad%8c%e5%bf%ab%e9%80%9f%e6%94%b6%e9%8c%84-XbRsepStID\">WordPress \u6559\u7a0b\uff1a\u5982\u4f55\u7533\u8acb\u53ca\u914d\u7f6e Google Index API \u5be6\u73fe\u8c37\u6b4c\u5feb\u901f\u6536\u9304<\/h1>\n<p>\u5728\u7576\u4eca\u7684\u6578\u4f4d\u6642\u4ee3\uff0c\u7db2\u7ad9\u7684\u53ef\u898b\u6027\u5c0d\u65bc\u5438\u5f15\u6d41\u91cf\u548c\u589e\u5f37\u54c1\u724c\u5f71\u97ff\u529b\u81f3\u95dc\u91cd\u8981\u3002\u5c0d\u65bc\u4f7f\u7528 WordPress \u7684\u7db2\u7ad9\u4f86\u8aaa\uff0c\u5feb\u901f\u88ab\u8c37\u6b4c\u6536\u9304\u662f\u63d0\u5347\u7db2\u7ad9\u6d41\u91cf\u7684\u95dc\u9375\u4e4b\u4e00\u3002Google Index API \u662f\u4e00\u500b\u5f37\u5927\u7684\u5de5\u5177\uff0c\u53ef\u4ee5\u5e6b\u52a9\u7db2\u7ad9\u6240\u6709\u8005\u66f4\u5feb\u5730\u5c07\u5176\u5167\u5bb9\u63d0\u4ea4\u7d66\u8c37\u6b4c\u3002\u672c\u6587\u5c07\u8a73\u7d30\u4ecb\u7d39\u5982\u4f55\u7533\u8acb\u53ca\u914d\u7f6e Google Index API\uff0c\u4ee5\u5be6\u73fe\u8c37\u6b4c\u7684\u5feb\u901f\u6536\u9304\u3002<\/p>\n<h2 id=\"%e4%bb%80%e9%ba%bc%e6%98%af-google-index-api%ef%bc%9f-XbRsepStID\">\u4ec0\u9ebc\u662f Google Index API\uff1f<\/h2>\n<p>Google Index API \u662f\u4e00\u7a2e\u5141\u8a31\u7db2\u7ad9\u6240\u6709\u8005\u76f4\u63a5\u5411\u8c37\u6b4c\u63d0\u4ea4\u5176\u5167\u5bb9\u7684\u5de5\u5177\u3002\u9019\u610f\u5473\u8457\u7576\u4f60\u767c\u5e03\u65b0\u5167\u5bb9\u6216\u66f4\u65b0\u73fe\u6709\u5167\u5bb9\u6642\uff0c\u53ef\u4ee5\u7acb\u5373\u901a\u77e5\u8c37\u6b4c\uff0c\u800c\u4e0d\u5fc5\u7b49\u5f85\u5176\u81ea\u52d5\u722c\u53d6\u3002\u9019\u5c0d\u65bc\u9700\u8981\u5feb\u901f\u66f4\u65b0\u7684\u7db2\u7ad9\uff08\u5982\u65b0\u805e\u7db2\u7ad9\u6216\u96fb\u5b50\u5546\u52d9\u7db2\u7ad9\uff09\u7279\u5225\u6709\u7528\u3002<\/p>\n<h2 id=\"%e7%94%b3%e8%ab%8b-google-index-api-%e7%9a%84%e6%ad%a5%e9%a9%9f-XbRsepStID\">\u7533\u8acb Google Index API \u7684\u6b65\u9a5f<\/h2>\n<h3 id=\"1-%e5%89%b5%e5%bb%ba-google-cloud-platform-%e9%a0%85%e7%9b%ae-XbRsepStID\">1. \u5275\u5efa Google Cloud Platform \u9805\u76ee<\/h3>\n<ul>\n<li>\u8a2a\u554f <a href=\"https:\/\/console.cloud.google.com\/\" rel=\"nofollow noopener\" target=\"_blank\">Google Cloud Console<\/a>\u3002<\/li>\n<li>\u767b\u9304\u4f60\u7684 Google \u5e33\u6236\uff0c\u7136\u5f8c\u9ede\u64ca\u300c\u9078\u64c7\u9805\u76ee\u300d\u4e0b\u62c9\u83dc\u55ae\uff0c\u9078\u64c7\u300c\u65b0\u5efa\u9805\u76ee\u300d\u3002<\/li>\n<li>\u70ba\u4f60\u7684\u9805\u76ee\u547d\u540d\uff0c\u7136\u5f8c\u9ede\u64ca\u300c\u5275\u5efa\u300d\u3002<\/li>\n<\/ul>\n<h3 id=\"2-%e5%95%9f%e7%94%a8-indexing-api-XbRsepStID\">2. \u555f\u7528 Indexing API<\/h3>\n<ul>\n<li>\u5728 Google Cloud Console \u4e2d\uff0c\u9078\u64c7\u525b\u525b\u5275\u5efa\u7684\u9805\u76ee\u3002<\/li>\n<li>\u5c0e\u822a\u5230\u300cAPI \u548c\u670d\u52d9\u300d&gt;\u300c\u5eab\u300d\uff0c\u7136\u5f8c\u641c\u7d22\u300cIndexing API\u300d\u3002<\/li>\n<li>\u9ede\u64ca\u300c\u555f\u7528\u300d\u4ee5\u555f\u7528\u8a72 API\u3002<\/li>\n<\/ul>\n<h3 id=\"3-%e5%89%b5%e5%bb%ba%e6%9c%8d%e5%8b%99%e5%b8%b3%e6%88%b6-XbRsepStID\">3. \u5275\u5efa\u670d\u52d9\u5e33\u6236<\/h3>\n<ul>\n<li>\u5728 Google Cloud Console \u4e2d\uff0c\u5c0e\u822a\u5230\u300cAPI \u548c\u670d\u52d9\u300d&gt;\u300c\u6191\u8b49\u300d\u3002<\/li>\n<li>\u9ede\u64ca\u300c\u5275\u5efa\u6191\u8b49\u300d\uff0c\u7136\u5f8c\u9078\u64c7\u300c\u670d\u52d9\u5e33\u6236\u300d\u3002<\/li>\n<li>\u70ba\u670d\u52d9\u5e33\u6236\u547d\u540d\uff0c\u4e26\u9078\u64c7\u89d2\u8272\uff08\u5982\u300c\u7de8\u8f2f\u8005\u300d\uff09\u3002<\/li>\n<li>\u5275\u5efa\u5f8c\uff0c\u4e0b\u8f09 JSON \u683c\u5f0f\u7684\u5bc6\u9470\u6587\u4ef6\uff0c\u9019\u5c07\u7528\u65bc\u8eab\u4efd\u9a57\u8b49\u3002<\/li>\n<\/ul>\n<h2 id=\"%e9%85%8d%e7%bd%ae-wordpress-%e4%bd%bf%e7%94%a8-google-index-api-XbRsepStID\">\u914d\u7f6e WordPress \u4f7f\u7528 Google Index API<\/h2>\n<h3 id=\"1-%e5%ae%89%e8%a3%9d%e5%bf%85%e8%a6%81%e7%9a%84%e6%8f%92%e4%bb%b6-XbRsepStID\">1. \u5b89\u88dd\u5fc5\u8981\u7684\u63d2\u4ef6<\/h3>\n<p>\u70ba\u4e86\u5728 WordPress \u4e2d\u4f7f\u7528 Google Index API\uff0c\u4f60\u9700\u8981\u5b89\u88dd\u4e00\u500b\u53ef\u4ee5\u8655\u7406 API \u8acb\u6c42\u7684\u63d2\u4ef6\u3002\u53ef\u4ee5\u8003\u616e\u4f7f\u7528 <a href=\"https:\/\/wordpress.org\/plugins\/wp-rest-api\/\" target=\"_blank\" rel=\"nofollow noopener\">WP REST API<\/a> \u63d2\u4ef6\u3002<\/p>\n<h3 id=\"2-%e7%b7%a8%e5%af%ab%e4%bb%a3%e7%a2%bc%e4%bb%a5%e6%8f%90%e4%ba%a4-url-XbRsepStID\">2. \u7de8\u5beb\u4ee3\u78bc\u4ee5\u63d0\u4ea4 URL<\/h3>\n<p>\u5728\u4f60\u7684\u4e3b\u984c\u7684 functions.php \u6587\u4ef6\u4e2d\uff0c\u6dfb\u52a0\u4ee5\u4e0b\u4ee3\u78bc\u4f86\u63d0\u4ea4 URL\uff1a<\/p>\n<pre><code>\nfunction submit_to_google_index_api($url) {\n    $service_account_email = 'YOUR_SERVICE_ACCOUNT_EMAIL';\n    $key_file_location = 'PATH_TO_YOUR_JSON_KEY_FILE';\n    \n    $client = new Google_Client();\n    $client-&gt;setAuthConfig($key_file_location);\n    $client-&gt;addScope(Google_Service_Indexing::INDEXING);\n    \n    $service = new Google_Service_Indexing($client);\n    \n    $body = new Google_Service_Indexing_UrlNotification();\n    $body-&gt;setUrl($url);\n    $body-&gt;setType('URL_UPDATED');\n    \n    $service-&gt;urlNotifications-&gt;publish($body);\n}\n<\/code><\/pre>\n<p>\u78ba\u4fdd\u5c07 `YOUR_SERVICE_ACCOUNT_EMAIL` \u548c `PATH_TO_YOUR_JSON_KEY_FILE` \u66ff\u63db\u70ba\u4f60\u7684\u670d\u52d9\u5e33\u6236\u96fb\u5b50\u90f5\u4ef6\u548c JSON \u5bc6\u9470\u6587\u4ef6\u7684\u8def\u5f91\u3002<\/p>\n<h3 id=\"3-%e8%87%aa%e5%8b%95%e6%8f%90%e4%ba%a4%e6%96%b0%e6%96%87%e7%ab%a0-XbRsepStID\">3. \u81ea\u52d5\u63d0\u4ea4\u65b0\u6587\u7ae0<\/h3>\n<p>\u4f60\u53ef\u4ee5\u4f7f\u7528 WordPress \u7684\u9264\u5b50\u4f86\u81ea\u52d5\u63d0\u4ea4\u65b0\u6587\u7ae0\u7684 URL\u3002\u4ee5\u4e0b\u662f\u5982\u4f55\u5be6\u73fe\u7684\uff1a<\/p>\n<pre><code>\nadd_action('publish_post', 'submit_post_to_google_index');\n\nfunction submit_post_to_google_index($ID) {\n    $post_url = get_permalink($ID);\n    submit_to_google_index_api($post_url);\n}\n<\/code><\/pre>\n<h2 id=\"%e7%b8%bd%e7%b5%90-XbRsepStID\">\u7e3d\u7d50<\/h2>\n<p>\u901a\u904e\u4ee5\u4e0a\u6b65\u9a5f\uff0c\u4f60\u53ef\u4ee5\u6210\u529f\u7533\u8acb\u4e26\u914d\u7f6e Google Index API\uff0c\u5f9e\u800c\u5be6\u73fe\u8c37\u6b4c\u7684\u5feb\u901f\u6536\u9304\u3002\u9019\u4e0d\u50c5\u80fd\u63d0\u9ad8\u4f60\u7db2\u7ad9\u7684\u53ef\u898b\u6027\uff0c\u9084\u80fd\u5e6b\u52a9\u4f60\u5728\u7af6\u722d\u6fc0\u70c8\u7684\u5e02\u5834\u4e2d\u812b\u7a4e\u800c\u51fa\u3002\u5982\u679c\u4f60\u6b63\u5728\u5c0b\u627e\u7a69\u5b9a\u7684 <a href=\"https:\/\/server.hk\">\u9999\u6e2f VPS<\/a> \u89e3\u6c7a\u65b9\u6848\u4f86\u652f\u6301\u4f60\u7684 WordPress \u7db2\u7ad9\uff0cServer.HK \u63d0\u4f9b\u591a\u7a2e\u9078\u64c7\uff0c\u6eff\u8db3\u4e0d\u540c\u9700\u6c42\u3002\u7121\u8ad6\u662f\u5c0f\u578b\u535a\u5ba2\u9084\u662f\u5927\u578b\u5546\u696d\u7db2\u7ad9\uff0c\u6211\u5011\u7684 <a href=\"https:\/\/server.hk\">\u4e91\u670d\u52a1\u5668<\/a> \u90fd\u80fd\u70ba\u4f60\u63d0\u4f9b\u5f37\u5927\u7684\u652f\u6301\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u5b78\u7fd2\u5982\u4f55\u7533\u8acb\u53ca\u914d\u7f6e Google Index API\uff0c\u5be6\u73fe\u8c37\u6b4c\u5feb\u901f\u6536\u9304\uff0c\u63d0\u5347 WordPress \u7db2\u7ad9\u7684\u641c\u5c0b\u5f15\u64ce\u53ef\u898b\u6027\u3002<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[4925],"tags":[],"class_list":["post-56195","post","type-post","status-publish","format-standard","hentry","category-4925"],"_links":{"self":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/56195","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/comments?post=56195"}],"version-history":[{"count":1,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/56195\/revisions"}],"predecessor-version":[{"id":56196,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/posts\/56195\/revisions\/56196"}],"wp:attachment":[{"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/media?parent=56195"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/categories?post=56195"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/server.hk\/cnblog\/wp-json\/wp\/v2\/tags?post=56195"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}