设置 google-api-php-client 以与自定义 google 应用引擎 API 端点进行通信



我正在尝试在我的项目中配置google-api-php-client库。我已经创建了一个自定义的谷歌应用引擎项目,该项目包含在一个云端点中。该项目称为"set-core",服务称为"vrp API",版本为"v1",方法vrp.vrp.getSolution()

现在在我的 PHP 代码中,我遵循以下示例:https://developers.google.com/api-client-library/php/start/get_started#building-and-calling-a-service

问题是,在这个例子中,没有提到如何连接到谷歌服务之外的任何自定义服务。

我的PHP代码是:

$client = new Google_Client();
$client->setApplicationName("set-core");
$client->setDeveloperKey("AIzaSyByd8cRJNGYC4szFLbr3**************");
$client->isAppEngine(true);
$service = new Google_Service_Appengine_Service($client);
$results = $service->vrp->vrp.vrp.getSolution($stringVehicles, $stringServices, $stringDepot);

不幸的是,在最后一行,PHP 警告我:

注意:尝试获取非对象的属性(我假设它是$service)。

问题是我真的不知道如何设置所有客户端的参数以及使用哪种服务类型。

您将需要创建一个授权的 HTTP 客户端,然后直接使用它请求您的 API 端点。您在上面操作的 AppEngine 服务类不适用于此用例。这样的事情应该有效:

$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$httpClient = $client->authorize();
$response = $httpClient->request('GET', 'https://myapp.appspot.com/vrp/getSolution');

$httpClient类是GuzzleHttpClient的一个实例,但已经添加了您的Google身份验证。请参阅有关向 Guzzle 提出请求的文档。

我希望这有帮助!

最新更新