2012-12-18 29 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'));