Skip to content

Instantly share code, notes, and snippets.

@jamesrwhite
Created December 9, 2012 14:18
Show Gist options
  • Save jamesrwhite/4245135 to your computer and use it in GitHub Desktop.
Save jamesrwhite/4245135 to your computer and use it in GitHub Desktop.
cURL Headers
<?php
$curl = curl_init('http://123123123123123123123123213w.com');
curl_setopt_array($curl, array(
CURLOPT_HEADER => true,
CURLOPT_NOBODY => true,
CURLOPT_RETURNTRANSFER => true,
));
// Get the headers
$headers = curl_exec($curl);
// Split by new lines into an array
$headers = explode(PHP_EOL, $headers);
// Remove empty lines
$headers = array_filter($headers, function($value) {
return !empty($value) and !in_array($value, array("\r", "\n"));
});
print_r($headers);
@PatrickSocha
Copy link

Returns the following:

Array ( [0] => HTTP/1.1 200 OK [1] => Date: Sun, 09 Dec 2012 14:21:37 GMT [2] => Server: Apache [3] => Expires: Sun Dec 9 14:26:37 2012 GMT [4] => Cache-Control: private, max-age=300, must-revalidate [5] => P3P: CP="NON DSP COR DEVa PSAa PSDa OUR IND UNI COM", policyref="http://www.cnet.com/w3c/p3p.xml" [6] => Content-Type: text/html; charset=utf-8 )

@jamesrwhite
Copy link
Author

Weird. For me it's an empty array. I've tried from my laptop and vps as well.

@PatrickSocha
Copy link

May try reinstall php again, see if that solves the problem. Thanks for the help :-)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment