我的卷曲中有这个代码: $ data_string = json_encode($ data);
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($ch, CURLOPT_POSTFIELDS, $data_string);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json; charset=utf-8',
'User-Agent: '.$_SERVER['HTTP_USER_AGENT'],
'X-Forwarded-For: '.$this->getIp()
));
$result = curl_exec($ch);
return $result;
我得到了这个圆滑的结果:
我期待一个图片文件。
问题是我如何下载此图像文件?
答案 0 :(得分:5)
您可以使用此代码
$profile_Image = $url; //image url
$userImage = 'myimg.jpg'; // renaming image
$path = ''; // your saving path
$ch = curl_init($profile_Image);
$fp = fopen($path . $userImage, 'wb');
curl_setopt($ch, CURLOPT_FILE, $fp);
curl_setopt($ch, CURLOPT_HEADER, 0);
$result = curl_exec($ch);
curl_close($ch);
fclose($fp);
答案 1 :(得分:2)
你可以试试这个:
$data=send_curl($url);
$path='./test.jpg';
$file = file_put_contents($path,$data);
send_curl的功能
function send_curl($url, $data, $PROXY = "") {
$ch = curl_init ();
if ($PROXY != "") {
curl_setopt ( $ch, CURLOPT_PROXY, $PROXY );
}
curl_setopt ( $ch, CURLOPT_URL, $url );
curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
curl_setopt ( $ch, CURLOPT_POST, 1 );
curl_setopt ( $ch, CURLOPT_POSTFIELDS, $data );
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, FALSE);
$result = curl_exec ( $ch );
curl_close ( $ch );
return $result;
}
答案 2 :(得分:0)
理想情况下,浏览器决定如何使用Content-Type
HTTP标头的信息显示给定的内容,PHP可以使用header()函数生成HTTP标头。
如果您确定您的照片将始终采用JPEG格式,则相当简单:
header('Content-Type: image/jpeg');
echo your_download_function();
exit;
否则,您需要获取远程服务器返回的标头。 Curl有一个棘手的语法 - 我引用你PHP cURL retrieving response headers AND body in a single request?