In my script I am using a Google YouTube API to receive avatar images from a YouTube channel. Every avatar is received and saved to our own server, except the 'standard' avatars.
This is a screen shot of the standard avatar.
I am trying to save them with file_get_contents
, but it gives me an error.
The code:
$filename = './img/avatars/'. $ytname .'.jpg';
if (file_exists($filename)) {
print_r("http://youtube-top.eu/img/avatars/".$ytname.".jpg");
} else {
$url = file_get_contents("https://www.googleapis.com/youtube/v3/channels?part=brandingSettings,snippet&forUsername=".$ytname."&key=#KEYISPRIVATE#");
$json = json_decode($url, true);
$str = $json['items'][0]['snippet']['thumbnails']['default']['url'];
$str = str_replace('https://', 'http://', $str );
$content = file_get_contents($str);
?>
/*
<?php
echo $str;
?>
*/
<?php
file_put_contents('./img/avatars/'. $ytname . '.jpg', $content);
print_r("http://youtube-top.eu/img/avatars/".$ytname.".jpg");
}
}
With channels with a custom avatar, it saved to our server, only not for the standard avatars. This is the error:
file_get_contents(http://i.ytimg.com/i/MJJbPVnNlvRAWSgfVuzxJA/1.jpg): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found
The image link in the error does exist.
Does someone understand this?