【问题标题】:Grabbing image with cURL php使用 cURL php 抓取图像
【发布时间】:2012-06-05 01:54:13
【问题描述】:

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

【问题讨论】:

  • 尝试使用 true 和 false 而不是 0 和 1。我不认为会有所作为,但它可能会有所作为。另外,如果您在 curl_close($ch) 语句之后执行 var_export(strlen($rawdata)) 会发生什么?我只是想弄清楚问题是否与图像检索或之后的存储有关。

标签: php image curl upload save


【解决方案1】:

我测试了你的脚本,它对我来说很好,只需删除 $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);
?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2012-04-14
    • 2017-09-21
    • 2015-10-11
    • 1970-01-01
    • 2014-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多