Keyfer Mathewson
Keyfer Mathewson

Reputation: 1075

Pulling YouTube viewCount

I'm trying to pull my viewcount from youtube through a php script and I'm having some issues.

My code is below. It doesn't seem to be working at all.

<?php
    $video_ID = '<?php echo(types_render_field("youtube-id", array())); ?>';
    $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;
?>

Thanks so much!

Upvotes: 0

Views: 178

Answers (1)

Ibu
Ibu

Reputation: 43810

I am not familiar with the api but i believe this is how it should be:

<?php
    $video_ID = types_render_field("youtube-id", array());
    $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;
?>

You had passed a string as the video id, see my changes

Upvotes: 1

Related Questions