2

I have a json file and I want to display its data in a PHP file. I tried with the code below but its not work.

$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");
$json = json_decode($json_output, true);


foreach($json->data->items->thumbnail as $day) {

   echo $day->sqDefault;
   echo $day->hqDefault;
}

and my json file is

{"apiVersion":"2.1","data":{"id":"PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL","author":"Tutorial top","title":"دورة سيو للمبتدئين [ali baba]","description":"","thumbnail":{"sqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/default.jpg","hqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/hqdefault.jpg"},"content":{"5":"http://www.youtube.com/p/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL"},"totalItems":31,"startIndex":1,"itemsPerPage":1,"items":[{"id":"PLH_kL5FgJPmdcYTGqaMXFsVJJ-pbR_YyiC1Y73R1tfYY","position":1,"author":"Tutorial top","video":{"id":"Kw8m5S2OhPs","uploaded":"2013-11-10T16:04:28.000Z","updated":"2014-02-18T17:59:36.000Z","uploader":"vkd-nAV91SLMCYNBKGJmsA","category":"People","title":"Beginners SEO Tutorial Course   Intro","description":"","thumbnail":
{"sqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg","hqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?
v=Kw8m5S2OhPs&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=Kw8m5S2OhPs"},"content":{"5":"https://www.youtube.com/v/Kw8m5S2OhPs?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-
4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":413,"aspectRatio":"widescreen","rating":5.0,"likeCount":"1","ratingCount":1,"viewCount":283,"favoriteCount":0,"commentCount":0,"accessControl":
{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed",
"autoPlay":"allowed","syndicate":"allowed"}}}]}}

I want the output like this

sqDefault  |   hqDefault

https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg,|https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg
2
  • what type of Output as you expected..tel me format Commented Apr 10, 2015 at 5:13
  • ok you can see the output above now Commented Apr 10, 2015 at 5:21

4 Answers 4

4

After many trials this is working fine.

I have tested in my local system.

Here is code:--

<?php
$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");
$json = json_decode($json_output);
error_reporting(0);
foreach($json->data->thumbnail as $image) {
   echo $image.",|";
}

?>

Output:-

https://i.ytimg.com/vi/XIhVZqCVqhs/default.jpg,|https://i.ytimg.com/vi/XIhVZqCVqhs/hqdefault.jpg

Hope this helps

Sign up to request clarification or add additional context in comments.

Comments

3

Firstly, if you'd like to access the JSON structure as an object (as opposed to an array) drop the second argument of json_decode(), otherwise you end up with an array as $json. Have a look at the first example to see the difference.
Then you should loop through the items array. Each of its elements has a video property, which has the thumbnailproperty you want

$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");
$json = json_decode($json_output);
foreach ($json->data->items as $item) {
    echo $item->video->thumbnail->sqDefault;
    echo $item->video->thumbnail->hqDefault;
}

should do the job. You can see it in action.

To get a clear picture of the structure one of the many online available JSON viewers could help you ;-)

Comments

1

See that I remove the 2nd param in the json_decode() and I iterate with $json->data->items[0]->video->thumbnail and at the end I remove the final | with rtrim()

You can use this example:

<?php

$json_output = '{"apiVersion":"2.1","data":{"id":"PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL","author":"Tutorial top","title":"دورة سيو للمبتدئين [ali baba]","description":"","thumbnail":{"sqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/default.jpg","hqDefault":"https://i.ytimg.com/vi/XIhVZqCVqhs/hqdefault.jpg"},"content":{"5":"http://www.youtube.com/p/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL"},"totalItems":31,"startIndex":1,"itemsPerPage":1,"items":[{"id":"PLH_kL5FgJPmdcYTGqaMXFsVJJ-pbR_YyiC1Y73R1tfYY","position":1,"author":"Tutorial top","video":{"id":"Kw8m5S2OhPs","uploaded":"2013-11-10T16:04:28.000Z","updated":"2014-02-18T17:59:36.000Z","uploader":"vkd-nAV91SLMCYNBKGJmsA","category":"People","title":"Beginners SEO Tutorial Course   Intro","description":"","thumbnail":
{"sqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg","hqDefault":"https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg"},"player":{"default":"https://www.youtube.com/watch?
v=Kw8m5S2OhPs&feature=youtube_gdata_player","mobile":"https://m.youtube.com/details?v=Kw8m5S2OhPs"},"content":{"5":"https://www.youtube.com/v/Kw8m5S2OhPs?version=3&f=playlists&app=youtube_gdata","1":"rtsp://r8---sn-4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYDSANFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp","6":"rtsp://r8---sn-
4g57kues.c.youtube.com/CiULENy73wIaHAn7hI4t5SYPKxMYESARFEgGUglwbGF5bGlzdHMM/0/0/0/video.3gp"},"duration":413,"aspectRatio":"widescreen","rating":5.0,"likeCount":"1","ratingCount":1,"viewCount":283,"favoriteCount":0,"commentCount":0,"accessControl":
{"comment":"allowed","commentVote":"allowed","videoRespond":"moderated","rate":"allowed","embed":"allowed","list":"allowed",
    "autoPlay":"allowed","syndicate":"allowed"}}}]}}';
 /*
  * If you want use the URL remove the comments
 */   
//$json_output = file_get_contents("https://gdata.youtube.com/feeds/api/playlists/PL3n6kxxrIGdat31ZIf_7wlqtT3I20OqLL?v=2&alt=jsonc");

    $json = json_decode($json_output);
    $string = '';

    foreach($json->data->items[0]->video->thumbnail as $day) {


  $string .= $day . "|";
}

echo rtrim($string, '|');

Output:

https://i.ytimg.com/vi/Kw8m5S2OhPs/default.jpg|https://i.ytimg.com/vi/Kw8m5S2OhPs/hqdefault.jpg

Comments

-1

try removing true from json_decode()

4 Comments

add "video" to foreach. foreach($json->data->items->video->thumbnail as $day) I am making a mess in your code :)
the same the last problem no thing appear to me
when you var_dump($json); what do you see?
thank you for your care :) my problem is solved and for your code i'm not using javascript in my project

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.