How to Parse YouTube JSON into PHP? -


this question has answer here:

how go parsing json youtube php variables? here json receive youtube api.

{  "kind": "youtube#videolistresponse",  "etag": "\"dc9dtkvup_z_zif9bzmhcn8kvwq/mlc20hemv6-vvtw4gdcc1f-zxbc\"",  "pageinfo": {   "totalresults": 1,   "resultsperpage": 1  },  "items": [   {    "kind": "youtube#video",    "etag": "\"dc9dtkvup_z_zif9bzmhcn8kvwq/ynrmyduegezmm0swki_kmmbjv4o\"",    "id": "e-orhee9vvg",    "contentdetails": {     "duration": "pt4m33s",     "dimension": "2d",     "definition": "hd",     "caption": "false",     "licensedcontent": true    },    "statistics": {     "viewcount": "1092004149",     "likecount": "4304240",     "dislikecount": "314746",     "favoritecount": "0",     "commentcount": "412998"    }   }  ] 

how parse variables? example extract view count $viewcount.

you can use json_decode() parse data:

$data = json_decode($input, true);  foreach($data["items"] $item) {     echo $item["statistics"]["viewcount"]; } 

note ‘items‘ property array, have 0, 1 or more results. why iteration necessary (you $viewcount = $data['items'][0]['statistics']['viewcount']; if you're sure there 1 result, wouldn't recommend though).


Comments

Popular posts from this blog

dns - How To Use Custom Nameserver On Free Cloudflare? -

python - Pygame screen.blit not working -

c# - Web API response xml language -