2012-12-18 27 views
2

我正在使用cURL檢索圖像,重命名並將其存儲在本地。 的圖像出現爲0字節的文件,不管了,我是否使用捲曲,像這樣:cURL-ed圖像保留0字節

$strImageUrl = curl_init($strImageUrlSource); 
$fp = fopen($strTargetImage, 'wb'); 
curl_setopt($strImageUrl, CURLOPT_FILE, $fp); 
curl_setopt($strImageUrl, CURLOPT_HEADER, 0); 
curl_exec($strImageUrl); 
curl_close($strImageUrl); 
fclose($fp); 

或file_put /獲取。像這樣:

file_put_contents($strImageName, file_get_contents($strImageUrlSource)); 

我檢索的URL爲:

< IMG SRC = 'HTTP://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg' 寬度= 「150」height =「112」alt =「Wondecla,可根據要求提供地址」title =「Wondecla,可根據要求提供地址」/>

我可以手動保存這張圖片。 當Firefox中的屬性看它顯示三個條目:

data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAFgAAABYBAMAAACDuy0HAAAAG1BMVEX+/v4BAQH///8KCgoDAwN/f3/19fWAgID8... etc 

http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg

data:image/png;base64,iVBORw0KG ... etc. 

我在做什麼錯在這裏?

+0

有可能是超時,重定向,熱鏈接保護或這種性質的東西。您可能想要使用其他捲曲選項允許重定向等。請參閱我的解決方案http://stackoverflow.com/a/18927806/722796 –

+0

順便說一句,這會刪除Linux中所有大小爲零的圖像: find/home/somewhere/images -type f -size 0 -exec rm {} \; –

回答

1

這工作:

使用的file_get_contents

$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg'; 

$imageName = pathinfo($image, PATHINFO_BASENAME); 

file_put_contents($imageName, file_get_contents($image)); 

使用curl

$image = 'http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg'; 

$imageName = pathinfo($image, PATHINFO_BASENAME); 

$ch = curl_init(); 

curl_setopt($ch, CURLOPT_URL, $image); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 

$source = curl_exec($ch); 
curl_close($ch); 

file_put_contents($imageName, $source); 

希望這有助於。

0

使用var_dump()進行調試。你看到了什麼,當你

var_dump(file_get_contents('http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg'));