【发布时间】:2020-07-19 11:39:43
【问题描述】:
我正在尝试使用 PHP 实现 Google 索引 API,但出现空白页错误 500。
我相信我已经完成了此处提到的索引 API 的所有先决条件:https://developers.google.com/search/apis/indexing-api/v3/prereqs#php
然后我把下面的代码上传到这个文件夹/indexapi/index.php
require_once 'indexapi/google-api-php-client/src/Google/autoload.php';
$client = new Google_Client();
// service_account_file.json is the private key that you created for your service account.
$client->setAuthConfig('indexapi/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": "https://myurl2020.com",
"type": "URL_UPDATED"
}';
$response = $httpClient->post($endpoint, [ 'body' => $content ]);
$status_code = $response->getStatusCode();
print_r($response);
echo "\n";
echo "$status_code";
在先决条件期间,我还下载了我保存在这里的 JSON 私钥 /indexapi/service_account_file.json
然后我从这里https://github.com/googleapis/google-api-php-client 下载了最新的适用于 PHP 的 Google API 客户端库(版本 2.6.0),然后我将其上传到我的 /indexapi/ 文件夹中。
我唯一的错误是空白页错误 500,我认为 autoload.php 存在问题(请不要建议作曲家,因为我使用的是 Chromebook,我还没有找到在 Chromebook 上使用 composer 的方法)。
这是我的 autoload.php 文件:
/**
* THIS FILE IS FOR BACKWARDS COMPATIBLITY ONLY
*
* If you were not already including this file in your project, please ignore it
*/
$file = __DIR__ . 'indexapi/google-api-php-client/src/Google/autoload.php';
if (!file_exists($file)) {
$exception = 'This library must be installed via composer or by downloading the full package.';
$exception .= ' See the instructions at https://github.com/google/google-api-php-client#installation.';
throw new Exception($exception);
}
$error = 'google-api-php-client\'s autoloader was moved to vendor/autoload.php in 2.0.0. This ';
$error .= 'redirect will be removed in 2.1. Please adjust your code to use the new location.';
trigger_error($error, E_USER_DEPRECATED);
require_once $file;
谢谢,
【问题讨论】:
标签: php google-api google-oauth google-api-php-client google-indexing-api