cURL-ed图像保持0字节

时间:2012-12-18 07:57:12

标签: php curl

我正在使用cURL来检索图像,重命名并在本地存储它。 无论我是否使用cURL,图像都会显示为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 / get。像这样:

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

我正在检索的网址是:

  

< img src ='http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg'width =“150”height =“112”alt =“Wondecla,地址可根据要求提供” =“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.

我在这里做错了什么?

2 个答案:

答案 0 :(得分:2)

这有效:

使用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 );

希望这有帮助。

答案 1 :(得分:0)

使用var_dump()进行调试。

时你看到了什么?
var_dump(file_get_contents('http://i1.au.reastatic.net/150x112/73fa6c02a92d60a76320d0e89dfbc1a36a6e46c818f74772dec65bae6959c62f/main.jpg'));