Google 课堂 - 创建课程错误 PHP
Google Classroom - Create Course Error PHP
我正在尝试使用 Google_Service_Classroom API 创建课程,使用 PHP,但我在调用中遇到错误。
<?php
require_once __DIR__.'/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\laragon\www\classroom.json');
$user_admin= 'test00001@gmail.com';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($user_admin);
$client->setScopes(array(
'https://www.googleapis.com/auth/classroom.courses',
'https://www.googleapis.com/auth/classroom.courses.readonly',
'https://www.googleapis.com/auth/classroom.rosters',
'https://www.googleapis.com/auth/classroom.rosters.readonly',
'https://www.googleapis.com/auth/classroom.profile.emails',
'https://www.googleapis.com/auth/classroom.profile.photos'
));
$service = new Google_Service_Classroom($client);
$postBody = array(
"id" => "1",
"name" => "Course 01",
"section" => "15/19",
"descriptionHeading" => "Course 01 test 001",
"description" => "Course 01 test",
"room" => "03/12",
"creationTime" => "2020-12-12T11:48:50.951Z",
"enrollmentCode" => "yzdeeee",
"courseState" => "ACTIVE",
"alternateLink" => "https://classroom.google.com/c/coursetest01"
);
$optParams = array();
$results = $service->courses->create($postBody, $optParams);
echo '<pre>', print_r($results, true);
exit;
我的return:
Fatal error: Uncaught TypeError: Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given, called in C:\laragon\www\teste-03.php on line 41 and defined in C:\laragon\www\vendor\google\apiclient-services\src\Google\Service\Classroom\Resource\Courses.php:47 Stack trace: #0 C:\laragon\www\teste-03.php(41): Google_Service_Classroom_Resource_Courses->create(Array, Array) #1 {main} thrown in C:\laragon\www\vendor\google\apiclient-services\src\Google\Service\Classroom\Resource\Courses.php on line 47
我正在查看文档,但我不知道在哪里可以解决这个问题。
错误消息不言自明。它说 Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given
发生这种情况是因为 $postBody
是一个数组。
如函数文档中所示create
create( Google_Service_Classroom_Course $postBody, array $optParams = array() )
此函数要求第一个参数是 Google_Service_Classroom_Course
对象而不是数组。所以你需要先创建一个这个类型的对象,然后把这个对象作为参数传递。
所以最终结果应该是这样的:
$course = new Google_Service_Classroom_Course(array(
"id" => "1",
"name" => "Course 01",
"section" => "15/19",
"descriptionHeading" => "Course 01 test 001",
"description" => "Course 01 test",
"room" => "03/12",
"creationTime" => "2020-12-12T11:48:50.951Z",
"enrollmentCode" => "yzdeeee",
"courseState" => "ACTIVE",
"alternateLink" => "https://classroom.google.com/c/coursetest01"
));
$course = $service->courses->create($course);
注意:由于第二个参数的默认值为空数组 (array $optParams = array()
),因此无需显式包含它。上面的最后一行与:
完全相同
$optParams = array();
$course = $service->courses->create($course, $optParams);
还要记住,由于 $course
是一个对象,因此不建议像处理数组那样打印它。您可以改为打印对象的特定信息,例如:
printf("Course created: %s (%s)\n", $course->name, $course->id);
我正在尝试使用 Google_Service_Classroom API 创建课程,使用 PHP,但我在调用中遇到错误。
<?php
require_once __DIR__.'/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:\laragon\www\classroom.json');
$user_admin= 'test00001@gmail.com';
$client = new Google_Client();
$client->useApplicationDefaultCredentials();
$client->setSubject($user_admin);
$client->setScopes(array(
'https://www.googleapis.com/auth/classroom.courses',
'https://www.googleapis.com/auth/classroom.courses.readonly',
'https://www.googleapis.com/auth/classroom.rosters',
'https://www.googleapis.com/auth/classroom.rosters.readonly',
'https://www.googleapis.com/auth/classroom.profile.emails',
'https://www.googleapis.com/auth/classroom.profile.photos'
));
$service = new Google_Service_Classroom($client);
$postBody = array(
"id" => "1",
"name" => "Course 01",
"section" => "15/19",
"descriptionHeading" => "Course 01 test 001",
"description" => "Course 01 test",
"room" => "03/12",
"creationTime" => "2020-12-12T11:48:50.951Z",
"enrollmentCode" => "yzdeeee",
"courseState" => "ACTIVE",
"alternateLink" => "https://classroom.google.com/c/coursetest01"
);
$optParams = array();
$results = $service->courses->create($postBody, $optParams);
echo '<pre>', print_r($results, true);
exit;
我的return:
Fatal error: Uncaught TypeError: Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given, called in C:\laragon\www\teste-03.php on line 41 and defined in C:\laragon\www\vendor\google\apiclient-services\src\Google\Service\Classroom\Resource\Courses.php:47 Stack trace: #0 C:\laragon\www\teste-03.php(41): Google_Service_Classroom_Resource_Courses->create(Array, Array) #1 {main} thrown in C:\laragon\www\vendor\google\apiclient-services\src\Google\Service\Classroom\Resource\Courses.php on line 47
我正在查看文档,但我不知道在哪里可以解决这个问题。
错误消息不言自明。它说 Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given
发生这种情况是因为 $postBody
是一个数组。
如函数文档中所示create
create( Google_Service_Classroom_Course $postBody, array $optParams = array() )
此函数要求第一个参数是 Google_Service_Classroom_Course
对象而不是数组。所以你需要先创建一个这个类型的对象,然后把这个对象作为参数传递。
所以最终结果应该是这样的:
$course = new Google_Service_Classroom_Course(array(
"id" => "1",
"name" => "Course 01",
"section" => "15/19",
"descriptionHeading" => "Course 01 test 001",
"description" => "Course 01 test",
"room" => "03/12",
"creationTime" => "2020-12-12T11:48:50.951Z",
"enrollmentCode" => "yzdeeee",
"courseState" => "ACTIVE",
"alternateLink" => "https://classroom.google.com/c/coursetest01"
));
$course = $service->courses->create($course);
注意:由于第二个参数的默认值为空数组 (array $optParams = array()
),因此无需显式包含它。上面的最后一行与:
$optParams = array();
$course = $service->courses->create($course, $optParams);
还要记住,由于 $course
是一个对象,因此不建议像处理数组那样打印它。您可以改为打印对象的特定信息,例如:
printf("Course created: %s (%s)\n", $course->name, $course->id);