问题
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