title: php download a file with curl that returns binary data
body: <p>Im trying to hit a url that returns binary data and use to save the file. When i try using file_put_contents the file gets generated but the contents are empty or file says its damaged.</p>
  
  <pre><code> $url_call = 'http://someurl.com/file/15646';
  
   $ch=curl_init();   
   curl_setopt($ch, CURLOPT_URL, $url_call);
   curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
   curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
   curl_setopt($ch, CURLOPT_BINARYTRANSFER,1);
   curl_setopt($ch, CURLOPT_HEADER,0); 
   $response = curl_exec($ch);
   curl_close($ch);
  
  
   file_put_contents('/data/testingdirect.pdf', $response);
  </code></pre>
  
  <p>I know theres many questions out there similar and iv looked through all of them and tried them but they all return the same result a damaged or empty file.</p>
  
  <p>I know the file is good because I can actually access that file myself directly on the server and see that its good.</p>
  
  <p>Also is there a way for me to determine the file type or content type to know how to save the fil either pdf or jpeg or whatever else.?</p>
  
  <p>REWROTE THE CURL TO BE THIS:</p>
  
  <pre><code> $fp = fopen ('/data/testingdirect.jpg', 'w+');
   $ch=curl_init();   
   curl_setopt($ch, CURLOPT_FILE, $fp);
   curl_setopt($ch, CURLOPT_URL, $url_call);
   curl_setopt($ch, CURLOPT_HEADER,0);
  
  $response = curl_exec($ch);
  curl_close($ch);
  fclose($fp);
  </code></pre>
  
  <p>and same thing is still happening. </p>