php - 谷歌索引 API
问题描述
我有一个工作门户网站(Wordpress + PHP),我想为我的网站使用 Google Indexing API。我对 GoogleAPI 没有任何经验,所以我只是阅读了他们的指导。根据指南,要使用索引 API,它有 3 个步骤:
- 通过启用 Indexing API、创建新的服务帐户并在 Search Console 中验证所有权来完成先决条件。
- 获取访问令牌以验证您的 API 调用。
- 发送请求以通知 Google 有新的、更新的或删除的网页。
我完成了第 1 步,但第 2 步和第 3 步真的让我很困惑。似乎我需要通过编码获取 OAuth 令牌,但我应该将这些代码放在哪里?对于使用 API,他们向我展示了这个示例:
POST https://indexing.googleapis.com/v3/urlNotifications:publish
{
"url": "https://careers.google.com/jobs/google/technical-writer",
"type": "URL_UPDATED"
}
同样,我不确定我应该将这些块代码放在哪里使用 API。任何人都可以知道这一点,可以逐步解释如何为我做这件事吗?最后一个问题:因为我的网站每天大约有 10 到 15 个新职位发布。每当有人发布新工作时,我可以以某种方式将此索引 API 自动发送请求设置到 Google 吗?问候,
解决方案
您应该在请求中将其作为承载身份验证标头传递。
授权:承载
您也可以将它作为请求字符串的一部分传递,但我不记得这是否适用于 post 调用。
POST https://indexing.googleapis.com/v3/urlNotifications:publish?Access_token=XXXX
{
"url": "https://careers.google.com/jobs/google/technical-writer",
"type": "URL_UPDATED"
}
如果您使用的是 php,您应该考虑使用Google php 客户端库,它将为您处理大部分内容。这是他们在此处的示例中推荐的
require_once 'google-api-php-client/vendor/autoload.php';
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('service_account_file.json');
$client->addScope('https://www.googleapis.com/auth/indexing');
// Get a Guzzle HTTP Client
$httpClient = $client->authorize();
$endpoint = 'https://indexing.googleapis.com/v3/urlNotifications:publish';
// Define contents here. The structure of the content is described in the next step.
$content = "{
\"url\": \"http://example.com/jobs/42\",
\"type\": \"URL_UPDATED"
}";
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
确保您已正确设置服务帐户创建服务帐户
推荐阅读
- react-native - 如何在反应原生的webview中嵌入来自json数据的视频?
- javascript - 为什么这返回未定义而不是值?
- java - 互操作 Java 和 Powershell
- java - java.lang.IllegalArgumentException:迭代变量不能为空
- python - 如何使用 django rest 框架在后端突出显示文章的一部分
- c# - Unity Emission Lighting 不影响导入的对象
- algorithm - 存储字典(单词)以优化搜索时间的良好数据结构是什么?
- c++ - 如何从 C++ 中的文本文件正文中获取特定的单词和行
- android - 当用户单击特定位置时,我想在地图上标记位置
- node.js - Express 会话属性未在架构中设置