简体   繁体   English

如何为google api php客户端库设置超时

[英]How to set timeout for google api php client library

I am using Google's php client library to build an app. 我正在使用Google的php客户端库来构建应用程序。 Sometimes, Google takes up to 100 seconds to respond to an API request. 有时,Google最多需要100秒才能响应API请求。 I'd like to limit the socket timeout to 30 seconds. 我想将套接字超时限制为30秒。

Anyone know how this is possible? 有人知道这是怎么回事吗? Not seeing any clear examples in the docs and I nothing timeout-related jumped out at me looking at the source. 没有在文档中看到任何明确的例子,我没有看到任何与超时相关的内容。

I did find this example in the docs for the Java client, but I can't seem to find the PHP equivalent. 我确实在Java客户端的文档中找到了这个例子 ,但我似乎无法找到PHP的等价物。

Thanks for any help. 谢谢你的帮助。

In Google API v2 this can be done through the Guzzle client 在Google API v2中,这可以通过Guzzle客户端完成

$http = $googleClient->getHttpClient();
$http->setDefaultOption('connect_timeout', 10);
$http->setDefaultOption('timeout', 10);

According to this issue you can pass parameters directly to curl. 根据这个问题,您可以直接将参数传递给curl。

$client->setClassConfig('Google_IO_Curl', 'options',
    array(
        CURLOPT_CONNECTTIMEOUT => 10,
        CURLOPT_TIMEOUT => 10
    )
);

This one worked for me on v2.2.2: 这个在v2.2.2上为我工作:

$client->setConfig('CURLOPT_CONNECTTIMEOUT', 100); 
$client->setConfig('CURLOPT_TIMEOUT', 1000);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM