简体   繁体   中英

How to set timeout for google api php client library

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.

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

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

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

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

This one worked for me on v2.2.2:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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