skip to Main Content

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


  1. 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 😉

    Login or Signup to reply.
  2. try removing true from json_decode()

    Login or Signup to reply.
  3. 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

    Login or Signup to reply.
  4. 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
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search