Get XML Attribute with SimpleXML

后端 未结 3 1326
予麋鹿
予麋鹿 2020-12-20 06:23

I\'m trying to get the $xml->entry->yt:statistics->attributes()->viewCount attribute, and I\'ve tried some stuff with SimpleXML, and I can\'t really

3条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-20 07:10

    If you just want to get the viewcount of a youtube video then you have to specify the video ID. The youtube ID is found in each video url. For example "http://www.youtube.com/watch?v=ccI-MugndOU" so the id is ccI-MugndOU. In order to get the viewcount then try the code below

    $sample_video_ID = "ccI-MugndOU";
    $JSON = file_get_contents("http://gdata.youtube.com/feeds/api/videos?q={$sample_video_ID}&alt=json");
    $JSON_Data = json_decode($JSON);
    $views = $JSON_Data->{'feed'}->{'entry'}[0]->{'yt$statistics'}->{'viewCount'};
        echo $views;
    

提交回复
热议问题