cURL不工作但file_put_contents()呢?

Ang*_*elo 0 php curl

我在cURL中有一个代码,应该将图像从URL复制到我的服务器:

$curl = curl_init( $url );
$file = fopen( $imageURL , 'wb' );
curl_setopt( $curl , CURLOPT_FILE , $file );
curl_setopt( $curl , CURLOPT_HEADER , true );
curl_setopt( $curl , CURLOPT_FOLLOWLOCATION , true );
curl_exec( $curl );
curl_close( $curl );
fclose( $file );
Run Code Online (Sandbox Code Playgroud)

它不能正常工作,但file_put_contents()确实如此.我的cURL代码有问题吗?

Jon*_*han 5

有多种解决方案,cURL可能不是最好的.

$remote_img = 'http://www.somwhere.com/images/image.jpg';
$img = imagecreatefromjpeg($remote_img);
$path = 'images/';
imagejpeg($img, $path);
Run Code Online (Sandbox Code Playgroud)

可以很好地工作,但如果你在cURL上设置,试试这个:

$ch = curl_init ($img);
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);
if(file_exists($fullpath)){
    unlink($fullpath);
}
$fp = fopen($fullpath,'x');
fwrite($fp, $rawdata);
fclose($fp); 
Run Code Online (Sandbox Code Playgroud)

这也应该有效.

祝你好运!

  • @ shiplu.mokadd.im他没有提到它应该是 - 它是一个很好的功能来运行安全性,因为他抓住的东西在技术上可能是一个恶意脚本,如果用户输入它. (2认同)