How to Filter course object fields from Google Classroom courses list using PHP

Viewed 109

I want to filter course object fields from the Google Classroom courses list. When I call API for the course list It responds to all objects. But I want specific course object names, sections from the course list.

$optParams = array(
  'pageSize' => 100,
   'courses' => 'name','section',
   'fields' => 'courses(id)'
);
$results = $service->courses->listCourses($optParams);

How I get specific course object names, sections from the course list using PHP.

The below problem shows if the code test

Fatal error: Uncaught Google\Exception: (list) unknown parameter: 'courses' in C:\xampp\htdocs\classroom\vendor\google\apiclient\src\Service\Resource.php:153 Stack trace: #0 C:\xampp\htdocs\classroom\vendor\google\apiclient-services\src\Classroom\Resource\Courses.php(122): Google\Service\Resource->call('list', Array, 'Google\\Service\\...') #1 C:\xampp\htdocs\classroom\quickstart1.php(70): Google\Service\Classroom\Resource\Courses->listCourses(Array) #2 C:\xampp\htdocs\classroom\quickstart1.php(132): test('406487331584') #3 {main} thrown in C:\xampp\htdocs\classroom\vendor\google\apiclient\src\Service\Resource.php on line 153

If the code part will as below then the response has all objects names without value and shows fields object name,section and value which I want.

$optParams = array(
  'pageSize' => 100,
   'fields' => 'courses(name,section)'
);
$results = $service->courses->listCourses($optParams);

List Courses Response Object:

 [courses] => Array
        (
            [0] => Google\Service\Classroom\Course Object
                (
                    [collection_key:protected] => courseMaterialSets
                    [alternateLink] => 
                    [calendarId] => 
                    [courseGroupEmail] => 
                    [courseMaterialSetsType:protected] => Google\Service\Classroom\CourseMaterialSet
                    [courseMaterialSetsDataType:protected] => array
                    [courseState] => 
                    [creationTime] => 
                    [description] => 
                    [descriptionHeading] => 
                    [enrollmentCode] => 
                    [guardiansEnabled] => 
                    [id] => 
                    [name] => Android
                    [ownerId] => 
                    [room] => 
                    [section] => PC-D
                    [teacherFolderType:protected] => Google\Service\Classroom\DriveFolder
                    [teacherFolderDataType:protected] => 
                    [teacherGroupEmail] => 
                    [updateTime] => 
                    [internal_gapi_mappings:protected] => Array
                        (
                        )

                    [modelData:protected] => Array
                        (
                        )

                    [processed:protected] => Array
                        (
                        )

                )

            [1] => Google\Service\Classroom\Course Object
                (
                    [collection_key:protected] => courseMaterialSets
                    [alternateLink] => 
                    [calendarId] => 
                    [courseGroupEmail] => 
                    [courseMaterialSetsType:protected] => Google\Service\Classroom\CourseMaterialSet
                    [courseMaterialSetsDataType:protected] => array
                    [courseState] => 
                    [creationTime] => 
                    [description] => 
                    [descriptionHeading] => 
                    [enrollmentCode] => 
                    [guardiansEnabled] => 
                    [id] => 
                    [name] => CSS
                    [ownerId] => 
                    [room] => 
                    [section] => PC-D
                    [teacherFolderType:protected] => Google\Service\Classroom\DriveFolder
                    [teacherFolderDataType:protected] => 
                    [teacherGroupEmail] => 
                    [updateTime] => 
                    [internal_gapi_mappings:protected] => Array
                        (
                        )

                    [modelData:protected] => Array
                        (
                        )

                    [processed:protected] => Array
                        (
                        )

                )

But I need to Look like as below:

 [courses] => Array
        (
            [0] => Google\Service\Classroom\Course Object
                (
                    
                    [name] => Android                
                    [section] => PC-D
                   
                   
                )
         )
1 Answers

What you are looking for is something called partial response. All of the google apis support it as far as I know.

The way it works is you request the properties you want to see. It can be a bit tricky to get to work I recommend using the try me on the documentation page to test it out before you add it to your code.

Lets look at how it works though.

This is the response object for your method. It returns a list of courses. So the first thing you will probably want is courses.

{
  "courses": [
    {
      object (Course)
    }
  ],
  "nextPageToken": string
}

Then you may want to limit the object within courses. Which is a course object A course object has an id. So what if you just wanted all the ids for all the course.

The code should look something like this.

$optParams = array(
  'pageSize' => 100,
   'courses' => 'name','section',
   'fields' => 'courses(id)'
);
$results = $service->courses->listCourses($optParams);

Note i dont have access to this api so i cant test it let me know if you have any issues.

update from updated question and comment

What fields does is it ensures that the fields you ask for are only retured. So by the looks of it the changes i sugested are working

However you state you want to see something like this

[courses] => Array
        (
            [0] => Google\Service\Classroom\Course Object
                (
                    
                    [name] => Android                
                    [section] => PC-D
                   
                   
                )
         )

That is not possible. The API will just return nulls in the fields that you request partial response is just intended to make the request faster. Your code should just ignore the null fields.

Related