N
N
newdancer2016-06-19 18:14:59
PHP
newdancer, 2016-06-19 18:14:59

How to parse json from youtube?

How to parse json from youtube? Youtube gives me this result:

{
 "kind": "youtube#searchListResponse",
 "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/jHXo5V2uAwW_-Uhg3vfYZbOFz-w\"",
 "nextPageToken": "CAEQAA",
 "regionCode": "UA",
 "pageInfo": {
  "totalResults": 3492,
  "resultsPerPage": 1
 },
 "items": [
  {
   "kind": "youtube#searchResult",
   "etag": "\"5g01s4-wS2b4VpScndqCYc5Y-8k/F5nUecrRmG9NbJlEsQxs2ygvuhE\"",
   "id": {
    "kind": "youtube#video",
    "videoId": "Xq-Hq7lpZGo"
   },
   "snippet": {
    "publishedAt": "2012-08-01T14:30:47.000Z",
    "channelId": "UCsFbXtoqQwXW85_nYXAiBzA",
    "title": "007: Координаты Скайфолл. Русский трейлер, 2012 (HD)",
    "description": "Подпишись на канал русских трейлеров http://goo.gl/hXRG7 На MI6 совершено нападение и Джеймсу Бонду предстоит найти...",
    "thumbnails": {
     "default": {
      "url": "https://i.ytimg.com/vi/Xq-Hq7lpZGo/default.jpg",
      "width": 120,
      "height": 90
     },
     "medium": {
      "url": "https://i.ytimg.com/vi/Xq-Hq7lpZGo/mqdefault.jpg",
      "width": 320,
      "height": 180
     },
     "high": {
      "url": "https://i.ytimg.com/vi/Xq-Hq7lpZGo/hqdefault.jpg",
      "width": 480,
      "height": 360
     }
    },
    "channelTitle": "Новые трейлеры",
    "liveBroadcastContent": "none"
   }
  }
 ]
}

Actually, from the item block, you need the video id videoId. Here is the php code:
$lYou = fopen($linkYoutube, 'r');
  $jsonYou = stream_get_contents($lYou);
  $resYou = json_decode($jsonYou, true);
  var_dump($resYou);

As a result, it var_dump($resYou);returns NULL. What's wrong?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
I
IceJOKER, 2016-06-19
@IceJOKER

Try like this:

$json = file_get_contents($youtube_link);
if(empty($json)){
  die('Something went wrong...=(');
}
$data = json_decode($json, true);
var_dump($data);

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question