Ismail
Ismail

Reputation: 743

Trouble retrieving video info from JSON data using PHP for and foreach loops - YouTube API 3

Basically I want to list the last uploads of a YouTube channel, but I had trouble using the for loop or I am missing foreach use as well.

I have tried the following:

function last_uploads() {
for($i = 0; $i < 20; ){
        error_reporting(E_ALL);
        $url =      'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCBR8-60-B28hp2BmDPdntcQ&maxResults=3&key={key}&type=video';
        $json = file_get_contents($url);
        $json_data = json_decode($json, false);
        $id = $json_data->items[0]->id->videoId;
                echo '<iframe id="ytplayer" type="text/html" width="auto" height="auto" 
                src="http://www.youtube.com/embed/' . $id . '?rel=0&showinfo=1"
                frameborder="0" allowfullscreen></iframe><br class="clear" />';
}
        $i++;
        break;
}

The output of the feed url ( $url ) is the following:

    {
 "kind": "youtube#searchListResponse",
 "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/sa0AWnsYjVk9MPM3sjMlLcrfFxU\"",
 "nextPageToken": "CAMQAA",
 "pageInfo": {
  "totalResults": 219,
  "resultsPerPage": 3
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/2QHwn2uxHHlB9OQ9ViQw0LdnQwI\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "AbqT_ubkT0Y"
   },
   "snippet": {
    "publishedAt": "2015-03-03T17:44:56.000Z",
    "channelId": "UCBR8-60-B28hp2BmDPdntcQ",
    "title": "#DearMe - What Advice Would You Give Your Younger Self?",
    "description": "Share your advice by making your own #DearMe GIF at http://youtubedearme.com ** In celebration of International Women's Day, take part in YouTube's global ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/AbqT_ubkT0Y/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/AbqT_ubkT0Y/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/AbqT_ubkT0Y/hqdefault.jpg"
     }
    },
    "channelTitle": "YouTube",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/PoM38fElRhJodbFBOt_g1xgJ2RE\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "OUmMAAPX6E8"
   },
   "snippet": {
    "publishedAt": "2015-02-23T14:35:35.000Z",
    "channelId": "UCBR8-60-B28hp2BmDPdntcQ",
    "title": "Introducing the YouTube Kids app",
    "description": "Download YouTube Kids for free on your Android or iOS device. Android: http://goo.gl/SsDTHh iOS: http://goo.gl/P0cikI The YouTube Kids app is designed for ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/OUmMAAPX6E8/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/OUmMAAPX6E8/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/OUmMAAPX6E8/hqdefault.jpg"
     }
    },
    "channelTitle": "YouTube",
    "liveBroadcastContent": "none"
   }
  },
  {
   "kind": "youtube#searchResult",
   "etag": "\"IHLB7Mi__JPvvG2zLQWAg8l36UU/-017SY9hNMURE5Slk5j5CU087wE\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "SDdXVOD4llU"
   },
   "snippet": {
    "publishedAt": "2015-01-24T02:59:11.000Z",
    "channelId": "UCBR8-60-B28hp2BmDPdntcQ",
    "title": "Highlights From the YouTube Interview with President Obama",
    "description": "On Jan 22, 2015, President Obama sat down for his first interview after the State of the Union with popular YouTube creators, Bethany Mota, Hank Green and ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/SDdXVOD4llU/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/SDdXVOD4llU/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/SDdXVOD4llU/hqdefault.jpg"
     }
    },
    "channelTitle": "YouTube",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

The issue is that last_uploads() returns the last 1 video and keeps repeating it until 120 seconds exceeded error appears.

Any thoughts?

Regards,

Thank you!

Upvotes: 3

Views: 1072

Answers (2)

Ismail
Ismail

Reputation: 743

Thanks to @johnh10 [ answer ], deleting for loop got it working:

function last_uploads() {
        $url = 'https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=UCBR8-60-B28hp2BmDPdntcQ&maxResults=3&key={key}&type=video';
        $json = file_get_contents($url);
        $json_data = json_decode($json, false);
        foreach ( $json_data->items as $item ) {
            $id = $item->id->videoId;
            echo '<iframe id="ytplayer" type="text/html" width="auto" height="auto" 
                src="http://www.youtube.com/embed/' . $id . '?rel=0&showinfo=1"
                frameborder="0" allowfullscreen></iframe><br class="clear" />';
        }
}

Thanks John :)

Upvotes: 1

johnh10
johnh10

Reputation: 4185

It's just messed up.

  1. Your for loop will execute indefinitely until timeout because the $i++ is AFTER the for loop. (Truth is, the for loop isn't even needed because you're fetching the latest 3 videos from the channel - no need to do that 20 times).
  2. Your code only fetches the id of the first video that is returned. This is where a for loop should be to loop through the results.

    foreach ( $json_data->items as $item ) { $id = $item->id->videoId; # print your stuff }

Upvotes: 3

Related Questions