-1

The JSON structure below shows the format of a search result. You can get more details here

{
  "kind": "youtube#searchResult",
  "etag": etag,
  "id": {
    "kind": string,
    "videoId": string,
    "channelId": string,
    "playlistId": string
  },
  "snippet": {
    "publishedAt": datetime,
    "channelId": string,
    "title": string,
    "description": string,
    "thumbnails": {
      (key): {
        "url": string,
        "width": unsigned integer,
        "height": unsigned integer
      }
    },
    "channelTitle": string,
    "liveBroadcastContent": string
  }
}

Below is the code I am following to get the values in PHP file.

 $searchResponse = $youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => $_GET['maxResults'],
    ));

    foreach ($searchResponse['items'] as $searchResult) {
      switch ($searchResult['id']['kind']) {
        case 'youtube#video':
          $videos[] = $searchResult;
          break;
      }

   foreach ($videos as $video) {
      echo $video['snippet']['title'];
   }

I am able to get title tag from here but How to get thumbnail url string value. I tried this using $video['snippet']['thumbnails']['key']['url'].

EDIT: I just executed the code directly and got this as response. Now, I tried to access url value as $video['snippet']['thumbnails']['high']['url']. Still No luck !! It says Warning: Illegal string offset 'high'.

{
 "kind": "youtube#searchListResponse",
 "etag": "\"X98aQHqGvPBJLZLOiSGUHCM9jnE/iTnovD87h4Y7nwPlTFtrcd7IEPY\"",
 "nextPageToken": "CAEQAA",
 "pageInfo": {
  "totalResults": 118094,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"X98aQHqGvPBJLZLOiSGUHCM9jnE/d7ArhLqOVP8ys-9qfM5Mk0UTbH4\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "ixnBb9cyTkg"
   },
   "snippet": {
    "publishedAt": "2014-04-09T16:58:02.000Z",
    "channelId": "UC7rvhg8JyqjmD_D2BV5lzNQ",
    "title": "Mahabharat 9 April 2014 Full Episode (Krishna Save Draupadi Maha Episode)",
    "description": "Mahabharat 10 April Promo ( Pandavas Anger ) https://www.youtube.com/watch?v=pGWMzWf-h2s Mahabharat 9 April 2014 Full Episode.",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/ixnBb9cyTkg/default.jpg"
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/ixnBb9cyTkg/mqdefault.jpg"
     },
     "high": {
      "url": "https://i.ytimg.com/vi/ixnBb9cyTkg/hqdefault.jpg"
     }
    },
    "channelTitle": "1ColorsTV",
    "liveBroadcastContent": "none"
   }
  }
 ]
}
Zini
  • 909
  • 7
  • 15
  • looking in to this... first thing I notice is the use of $_GET... this is pretty dangerous to do, it could cause all kinds of code infections... better would be to use filter_input(INPUT_GET,"q",FILTER_SANITIZE_STRING) – patrick Apr 12 '14 at 01:09

2 Answers2

0

You are accessing it correctly .. but your JSON is kind of Malformed..

Below is the fixed JSON and the code...

The code..

<?php
$json='{
  "kind": "youtube#searchResult",
  "etag": "etag",
  "id": {
    "kind": "string",
    "videoId": "string",
    "channelId": "string",
    "playlistId": "string"
  },
  "snippet": {
    "publishedAt": "datetime",
    "channelId": "string",
    "title": "string",
    "description": "string",
    "thumbnails": {
      "key": {
        "url": "string",
        "width": "unsigned integer",
        "height": "unsigned integer"
      }
    },
    "channelTitle": "string",
    "liveBroadcastContent": "string"
  }
}';

$yourarr = json_decode($json,true);
echo $yourarr['snippet']['thumbnails']['key']['url'];

Demo

Community
  • 1
  • 1
Shankar Narayana Damodaran
  • 68,075
  • 43
  • 96
  • 126
0

It's not a multi-dimentional array, it's an object...

$j=$youtube->search->listSearch('id,snippet', array(
      'q' => $_GET['q'],
      'maxResults' => $_GET['maxResults'],
    ));

Now you can get the object displayed by using:

var_dump($j);

the way to get to your snippet's title is then easy to find:

echo $j->items[0]->snippet->title;
patrick
  • 11,519
  • 8
  • 71
  • 80
  • It says Warning: json_decode() expects parameter 1 to be string, object given. – Jain Shubham Apr 12 '14 at 01:53
  • repace it by your JSON string. I couldn't debug your $youtube->search->listSearch function, so I assumed it returned a string. What might even work is remove the json_decode altogether since the $youtube function apparently returns an object already... you might not need to json_decode that at all – patrick Apr 12 '14 at 02:01
  • I don't know why it wasn't working before but it is working and I am good to go. Thanks ! – Jain Shubham Apr 12 '14 at 02:27