-1

I have this code in my curl: $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;

I am getting this giberish result:

enter image description here

I am expecting an image file.

Question here is that how can i download this image file?

PinoyStackOverflower
  • 5,214
  • 18
  • 63
  • 126

3 Answers3

6

you can use this code

$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);
aniket ashtekar
  • 290
  • 1
  • 11
  • 1
    This works like a charm, if you want it to be downloaded automatically, you can add this code : $content = file_get_contents($path); $response = new Response(); $response->headers->set('Content-Type', 'mime/type'); $response->headers->set('Content-Disposition', 'attachment;filename=filename.pdf'); $response->setContent($content); return $response; – Brahim Belghmi Sep 03 '22 at 10:34
2

you can try this:

$data=send_curl($url);
$path='./test.jpg';
$file = file_put_contents($path,$data);

the function of 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;
}
gdreamlend
  • 108
  • 9
0

Ideally, browsers decide how to display a given piece of content using the information of the Content-Type HTTP header and PHP can generate HTTP headers with the header() function.

If you know for sure that your picture will always be in JPEG format it's rather straightforward:

header('Content-Type: image/jpeg');
echo your_download_function();
exit;

Otherwise, you need to fetch the header returned by the remote server. Curl has a tricky syntax—I refer you to PHP cURL retrieving response headers AND body in a single request?

Community
  • 1
  • 1
Álvaro González
  • 142,137
  • 41
  • 261
  • 360