0

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?

Nathaniel Ford
  • 20,545
  • 20
  • 91
  • 102
Chris Toxz
  • 13
  • 9

2 Answers2

0

Google has some magic in place to block automated requests, hotlinking, etc. If their watchdog algorithms have flagged activity from your IP, they could be blocking you.

You might try using cURL functions and setting the User-Agent string to something reasonable for a modern browser. Otherwise, I'd suggest rethinking your logic for the app.

Source: personal experience; I can't use Google to automatically determine the nature of 2nd-hand office machines for one of our applications.

Kevin_Kinsey
  • 2,285
  • 1
  • 22
  • 23
0

Google has some magic in place to block automated requests, hotlinking, etc. If their watchdog algorithms have flagged activity from your IP, they could be blocking you.

But why dont dont block me for the custom avatars, and only for the standard avatar ?

and when i do:

<img src="http://i.ytimg.com/i/MJJbPVnNlvRAWSgfVuzxJA/1.jpg" />

its show correctly the image. So google is not blocking the ip of my server..

Chris Toxz
  • 13
  • 9