Search code examples
phpgoogle-apiyoutube-apigoogle-api-php-client

How do I save Google API results into an array and add new results on it?


I am trying to make a small web app as a side project that uses the Google API PHP client. After the application has been granted permission by the user, it requests the playlists of the user (mine=true parameter) and then it should save the names and IDs into an array. The problem I am having is the result of my limited PHP knowledge.

I am instantiating the Google and YouTube clients like this:

$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);

Then I am making a request to the YouTube API like this :

$listResponse = $youtube->playlists->listPlaylists('snippet', $queryParams);

The $listResponse variable returns the correct results but it is an object which makes it very peculiar to me. This object contains several arrays and it makes it difficult for me to store this data. Then comes the issue of pagination. What is the best way for me to save the results of this request into an array (so I can store it in a database later) and add on top of these results the next page of results as required by pagination?

This is an example of the response from the API with two results (var_dump($listResponse)):

object(Google_Service_YouTube_PlaylistListResponse)#89 (18) {
  ["collection_key":protected]=>
  string(5) "items"
  ["etag"]=>
  string(27) "zQG5YzR-JAU6eqwerdrAindolIs"
  ["eventId"]=>
  NULL
  ["itemsType":protected]=>
  string(31) "Google_Service_YouTube_Playlist"
  ["itemsDataType":protected]=>
  string(5) "array"
  ["kind"]=>
  string(28) "youtube#playlistListResponse"
  ["nextPageToken"]=>
  string(6) "CAQQAA"
  ["pageInfoType":protected]=>
  string(31) "Google_Service_YouTube_PageInfo"
  ["pageInfoDataType":protected]=>
  string(0) ""
  ["prevPageToken"]=>
  string(6) "CAIQAQ"
  ["tokenPaginationType":protected]=>
  string(38) "Google_Service_YouTube_TokenPagination"
  ["tokenPaginationDataType":protected]=>
  string(0) ""
  ["visitorId"]=>
  NULL
  ["internal_gapi_mappings":protected]=>
  array(0) {
  }
  ["modelData":protected]=>
  array(0) {
  }
  ["processed":protected]=>
  array(0) {
  }
  ["pageInfo"]=>
  object(Google_Service_YouTube_PageInfo)#93 (5) {
    ["resultsPerPage"]=>
    int(2)
    ["totalResults"]=>
    int(47)
    ["internal_gapi_mappings":protected]=>
    array(0) {
    }
    ["modelData":protected]=>
    array(0) {
    }
    ["processed":protected]=>
    array(0) {
    }
  }
  ["items"]=>
  array(2) {
    [0]=>
    object(Google_Service_YouTube_Playlist)#58 (17) {
      ["contentDetailsType":protected]=>
      string(45) "Google_Service_YouTube_PlaylistContentDetails"
      ["contentDetailsDataType":protected]=>
      string(0) ""
      ["etag"]=>
      string(27) "IXu48tsFsPRHOKKrr-ITKuPp39A"
      ["id"]=>
      string(34) "PLYZiZDBjPdaZszrfW7goQLqAsde_XNMSs"
      ["kind"]=>
      string(16) "youtube#playlist"
      ["localizationsType":protected]=>
      string(43) "Google_Service_YouTube_PlaylistLocalization"
      ["localizationsDataType":protected]=>
      string(3) "map"
      ["playerType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistPlayer"
      ["playerDataType":protected]=>
      string(0) ""
      ["snippetType":protected]=>
      string(38) "Google_Service_YouTube_PlaylistSnippet"
      ["snippetDataType":protected]=>
      string(0) ""
      ["statusType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistStatus"
      ["statusDataType":protected]=>
      string(0) ""
      ["internal_gapi_mappings":protected]=>
      array(0) {
      }
      ["modelData":protected]=>
      array(0) {
      }
      ["processed":protected]=>
      array(0) {
      }
      ["snippet"]=>
      object(Google_Service_YouTube_PlaylistSnippet)#65 (17) {
        ["collection_key":protected]=>
        string(4) "tags"
        ["channelId"]=>
        string(24) "UCBDfenjx1r7mZgHWksfpOuQ"
        ["channelTitle"]=>
        string(9) "the Legit"
        ["defaultLanguage"]=>
        NULL
        ["description"]=>
        string(0) ""
        ["localizedType":protected]=>
        string(43) "Google_Service_YouTube_PlaylistLocalization"
        ["localizedDataType":protected]=>
        string(0) ""
        ["publishedAt"]=>
        string(20) "2020-01-15T09:03:20Z"
        ["tags"]=>
        NULL
        ["thumbnailsType":protected]=>
        string(39) "Google_Service_YouTube_ThumbnailDetails"
        ["thumbnailsDataType":protected]=>
        string(0) ""
        ["title"]=>
        string(5) "music"
        ["internal_gapi_mappings":protected]=>
        array(0) {
        }
        ["modelData":protected]=>
        array(0) {
        }
        ["processed":protected]=>
        array(0) {
        }
        ["thumbnails"]=>
        object(Google_Service_YouTube_ThumbnailDetails)#91 (16) {
          ["defaultType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["defaultDataType":protected]=>
          string(0) ""
          ["highType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["highDataType":protected]=>
          string(0) ""
          ["maxresType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["maxresDataType":protected]=>
          string(0) ""
          ["mediumType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["mediumDataType":protected]=>
          string(0) ""
          ["standardType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["standardDataType":protected]=>
          string(0) ""
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
          ["default"]=>
          object(Google_Service_YouTube_Thumbnail)#94 (6) {
            ["height"]=>
            int(90)
            ["url"]=>
            string(46) "https://i.ytimg.com/vi/o5AIp2Yc01M/default.jpg"
            ["width"]=>
            int(120)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["medium"]=>
          object(Google_Service_YouTube_Thumbnail)#95 (6) {
            ["height"]=>
            int(180)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/o5AIp2Yc01M/mqdefault.jpg"
            ["width"]=>
            int(320)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["high"]=>
          object(Google_Service_YouTube_Thumbnail)#96 (6) {
            ["height"]=>
            int(360)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/o5AIp2Yc01M/hqdefault.jpg"
            ["width"]=>
            int(480)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
        }
        ["localized"]=>
        object(Google_Service_YouTube_PlaylistLocalization)#97 (5) {
          ["description"]=>
          string(0) ""
          ["title"]=>
          string(5) "music"
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
        }
      }
    }
    [1]=>
    object(Google_Service_YouTube_Playlist)#98 (17) {
      ["contentDetailsType":protected]=>
      string(45) "Google_Service_YouTube_PlaylistContentDetails"
      ["contentDetailsDataType":protected]=>
      string(0) ""
      ["etag"]=>
      string(27) "u8p3zQc4p0ouUduo7o52VI4-tRE"
      ["id"]=>
      string(34) "PLYZiZDBjPdaYHroslJaM21_60yEdP6FZE"
      ["kind"]=>
      string(16) "youtube#playlist"
      ["localizationsType":protected]=>
      string(43) "Google_Service_YouTube_PlaylistLocalization"
      ["localizationsDataType":protected]=>
      string(3) "map"
      ["playerType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistPlayer"
      ["playerDataType":protected]=>
      string(0) ""
      ["snippetType":protected]=>
      string(38) "Google_Service_YouTube_PlaylistSnippet"
      ["snippetDataType":protected]=>
      string(0) ""
      ["statusType":protected]=>
      string(37) "Google_Service_YouTube_PlaylistStatus"
      ["statusDataType":protected]=>
      string(0) ""
      ["internal_gapi_mappings":protected]=>
      array(0) {
      }
      ["modelData":protected]=>
      array(0) {
      }
      ["processed":protected]=>
      array(0) {
      }
      ["snippet"]=>
      object(Google_Service_YouTube_PlaylistSnippet)#99 (17) {
        ["collection_key":protected]=>
        string(4) "tags"
        ["channelId"]=>
        string(24) "UCBDfenjx1r7mZgHWksfpOuQ"
        ["channelTitle"]=>
        string(9) "the Legit"
        ["defaultLanguage"]=>
        NULL
        ["description"]=>
        string(0) ""
        ["localizedType":protected]=>
        string(43) "Google_Service_YouTube_PlaylistLocalization"
        ["localizedDataType":protected]=>
        string(0) ""
        ["publishedAt"]=>
        string(20) "2019-10-04T08:46:06Z"
        ["tags"]=>
        NULL
        ["thumbnailsType":protected]=>
        string(39) "Google_Service_YouTube_ThumbnailDetails"
        ["thumbnailsDataType":protected]=>
        string(0) ""
        ["title"]=>
        string(10) "electronic"
        ["internal_gapi_mappings":protected]=>
        array(0) {
        }
        ["modelData":protected]=>
        array(0) {
        }
        ["processed":protected]=>
        array(0) {
        }
        ["thumbnails"]=>
        object(Google_Service_YouTube_ThumbnailDetails)#100 (18) {
          ["defaultType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["defaultDataType":protected]=>
          string(0) ""
          ["highType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["highDataType":protected]=>
          string(0) ""
          ["maxresType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["maxresDataType":protected]=>
          string(0) ""
          ["mediumType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["mediumDataType":protected]=>
          string(0) ""
          ["standardType":protected]=>
          string(32) "Google_Service_YouTube_Thumbnail"
          ["standardDataType":protected]=>
          string(0) ""
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
          ["default"]=>
          object(Google_Service_YouTube_Thumbnail)#101 (6) {
            ["height"]=>
            int(90)
            ["url"]=>
            string(46) "https://i.ytimg.com/vi/PTxkKPSZyMg/default.jpg"
            ["width"]=>
            int(120)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["medium"]=>
          object(Google_Service_YouTube_Thumbnail)#102 (6) {
            ["height"]=>
            int(180)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/PTxkKPSZyMg/mqdefault.jpg"
            ["width"]=>
            int(320)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["high"]=>
          object(Google_Service_YouTube_Thumbnail)#103 (6) {
            ["height"]=>
            int(360)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/PTxkKPSZyMg/hqdefault.jpg"
            ["width"]=>
            int(480)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["standard"]=>
          object(Google_Service_YouTube_Thumbnail)#104 (6) {
            ["height"]=>
            int(480)
            ["url"]=>
            string(48) "https://i.ytimg.com/vi/PTxkKPSZyMg/sddefault.jpg"
            ["width"]=>
            int(640)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
          ["maxres"]=>
          object(Google_Service_YouTube_Thumbnail)#105 (6) {
            ["height"]=>
            int(720)
            ["url"]=>
            string(52) "https://i.ytimg.com/vi/PTxkKPSZyMg/maxresdefault.jpg"
            ["width"]=>
            int(1280)
            ["internal_gapi_mappings":protected]=>
            array(0) {
            }
            ["modelData":protected]=>
            array(0) {
            }
            ["processed":protected]=>
            array(0) {
            }
          }
        }
        ["localized"]=>
        object(Google_Service_YouTube_PlaylistLocalization)#106 (5) {
          ["description"]=>
          string(0) ""
          ["title"]=>
          string(10) "electronic"
          ["internal_gapi_mappings":protected]=>
          array(0) {
          }
          ["modelData":protected]=>
          array(0) {
          }
          ["processed":protected]=>
          array(0) {
          }
        }
      }
    }
  }
}

Solution

  • I cant really help you covert the object to your own object as we dont know what your object looks like.

    I can tell you that the object returned has an array called items this is a single row of type playlist. I would start with looping though $listRespose->items

    as for the pagination. Something like this should work

    $token = $results->getNextPageToken();
    while($token != null) {    
    
       $params = array('pageToken' => $token);
    
       $listResponse = $youtube->playlists->listPlaylists('snippet', $params);
    
       $token = $results->getNextPageToken();
    
    }