2016-01-18 50 views
0

在我的腳本中,我使用Google YouTube API從YouTube頻道接收頭像圖像。每個頭像都被接收並保存到我們自己的服務器上,除了「標準」頭像外。PHP文件獲取內容給出404錯誤,但存在

This is a screen shot of the standard avatar

我試圖用file_get_contents保存它們,但它給了我一個錯誤。

代碼:

$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"); 

    } 
} 

與自定義頭像渠道,它保存到我們的服務器,只不是標準化身。這是錯誤:

file_get_contents(http://i.ytimg.com/i/MJJbPVnNlvRAWSgfVuzxJA/1.jpg): failed to open stream: HTTP request failed! HTTP/1.0 404 Not Found

錯誤中的圖像鏈接確實存在。

有人明白嗎?

+0

請參閱http://stackoverflow.com/questions/1975461/how-to-get-file-get-contents-work-with-https –

+0

我不明白,威廉。 –

回答

0

谷歌有一些魔術可以阻止自動請求,盜鏈等。如果他們的看門狗算法從你的IP標記了活動,他們可能會阻止你。

您可以嘗試使用cURL函數並將User-Agent字符串設置爲適合現代瀏覽器的某些內容。否則,我會建議重新考慮你的應用邏輯。

來源:個人經驗;我無法使用Google爲我們的某個應用程序自動確定二手辦公設備的性質。

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.

但是爲什麼不要阻止我的自定義頭像,只爲標準頭像?

,當我做:

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

其正確顯示圖像。所以谷歌是不會阻止我的服務器的IP ..