Php 将Google Http批处理与Google教室Api结合使用
我正在编写一个应用程序(使用PHP),将学生添加到谷歌教室。我正在以下网址查阅文件: 我使用批处理请求将多个学生添加到Google教室。但是,批处理请求似乎失败。我的代码如下:Php 将Google Http批处理与Google教室Api结合使用,php,google-api-php-client,google-classroom,Php,Google Api Php Client,Google Classroom,我正在编写一个应用程序(使用PHP),将学生添加到谷歌教室。我正在以下网址查阅文件: 我使用批处理请求将多个学生添加到Google教室。但是,批处理请求似乎失败。我的代码如下: $service = new Google_Service_Classroom($client); $service->getClient()->setUseBatch(true); $batch = $service->createBatch(); $courseId = "123456";
$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 ...
但是,用户和课程都存在于域中。如果我删除批处理代码并一次提出一个请求,学生将成功添加到教室,这让我相信我在批处理请求中遗漏了一些东西,您可能需要首先检查并确保您完成了给定页面其余部分中描述的步骤,以便能够向课堂API发出请求,如批处理请求中所述 成功安装并设置库之后,您可以尝试代码示例,该示例演示如何使用GoogleAPI客户端库发出批处理请求
$courseId = '123456';
$studentEmails = array('alice@example.edu', 'bob@example.edu');
$service->getClient()->setUseBatch(true);
$batch = $service->createBatch();
foreach($studentEmails as $studentEmail) {
$student = new Google_Service_Classroom_Student(array(
'userId' => $studentEmail
));
$request = $service->courses_students->create($courseId, $student);
$requestId = $studentEmail;
$batch->add($request, $requestId);
}
$results = $batch->execute();
foreach($results as $responseId => $student) {
$studentEmail = substr($responseId, strlen('response-') + 1);
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);
您可能希望首先检查并确保完成给定页面其余部分中描述的步骤,以便能够向课堂API发出请求,如批处理请求中所述 成功安装并设置库之后,您可以尝试代码示例,该示例演示如何使用GoogleAPI客户端库发出批处理请求
$courseId = '123456';
$studentEmails = array('alice@example.edu', 'bob@example.edu');
$service->getClient()->setUseBatch(true);
$batch = $service->createBatch();
foreach($studentEmails as $studentEmail) {
$student = new Google_Service_Classroom_Student(array(
'userId' => $studentEmail
));
$request = $service->courses_students->create($courseId, $student);
$requestId = $studentEmail;
$batch->add($request, $requestId);
}
$results = $batch->execute();
foreach($results as $responseId => $student) {
$studentEmail = substr($responseId, strlen('response-') + 1);
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);
谢谢你的帖子,我已经阅读了quickstart文档,并成功地在其他google服务上使用了批处理请求。在为学生注册教室时,这似乎不是在玩游戏。谢谢你的帖子,我已经阅读了quickstart文档,并成功地在其他谷歌服务上使用了批处理请求。把学生招收到教室里似乎不是在玩球