【问题标题】:Google Classroom - Create Course Error PHPGoogle 课堂 - 创建课程错误 PHP
【发布时间】:2021-01-06 18:39:58
【问题描述】:

我正在尝试使用 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;

我的回报:

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

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

【问题讨论】:

    标签: php google-classroom


    【解决方案1】:

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

    【讨论】:

    • 非常感谢您的帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多