首页 > 解决方案 > Google 课堂 - 创建课程错误 PHP

问题描述

我正在尝试使用 PHP 使用 Google_Service_Classroom API 创建课程,但在调用中出现错误。

<?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;

我的回报:

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

我正在查看文档,但我看不到在哪里解决此问题。

标签: phpgoogle-classroom

解决方案


错误消息是不言自明的。它说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);

推荐阅读