-1

How to get description in this json format using php ?

This is json data

{
 "items": [
  {
   "id": "DHHlQxea3Pg",
   "snippet": {
    "description": "Private Jet trip to the Rocky Mountains 1080p"
   }
  }
 ]
}

and this is my code

$curl_json_video_description = curl_init('some url');
curl_setopt($curl_json_video_description, CURLOPT_RETURNTRANSFER, true);
$json_video_description = curl_exec($curl_json_video_description);
$result_json_video_description = json_decode($json_video_description);
$video_description = $result_json_video_description->items->snippet->description;
echo $video_description;

When i test my code it's not echo anything, how can i do for get description in json ?

  • 1
    The "items" node is an array, so if you want to access the first element, you wanna use `$result_json_video_description->items[0]->snippet->description` – rdiz Oct 17 '17 at 08:08

2 Answers2

1

You are missing 0 item key, because items is array of items.

Use $result_json_video_description->items[0]->snippet->description

OR Decode it as array json_decode($json, true) and then:

$result_json_video_description = json_decode();

$result_json_video_description['items'][0]['snippet']['description']
Justinas
  • 41,402
  • 5
  • 66
  • 96
1

This code works for me :)

$content = file_get_contents('url');
$jsonize = json_decode($content, true);

echo $jsonize['items'][0]['snippet']['description'];
Michele Riva
  • 552
  • 9
  • 24