如何在Google API PHP客户端版本2.2.2中设置curl超时

时间:2019-03-05 08:43:34

标签: php google-api google-apps google-api-php-client google-admin-sdk

我试图在用于使用目录api批量更新用户的脚本中设置默认的curl超时。在curl连接超时之前,仅添加了少数用户。

我有tried this,但不适用于API v2.2.2

有人可以告诉我如何使用php更新curl超时设置吗?

谢谢。

代码如下:

$groupEmail = "my group email";

require_once realpath(dirname(__FILE__).'/vendor/autoload.php');
$client = new Google_Client();
$client->setAuthConfig('my-config.json');
$client->useApplicationDefaultCredentials();
$client->setScopes(array('https://www.googleapis.com/auth/admin.directory.user.readonly', 
'https://www.googleapis.com/auth/admin.directory.group'));
$client->setSubject('user being impersonated');
$service = new Google_Service_Directory($client); 

try {
    //try something

    for($i=1;$i<=1000;$i++)
    {
        $addThese[] = "something".$i."@gmail.com";
    }   

    //EXECUTE ADDITIONS AND REMOVEALS
    $client->setUseBatch(true);
    $batch = new Google_Http_Batch($client);

    foreach($addThese as $addThis)
    {
        $member = new Google_Service_Directory_Member(array('email' => $addThis,
                                'kind' => 'admin#directory#member',
                                'role' => 'MEMBER',
                                'type' => 'USER'));
        $batch->add($service->members->insert($groupEmail, $member));
    }
    $result = $batch->execute();

} catch (Exception $e) {
    // do something about the error
    //echo "Error: $e";
}

1 个答案:

答案 0 :(得分:1)

尝试一下:

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

请记住,可能的问题可能在于最大执行时间(如果我记得3到5分钟之间的时间)