【发布时间】:2017-01-11 15:14:49
【问题描述】:
我正在编写一个应用程序(使用 PHP),它将学生添加到 Google 课堂。我正在关注以下文档:
https://developers.google.com/classroom/guides/batch
我正在使用批量请求将多个学生添加到 Google 课堂。但是,批处理请求似乎失败了。我的代码如下:
$service = new Google_Service_Classroom($client);
$service->getClient()->setUseBatch(true);
$batch = $service->createBatch();
$courseId = "123456";
$studentEmails = ["user1@domain.com","user2@domain.com"];
foreach($studentEmails as $email) {
$student = new Google_Service_Classroom_Student(['userId' => $email]);
$request = $service->courses_students->create($courseId, $student);
$requestId = $email;
$batch->add($request, $requestId);
}
$results = $batch->execute();
foreach($results as $responseId => $student) {
$studentEmail = substr($responseId, strlen('response-') );
if ($student instanceof Google_Service_Exception) {
$e = $student;
printf("Error adding user '%s' to the course: %s\n", $studentEmail,
$e->getMessage());
} else {
printf("User '%s' was added as a student to the course.\n",
$student->profile->name->fullName, $courseId);
}
}
$service->getClient()->setUseBatch(false);
这段代码的输出是:
Error adding user 'user1@domain.com' to the course: Not Found ...
但是,用户和课程都存在于域中。如果我删除批处理代码并一次发出一个请求,学生会成功添加到课堂,这让我相信我在批处理请求中遗漏了一些东西
【问题讨论】:
标签: php google-api-php-client google-classroom