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
4
Answers
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 avideo
property, which has thethumbnail
property you wantshould 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 😉
try removing true from
json_decode()
After many trials this is working fine.
I have tested in my local system.
Here is code:–
Output:-
Hope this helps
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|
withrtrim()
You can use this example:
Output: