1

我尝试从 6 小时开始使用 skrypt如何使用 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;
?>

为什么它不起作用?

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

要解码的代码:

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

我认为问题出在“[”“]”处,应该是什么样子?非常感谢

4

1 回答 1

0

media$category是数组格式,所以你可以使用{'media$category'}[0]

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;

输出 :Entertainment

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'});

输出 :Entertainment

于 2013-09-18T07:07:43.880 回答