1

i trying use skrypt from 6 hours How to get number of video views with YouTube API?

<?php
$video_ID = 'your-video-ID';
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$views = $JSON_Data->{'entry'}->{'yt$statistics'}->{'viewCount'};
echo $views;
?>

why it don't work?

$JSON_Data->{'entry'}->{'media$group'}->{'media$category'}->{'$t'};

the code to decode:

"media$category":[{"$t":"Entertainment","label":"Rozrywka","scheme":"http://gdata.youtube.com/schemas/2007/categories.cat"}],

I think the problem is at "[" "]", how should look like? Thank you very much

Community
  • 1
  • 1
mario
  • 25
  • 1
  • 6

1 Answers1

0

media$category is array format so you can use {'media$category'}[0]

Code in file_get_contents

$video_ID = 'BM7FWtADD0s';
$url =  "https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json";
$JSON = file_get_contents("https://gdata.youtube.com/feeds/api/videos/{$video_ID}?v=2&alt=json");
$JSON_Data = json_decode($JSON);
$category = $JSON_Data->{'entry'}->{'media$group'}->{'media$category'}[0]->{'$t'};
echo $category;

Output : Entertainment

Code in curl

      $c = curl_init();
      curl_setopt_array($c, array(
        CURLOPT_URL => $url,
        CURLOPT_HTTPHEADER => array('Content-type: application/json'),
        CURLOPT_TIMEOUT => 10,
        CURLOPT_RETURNTRANSFER => true,
        CURLOPT_SSL_VERIFYPEER =>false
    ));
    $json = curl_exec($c);

    curl_close($c);
    $JSON_Data = json_decode($json);
    echo "<pre>";
    print_r($JSON_Data->{'entry'}->{'media$group'}->{'media$category'}[0]->{'$t'});

Output : Entertainment

Padmanathan J
  • 4,614
  • 5
  • 37
  • 75