PHP谷歌课堂API list_classes

eqfvzcg8  于 2023-09-29  发布在  PHP
关注(0)|答案(3)|浏览(95)

我在使用PHP集成Google API时遇到了问题。
下面的代码将拉取类,但它会拉取所有类(ACTIVE、ARCHIVED等)。我只想要活跃的班级。
问题是当我添加courseStates参数时,它说它不存在。

$service = new Google_Service_Classroom($client);
            $courses = '';

            $optParams = array(
//none of these seem to work
              //'courseStates' => 'ACTIVE',
              //'courseState' => 'ACTIVE',
              //'CourseStates' => 'ACTIVE',
              'pageSize' => 50,
              'studentId' => $user_email
            );
            $results = $service->courses->listCourses($optParams);

            if (count($results->getCourses()) == 0) {
             // print "No courses found.\n";
            } else {
             // print "Courses:\n";
              foreach ($results->getCourses() as $course) {
                                $courses .= $course->getName()."<br />";//." ". $course->getId();

                //printf("%s (%s)\n", $course->getName(), $course->getId());
              }
            }

方法在这里,但显示的选项都不起作用https://developers.google.com/classroom/reference/rest/v1/courses/list

7gcisfzg

7gcisfzg1#

$results = $service->courses->listCourses($optParams);
if (count($results->getCourses()) == 0) {
    // print "No courses found.\n";
} else {
    // print "Courses:\n"; 
    
    $insert_array_fill = '';
    $select_array_fill = '';
    foreach ($results->getCourses() as $course) {
        // 
        //echo "<hr>";
        if(strcmp($course->getCourseState(), 'ACTIVE') == 0){
            
            $courses .= $course->getName()." - ".$course->getId()." - ".$course->getAlternateLink()." - ".$course->getOwnerId()."<br />";//." ". $course->getId();
        }
    }
}
jbose2ul

jbose2ul2#

选择的解决方案不正确。问题是您拼错了密钥(courseState 而不是 courseStates *,缺少s*)。
应该是-

'courseStates'  => 'ACTIVE'

此答案与Google API v2.4.0版本相关。

pqwbnv8z

pqwbnv8z3#

变量CourseState的类型是Enum而不是String,因此,它必须作为enum而不是string访问。将以下内容添加到optParams

'courseState' => 'courseState::ACTIVE',

相关问题