youtube data api 3 php,如何从一个频道获得50多个视频?
发布时间:2020-12-13 13:32:39 所属栏目:PHP教程 来源:网络整理
导读:我正在开发一个项目,要求使用youtubedata api 3.0列出来自频道的所有视频,而不是来自gdata(feed),Api只返回来自频道的50个视频,并且没有参考可以获得有关developers.google的更多视频. 救命. 这是我的代码 ?phprequire_once 'contrib/Google_YoutubeService.
我正在开发一个项目,要求使用youtubedata api 3.0列出来自频道的所有视频,而不是来自gdata(feed),Api只返回来自频道的50个视频,并且没有参考可以获得有关developers.google的更多视频.
救命. 这是我的代码 <?php require_once 'contrib/Google_YoutubeService.php';*/ require_once '../upload/google-api-php-client/src/Google_Client.php'; require_once '../upload/google-api-php-client/src/contrib/Google_YouTubeService.php'; session_start(); /* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console <http://code.google.com/apis/console#access> For more information about using OAuth2 to access Google APIs,please visit: <https://developers.google.com/accounts/docs/OAuth2> Please ensure that you have enabled the YouTube Data API for your project. */ $OAUTH2_CLIENT_ID = 'sadsadsadasdsad'; $OAUTH2_CLIENT_SECRET = 'sadsadsadasdasdasdasd'; $client = new Google_Client(); $client->setClientId($OAUTH2_CLIENT_ID); $client->setClientSecret($OAUTH2_CLIENT_SECRET); $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],FILTER_SANITIZE_URL); $client->setRedirectUri($redirect); $youtube = new Google_YoutubeService($client); if (isset($_GET['code'])) { if (strval($_SESSION['state']) !== strval($_GET['state'])) { die('The session state did not match.'); } $client->authenticate(); $_SESSION['token'] = $client->getAccessToken(); header('Location: ' . $redirect); } if (isset($_SESSION['token'])) { $client->setAccessToken($_SESSION['token']); } if ($client->getAccessToken()) { try { $channelsResponse = $youtube->channels->listChannels('contentDetails',array( 'mine' => 'true',)); $htmlBody = ''; foreach ($channelsResponse['items'] as $channel) { $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads']; $pageToken=1; while($pageToken<=25){ $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet',array( 'playlistId' => $uploadsListId,'maxResults' => 50,)); $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>"; foreach ($playlistItemsResponse['items'] as $playlistItem) { $htmlBody .= sprintf('<li>%s (%s)</li>',$playlistItem['snippet']['title'],$playlistItem['snippet']['resourceId']['videoId']); } $htmlBody .= '</ul>'; } } } catch (Google_ServiceException $e) { $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage())); } catch (Google_Exception $e) { $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage())); } $_SESSION['token'] = $client->getAccessToken(); } else { $state = mt_rand(); $client->setState($state); $_SESSION['state'] = $state; $authUrl = $client->createAuthUrl(); $htmlBody = <<<END <h3>Authorization Required</h3> <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p> END; } ?> <!doctype html> <html> <head> <title>YouTube Search</title> </head> <body> <?=$htmlBody?> </body> </html>
Yepp现在已经很晚了,但由于你没有接受答案,让我在这里给你解决方案.
说你打了一个网址BaseURL 所以当你点击api获得前50个视频时,它会给你一个nextPageToken 要获得下五十个视频,您只需将此令牌附加到BaseURL,如下所示: BaseURL“& pageToken =”nextPageToken 同样地,如果你已经做了一些分页,你也可以得到一个prevPageToken,现在你只需要像上面解释的那样附加这个标记来到达上一页. (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |