Using cURL for a HEAD request

software development

#1

I can’t figure out what I’m doing wrong here. The code block is intended to take the URL contained in $url, send a HEAD request and then print the header that comes back.

For this test, I set $url=“http://www.google.com”.

$ch = curl_init();
$timeout = 5;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt( $ch, CURLOPT_CUSTOMREQUEST,“HEAD”);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$head=curl_exec($ch);
var_dump($head);
curl_close($ch);

curl_exec only returns true - even though it should return the entire contents since I set RETURNTRANSFER to 1.

Any ideas?


#2

I actually got this solved at another place now, so if anyone else needs to do this, here’s how:

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_NOBODY, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

$r = curl_exec($ch);
var_dump($r);

I haven’t figured out why CUSTOMREQUEST “HEAD” doesn’t cut it, but HEADER and NOBODY combined appear to do the same.