How to set timeout for google api php client library

拟墨画扇 提交于 2019-12-13 14:17:44

问题


I am using Google's php client library to build an app. Sometimes, Google takes up to 100 seconds to respond to an API request. I'd like to limit the socket timeout to 30 seconds.

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.

Thanks for any help.


回答1:


According to this issue you can pass parameters directly to curl.

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



回答2:


In Google API v2 this can be done through the Guzzle client

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



回答3:


This one worked for me on v2.2.2:

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


来源:https://stackoverflow.com/questions/31660589/how-to-set-timeout-for-google-api-php-client-library

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!