0

I've written some code to get a file via curl. I only want the headers and not the actual file itself

$info = curl_init() or die('error 1');
    curl_setopt($info, CURLOPT_RETURNTRANSFER, 1);
    //curl_setopt($info, CURLOPT_PORT , 8089);
    curl_setopt($info, CURLOPT_URL, $url);
    curl_setopt($info, CURLOPT_HEADER,true); 
    curl_setopt($info, CURLOPT_CONNECTTIMEOUT, 30);
        curl_setopt($info, CURLOPT_NOBODY, true);
        //curl_setopt($info, CURLOPT_SSL_VERIFYPEER, 0);
        curl_exec($info);
        if(!curl_errno($info)){
            $response = curl_getinfo($info);
            echo "<pre>";
            print_r(get_headers($response));
            echo "</pre>";
        }else{
            echo "error!";
            echo "<br>" . curl_error($info);
        }

However the response returned by this doesn't contain any of the information that should be in a header - e.g filename

array(26) {
  ["url"]=>
  string(55) "https://www.filepicker.io/api/file/CjDfxG0WSmGiY3O2eKDE"
  ["content_type"]=>
  string(9) "image/png"
  ["http_code"]=>
  int(200)
  ["header_size"]=>
  int(840)
  ["request_size"]=>
  int(86)


["filetime"]=>
  int(-1)
  ["ssl_verify_result"]=>
  int(0)
  ["redirect_count"]=>
  int(0)
  ["total_time"]=>
  float(1.578048)
  ["namelookup_time"]=>
  float(0.000494)
  ["connect_time"]=>
  float(0.026931)
  ["pretransfer_time"]=>
  float(0.13615)
  ["size_upload"]=>
  float(0)
  ["size_download"]=>
  float(0)
  ["speed_download"]=>
  float(0)
  ["speed_upload"]=>
  float(0)
  ["download_content_length"]=>
  float(965985)
  ["upload_content_length"]=>
  float(0)
  ["starttransfer_time"]=>
  float(1.578002)
  ["redirect_time"]=>
  float(0)
  ["certinfo"]=>
  array(0) {
  }
  ["primary_ip"]=>
  string(11) "79.125.4.68"
  ["primary_port"]=>
  int(443)
  ["local_ip"]=>
  string(11) "192.168.0.9"
  ["local_port"]=>
  int(53950)
  ["redirect_url"]=>
  string(0) ""
}

So how can I get the actual header itself?

  • `curl_getinfo` is not the same as a HEAD request. See http://stackoverflow.com/questions/1378915/header-only-retrieval-in-php-via-curl – Joe Nov 17 '13 at 14:45
  • @Joe Thanks for linking to that, but so much of the code there is irrelevant I'm having problems picking through it. All I want is the filename from the header. Could you, if possible, tell me the code I need to do this? Thanks! –  Nov 17 '13 at 15:09
  • Yes: don't use `curl_getinfo`. That's not returning info from the call and getting you the headers from the curl call. You need to actually call curl issuing a `HEAD` request and get those headers. – Joe Nov 17 '13 at 15:12
  • But how do I call curl using a head request? I told it to get the headers and not the body. Is that not enough? –  Nov 17 '13 at 15:16
  • It's in the post I referenced, look at the first answer (the code for `setData()`) does exactly this by using CURLOPT_NOBODY. – Joe Nov 17 '13 at 15:20
  • @Joe I've got NOBODY set to true, as shown in the code –  Nov 17 '13 at 15:23
  • 1
    The header is the result from `curl_exec`, which you are discarding. The data you want isn't from curl_getinfo, it's from curl_exec. If you read the linked post and actually looked at the code you would see that. – Joe Nov 17 '13 at 15:26

1 Answers1

0

The reason this was happening is because I was discarding the input of curl_exec. I changed that section of the code to:

$headers=curl_exec($info);
  • @shihon Unfortunatley I do not have enough rep to instantly do this and must wait 2 days. –  Nov 17 '13 at 17:16