使用 cURL php 抓取图像

2022-01-09 00:00:00 image save upload curl php

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

Trying to save image to my server using cURL. The image appears to download. It shows the correct bytes but when i link image does not work. I then DL to see and nope its a blank image.

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

here is my code... whats the issue with it?

$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 的无用双引号和点.

I test your script it work fine for me, just remove the useless double quote and dot for $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);
?>

相关文章