Skip to content
Advertisement

Google Classroom – Create Course Error PHP

I’m trying to create a course using the Google_Service_Classroom API, using PHP, but I get an error in the call.

<?php
require_once __DIR__.'/vendor/autoload.php';
putenv('GOOGLE_APPLICATION_CREDENTIALS=C:laragonwwwclassroom.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;

My 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:laragonwwwteste-03.php on line 41 and defined in C:laragonwwwvendorgoogleapiclient-servicessrcGoogleServiceClassroomResourceCourses.php:47 Stack trace: #0 C:laragonwwwteste-03.php(41): Google_Service_Classroom_Resource_Courses->create(Array, Array) #1 {main} thrown in C:laragonwwwvendorgoogleapiclient-servicessrcGoogleServiceClassroomResourceCourses.php on line 47

I’m seeing the documents, but I don’t see where to fix this problem.

Advertisement

Answer

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)n", $course->name, $course->id);
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement