1

I am currently using YouTube's API JSON-C response to pull data from a playlist and display the content in a list. I am doing this using PHP, however because YouTube restricts the maximum number of videos called, I have a hit a stumbling block. The maximum I can request is 50 whereas I have over 200 videos which I need to put in a list and I want to be able to do this dynamically.

I understand that I will have to loop the response, which is what I have done but is there a way it can be dynamically done?

If you could help me that would be great, my code is:

$count = 0;
foreach($data->data->items as $item) {
    $count++;
    echo $count." ".$item->id;
    echo " - ";
    echo $item->title;
    echo "<br />";

    if($count == 50) {
        $query = "http://gdata.youtube.com/feeds/api/videos?q=USERNAME&start-index=50&max-results=50&v=2&alt=jsonc";
        $data = file_get_contents($query);
        if($data){
            $data = json_decode($data);
            foreach($data->data->items as $item) {
                $count++;
                echo $count." ".$item->id;
                echo " - ";
                echo $item->title;
                echo "<br />";
            }
        }
    }

    if($count == 100) {
        $query = "http://gdata.youtube.com/feeds/api/videos?q=USERNAME&start-index=100&max-results=50&v=2&alt=jsonc";
        $data = file_get_contents($query);
        if($data){
            $data = json_decode($data);
            foreach($data->data->items as $item) {
                $count++;
                echo $count." ".$item->id;
                echo " - ";
                echo $item->title;
            echo "<br />";
            }
        }
    }
}

and so on...

If you could help me out, or at least point me in the right direction that would be great, thanks.

Programmer Bruce
  • 64,977
  • 7
  • 99
  • 97
age2011
  • 37
  • 3

2 Answers2

0

One way is to loop over requests, and then over each item in the request. Like this:

$count = 1;
do {
    $data = ...; // get 50 results starting at $count
    foreach ($data->items as $item) {
        echo "$count {$item->id} - {$item->title}<br />\n";
        $count++;
    }
} while (count($data->items) == 50);

Note that start-index is 1-based, so you have to query for 1, 51, 101 etc.

(This is actually quite similar to reading a file through a buffer, except with a file you've reached the end if the read gives you 0 bytes, while here you've reached the end if you get less than the amount you asked for.)

aaz
  • 5,136
  • 22
  • 18
  • Thanks for this, I had managed to work up something similar and its working like magic. Thank you :) – age2011 Feb 16 '11 at 20:59
0

What I would do is first call the 4 pages, and then combine the results into 1 single array, then iterate over the data.

$offsets = array(0,50,100,150);
$data = array();

foreach($offsets as $offset)
{
    $query = "http://gdata.youtube.com/feeds/api/videos?q=USERNAME&start-index=" . $offset . "&max-results=50&v=2&alt=jsonc";
    $set = file_get_contents($query);
    if(!emprty($set ))
    {
        $data = array_merge($data,json_decode($set));
    }
}

//use $data here 
RobertPitt
  • 56,863
  • 21
  • 114
  • 161
  • Thanks for this, if I knew the exact number of videos I would do it this way but there could be more added over time so I'd need the offsets to be dynamically incremented. I have created a similar solution to aaz so I have it working, thanks very much for your answer though. – age2011 Feb 16 '11 at 21:01