youtube data api 3 php,如何从一个频道获得50多个视频?

前端之家收集整理的这篇文章主要介绍了youtube data api 3 php,如何从一个频道获得50多个视频?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我正在开发一个项目,要求使用youtubedata api 3.0列出来自频道的所有视频,而不是来自gdata(Feed),Api只返回来自频道的50个视频,并且没有参考可以获得有关developers.google的更多视频.
救命.
这是我的代码
  1. <?PHP
  2.  
  3. require_once 'contrib/Google_YoutubeService.PHP';*/
  4. require_once '../upload/google-api-PHP-client/src/Google_Client.PHP';
  5. require_once '../upload/google-api-PHP-client/src/contrib/Google_YouTubeService.PHP';
  6. session_start();
  7.  
  8. /* You can acquire an OAuth 2 ID/secret pair from the API Access tab on the Google APIs Console
  9. <http://code.google.com/apis/console#access>
  10. For more information about using OAuth2 to access Google APIs,please visit:
  11. <https://developers.google.com/accounts/docs/OAuth2>
  12. Please ensure that you have enabled the YouTube Data API for your project. */
  13. $OAUTH2_CLIENT_ID = 'sadsadsadasdsad';
  14. $OAUTH2_CLIENT_SECRET = 'sadsadsadasdasdasdasd';
  15.  
  16. $client = new Google_Client();
  17. $client->setClientId($OAUTH2_CLIENT_ID);
  18. $client->setClientSecret($OAUTH2_CLIENT_SECRET);
  19. $redirect = filter_var('http://' . $_SERVER['HTTP_HOST'] . $_SERVER['PHP_SELF'],FILTER_SANITIZE_URL);
  20. $client->setRedirectUri($redirect);
  21.  
  22. $youtube = new Google_YoutubeService($client);
  23.  
  24. if (isset($_GET['code'])) {
  25. if (strval($_SESSION['state']) !== strval($_GET['state'])) {
  26. die('The session state did not match.');
  27. }
  28.  
  29. $client->authenticate();
  30. $_SESSION['token'] = $client->getAccessToken();
  31. header('Location: ' . $redirect);
  32. }
  33.  
  34. if (isset($_SESSION['token'])) {
  35. $client->setAccessToken($_SESSION['token']);
  36. }
  37.  
  38. if ($client->getAccessToken()) {
  39. try {
  40. $channelsResponse = $youtube->channels->listChannels('contentDetails',array(
  41. 'mine' => 'true',));
  42.  
  43. $htmlBody = '';
  44. foreach ($channelsResponse['items'] as $channel) {
  45. $uploadsListId = $channel['contentDetails']['relatedPlaylists']['uploads'];
  46. $pageToken=1;
  47. while($pageToken<=25){
  48. $playlistItemsResponse = $youtube->playlistItems->listPlaylistItems('snippet',array(
  49. 'playlistId' => $uploadsListId,'maxResults' => 50,));
  50.  
  51.  
  52. $htmlBody .= "<h3>Videos in list $uploadsListId</h3><ul>";
  53. foreach ($playlistItemsResponse['items'] as $playlistItem) {
  54. $htmlBody .= sprintf('<li>%s (%s)</li>',$playlistItem['snippet']['title'],$playlistItem['snippet']['resourceId']['videoId']);
  55. }
  56. $htmlBody .= '</ul>';
  57. }
  58. }
  59. } catch (Google_ServiceException $e) {
  60. $htmlBody .= sprintf('<p>A service error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage()));
  61. } catch (Google_Exception $e) {
  62. $htmlBody .= sprintf('<p>An client error occurred: <code>%s</code></p>',htmlspecialchars($e->getMessage()));
  63. }
  64.  
  65. $_SESSION['token'] = $client->getAccessToken();
  66. } else {
  67. $state = mt_rand();
  68. $client->setState($state);
  69. $_SESSION['state'] = $state;
  70.  
  71. $authUrl = $client->createAuthUrl();
  72. $htmlBody = <<<END
  73. <h3>Authorization required</h3>
  74. <p>You need to <a href="$authUrl">authorize access</a> before proceeding.<p>
  75. END;
  76. }
  77. ?>
  78.  
  79. <!doctype html>
  80. <html>
  81. <head>
  82. <title>YouTube Search</title>
  83. </head>
  84. <body>
  85. <?=$htmlBody?>
  86. </body>
  87. </html>
Yepp现在已经很晚了,但由于你没有接受答案,让我在这里给你解决方案.

说你打了一个网址BaseURL

所以当你点击api获得前50个视频时,它会给你一个nextPageToken

要获得下五十个视频,您只需将此令牌附加到BaseURL,如下所示:

BaseURL“& pageToken =”nextPageToken

同样地,如果你已经做了一些分页,你也可以得到一个prevPageToken,现在你只需要像上面解释的那样附加这个标记来到达上一页.

猜你在找的PHP相关文章