The error message is self-explanatory. It says Argument 1 passed to Google_Service_Classroom_Resource_Courses::create() must be an instance of Google_Service_Classroom_Course, array given
and this occurs because $postBody
is an array.
As shown in the documentation of function create
create( Google_Service_Classroom_Course $postBody, array $optParams = array() )
this function expects the first parameter to be a Google_Service_Classroom_Course
object instead of an array. So you need to first create an object of this type and then pass this object as parameter.
So the end result should be like:
$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);
note: since the second parameter has a default value of empty array (array $optParams = array()
) it is not necessary to include it explicitly. The last line of the above is exactly the same with:
$optParams = array();
$course = $service->courses->create($course, $optParams);
Also have in mind that since $course
is an object its not advised to print it like you do with arrays. You can instead print specific info of the object like:
printf("Course created: %s (%s)
", $course->name, $course->id);
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…