使用 cURL php 抓取图像


Grabbing image with cURL php

尝试使用 cURL 将图像保存到我的服务器。 图像似乎正在下载。 它显示正确的字节,但是当我链接图像不起作用时。 然后我DL看到,不,这是一个空白的图像。

这是我的代码...它有什么问题?

$ch = curl_init("'. $image .'");
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("$rename.jpg",'w');
fwrite($fp, $rawdata); 
fclose($fp);

我测试了你的脚本,它对我来说工作正常,只需删除无用的双引号和点$image即可。

<?
$image ="http://cdn.sstatic.net/stackoverflow/img/sprites.png?v=5";
$rename="123";
$ch = curl_init($image);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
$rawdata=curl_exec ($ch);
curl_close ($ch);
$fp = fopen("$rename.jpg",'w');
fwrite($fp, $rawdata); 
fclose($fp);
?>