我在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代码有问题吗?
有多种解决方案,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)
这也应该有效.
祝你好运!
| 归档时间: |
|
| 查看次数: |
1765 次 |
| 最近记录: |