I'm trying to get a YouTube channel's video views and it works perfect on localhost but I'm getting this error on my public web server:
Warning: file_get_contents(https://www.googleapis.com/youtube/v3/channels?part=statistics&id={MY YOUTUBE USER ID}&fields=items%2Fstatistics&key={MY API KEY}): failed to open stream: HTTP request failed! HTTP/1.0 403 Forbidden in /var/www/*****.net/public_html/dashboard/inc/youtube-api.php on line 9 Notice: Trying to get property of non-object in /var/www/*****.net/public_html/dashboard/inc/youtube-api.php on line 10 Notice: Trying to get property of non-object in /var/www/*****.net/public_html/dashboard/inc/youtube-api.php on line 10 Notice: Trying to get property of non-object in /var/www/*****./public_html/dashboard/inc/youtube-api.php on line 10
Here's the code I'm using:
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
function getYouTubeStats($stat)
{
$channel = json_decode(file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=statistics&id={my youtube user id}&fields=items%2Fstatistics&key={MY API KEY}"));
return $channel->items[0]->statistics->videoCount;
}
echo getYouTubeStats($stat = null);
?>