我正在使用twitter api訪問某些帳戶詳細信息。它已經明確表示,你可以輕鬆地獲得Twitter帳戶圖片:如何將Twitter帳戶圖像複製到服務器?
http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg
然而,當我嘗試在PHP這個映像複製()到我的本地服務器這是行不通的。看來你甚至不能ping通地址爲'找不到主機'。
如何將Twitter帳戶圖像複製到本地服務器?
我正在使用twitter api訪問某些帳戶詳細信息。它已經明確表示,你可以輕鬆地獲得Twitter帳戶圖片:如何將Twitter帳戶圖像複製到服務器?
http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg
然而,當我嘗試在PHP這個映像複製()到我的本地服務器這是行不通的。看來你甚至不能ping通地址爲'找不到主機'。
如何將Twitter帳戶圖像複製到本地服務器?
$twitsImg ='http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg';
$twit=file_get_contents($twitsImg);
$new_img = basename($twitsImg);
file_put_contents($new_img,$twit);
您所遇到的問題是,PHP ini文件變量allow_url_fopen
被關閉,因爲它應該是。你需要使用cURL來獲取文件。默認情況下,cURL通常安裝在Web服務器上。此示例適用於我的網站:
<?php
function save_image($img,$path){
$ch = curl_init($img);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$data=curl_exec($ch);
curl_close ($ch);
if(file_exists($path)){
unlink($path);
}
$fp = fopen($path,'x');
fwrite($fp, $data);
fclose($fp);
}
save_image("http://a0.twimg.com/profile_images/1260994338/P3030586-2_bigger.jpg","up/file2.jpg");
?>
此外,請確保您在目標文件夾中設置了正確的CHMOD(775)。