我正在尝试制作一个小型Web应用程序作为使用Google API PHP客户端的辅助项目。在用户授予应用程序许可后,它会请求用户的播放列表(mine = true参数),然后应将名称和ID保存到数组中。我遇到的问题是我有限的PHP知识造成的。
我正在这样实例化Google和YouTube客户端:
$client = new Google_Client();
$youtube = new Google_Service_YouTube($client);
然后,我向YouTube API发送这样的请求:
$listResponse = $youtube->playlists->listPlaylists('snippet', $queryParams);
$listResponse
变量返回正确的结果,但是它是一个使我感到非常奇怪的对象。该对象包含多个数组,这使我很难存储此数据。然后是分页问题。对我来说,将请求的结果保存到数组中的最佳方法是什么(以便以后可以将其存储在数据库中),然后根据分页将结果的下一页添加到这些结果的顶部?
这是来自API的响应的示例,其中包含两个结果(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) {
}
}
}
}
}
}
我无法真正帮助您将对象隐蔽为自己的对象,因为我们不知道您的对象是什么样。
我可以告诉您,返回的对象有一个名为items的数组,这是播放列表类型的单行。我将从$ listRespose-> items
循环开始关于分页。像这样的东西应该工作
$token = $results->getNextPageToken();
while($token != null) {
$params = array('pageToken' => $token);
$listResponse = $youtube->playlists->listPlaylists('snippet', $params);
$token = $results->getNextPageToken();
}