Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

youtube data api 3 php ,how to get more than 50 video from a channel?

I am working on one project which requirs to lists all videos from a channel using youtubedata api 3.0 but not from gdata(feed), the Api returns only 50 videos from channel, and there is no reference to get more videos on developers.google. Help. This is my code

<?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> 
like image 438
ghost Avatar asked Jan 22 '14 17:01

ghost


People also ask

Does YouTube API have a limit?

Projects that enable the YouTube Data API have a default quota allocation of 10,000 units per day, an amount sufficient for the majority of our API users. You can see your quota usage on the Quotas page in the API Console.

How do I increase quota on YouTube API?

If you have been audited in the past 12 months and have been marked compliant by YouTube API Services Team, you can apply for an additional quota extension using this form. If YouTube denies the application for an additional quota extension, you can file an appeal by using this form.

Why is YouTube API quota exceeded?

If you have a high traffic site, that limit might be too small. If so, we suggest that you try things like reducing your gallery's page size or using the plugin's caching feature. After that, your traffic might be so high that you still get the "Youtube Data API quota exceeded" error message.

Is YouTube Data API v3 free?

Yes, using the YouTube API does not incur any monetary cost for the entity calling the API. If you go over your quota an 403 Error will be returned by the API.


1 Answers

YouTube API supports pagination. You need to put your nextPageToken in your next request as pageToken to get the next page(50 in your case) of results.

These 2 answers of mine should help you:

page tokens use youtube api v3

Retrieve all videos from youtube playlist using youtube v3 API

like image 198
Ibrahim Ulukaya Avatar answered Oct 15 '22 16:10

Ibrahim Ulukaya