如何在 PHP 中安装 Google 索引 API

How to install Google Index API in PHP

我正在尝试使用 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 API 的最新 Google API 客户端库(版本 2.6.0),然后我将其上传进入我的 /indexapi/ 文件夹。

我遇到的唯一错误是空白页错误 500,我认为 autoload.php 有问题(请不要推荐 composer,因为我使用的是 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;

谢谢,

下载 v2.5.0 的发布资产。看起来 v2.6.0 的自动化中断了。我们将在本周解决这个问题。需要vendor/autoload.php,不是src下的文件

编辑:发布资产现在可用于 v2.6.0。