0

I've been trying to access the "videoID" JSON object in the following array:

{
 "kind": "youtube#searchListResponse",
 "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/652q8gXfNeBDSoSQrv8VCrAv0Ho\"",
 "nextPageToken": "CAUQAA",
 "regionCode": "AU",
 "pageInfo": {
  "totalResults": 25,
  "resultsPerPage": 5
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/31YuvB6enDzigibEfdgUq4-ZYc0\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "Ldx1nwBd4AY"
   },
   "snippet": {
    "publishedAt": "2017-04-14T21:00:00.000Z",
    "channelId": "UC-z95jtL6-oDyFueQPoZLfQ",
    "title": "HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS Tutorial - Tech Scout",
    "description": "LIKE THE VIDEO + TURN ON CHANNEL NOTIFICATIONS! HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS ...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Tech Scout",
    "liveBroadcastContent": "none"
   }
  },

It continues from there but I've been trying to access the "videoID" object using the following code:

$youtubeurl = "https://www.googleapis.com/youtube/v3/search?order=date&part=snippet&channelId=" . $channelid . "&key=" . $api;
$youtubeinfo = file_get_contents($youtubeurl);
$youtubeencoded = json_decode($youtubeinfo, true);
$youtubevideoid = $youtubeencoded["data"]["items"][0]["videoID"];
echo $youtubevideoid;

I don't have too much experience with JSON so I just got the code in the 4th line (which I think is the problem) from another question on Stack Overflow.

Hydrone
  • 73
  • 8

3 Answers3

3

You don't have node data in your JSON. You also missed node id. Also, it's videoId and not videoID:

$youtubevideoid = $youtubeencoded["items"][0]["id"]["videoId"];
Saugat
  • 1,309
  • 14
  • 22
Pavlo Zhukov
  • 3,007
  • 3
  • 26
  • 43
1

Try

$youtubevideoid = $youtubeencoded["items"][0]["id"]["videoId"];

p.s. updated

Leguest
  • 2,097
  • 2
  • 17
  • 20
  • Thanks, but that did not work. I'm going to keep playing around with it and hope I find something that works – Hydrone Apr 15 '17 at 07:56
0

Your json file is wrong.it will be like that

{
   "kind": "youtube#searchListResponse",
   "etag":    "\"m2yskBQFythfE4irbTIeOgYYfBU/652q8gXfNeBDSoSQrv8VCrAv0Ho\"",
   "nextPageToken": "CAUQAA",
   "regionCode": "AU",
   "pageInfo": {
       "totalResults": 25,
       "resultsPerPage": 5
   },
   "items": [
     {
         "kind": "youtube#searchResult",
          "etag": "\"m2yskBQFythfE4irbTIeOgYYfBU/31YuvB6enDzigibEfdgUq4-ZYc0\"",
         "id": {
            "kind": "youtube#video",
             "videoId": "Ldx1nwBd4AY"
           },
          "snippet": {
              "publishedAt": "2017-04-14T21:00:00.000Z",
              "channelId": "UC-z95jtL6-oDyFueQPoZLfQ",
              "title": "HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS Tutorial - Tech Scout",
             "description": "LIKE THE VIDEO + TURN ON CHANNEL NOTIFICATIONS! HOW TO PLAY NINTENDO DS GAMES ON iPHONE iOS 10 FOR FREE (NDS4iOS) - NDS4IOS ...",
             "thumbnails": {
               "url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/default.jpg",
              "width": 120,
               "height": 90
    },
          "medium": {
             "url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/mqdefault.jpg",
            "width": 320,
            "height": 180
          },
          "high": {
            "url": "https://i.ytimg.com/vi/Ldx1nwBd4AY/hqdefault.jpg",
               "width": 480,
               "height": 360
          }
       },
      "channelTitle": "Tech Scout",
      "liveBroadcastContent": "none"
      }
   ]
 }

And then you can use this code

$data = json_decode($youtubejson,true);
echo $data["kind"];
ismetsezer
  • 19
  • 6