【问题标题】:How to install Google Index API in PHP如何在 PHP 中安装 Google Index API
【发布时间】: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


    【解决方案1】:

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

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

    【讨论】:

    • 感谢您的回复。有效。与v2.6.0有很大区别吗?因为我刚刚上传了 v2.5.0。这需要很长时间。
    • 您可以在此处找到更改列表:github.com/googleapis/google-api-php-client/releases/tag/v2.6.0。客户端包装器本身也有更新,但一般来说,相邻版本之间的差异不太可能很大。
    猜你喜欢
    • 1970-01-01
    • 2016-11-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-05-12
    • 1970-01-01
    • 2013-03-21
    相关资源
    最近更新 更多