4

Consider the following PHP cURL command:

$url = 'http://bit.ly/faV1vd';

$_h = curl_init();
curl_setopt($_h, CURLOPT_HEADER, 1);
curl_setopt($_h, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($_h, CURLOPT_HTTPGET, 1);
curl_setopt($_h, CURLOPT_URL, $url);
curl_setopt($_h, CURLOPT_DNS_USE_GLOBAL_CACHE, false );
curl_setopt($_h, CURLOPT_DNS_CACHE_TIMEOUT, 2 );

$return = curl_exec($_h);

This returns:

HTTP/1.1 301 Moved
Server: nginx
Date: Sun, 29 Apr 2012 12:48:07 GMT
Content-Type: text/html; charset=utf-8
Connection: keep-alive
Set-Cookie: _bit=4f9d3887-00215-020af-2f1cf10a;domain=.bit.ly;expires=Fri Oct 26 12:48:07 2012;path=/; HttpOnly
Cache-control: private; max-age=90
Location: http://www.macroaxis.com/invest/market/VZ--Sat-Feb-26-06-16-35-CST-2011?utm_source=twitterfeed&utm_medium=twitter
MIME-Version: 1.0
Content-Length: 209

I want to split the header info into an array, as follows

[Status] => HTTP/1.1 301 Moved,
[Server] => nginx,
[Date] => Sun, 29 Apr 2012 12:48:07 GMT,
...
[Content-Length] => 209

So: - the first line (HTTP/1.1 301 Moved) should be the value of [Status] - all other header info should be split on :

I'm not succeeding in splitting the header info:

explode("\r\n\r\n", $return);
explode("\r\n", $return);

This doesn't split the header into an array (to further split on :, etc. as expected. What am I doing wrong?

4 Answers 4

7

The answer by Altaf Hussain is good but does not support a case where the header response contains a ':'. i.e. X-URL: http://something.com. In this case the $myarray will only contain ('X-URL' => 'http')

This can be fixed by adding the limit parameter and setting it to 2. In addition, there should be a space after the colon. So the full solution with the bug fix is:

$myarray=array();
$data=explode("\n",$return);

$myarray['status']=$data[0];

array_shift($data);

foreach($data as $part){
$middle=explode(": ",$part,2);
$myarray[trim($middle[0])] = trim($middle[1]);
}

print_r($myarray);
Sign up to request clarification or add additional context in comments.

2 Comments

Good! But you could replace ... = $data[0]; array_shift($data); with ... = array_shift($data);. This because array_shift returns the removed value.
Perhaps a neater solution: $data = explode("\n", 'Status: ' . $return); then remove the next two lines.
6

Use this to split your header into an array

$myarray = array();
$data = explode("\n",$return);

$myarray['status'] = $data[0];

array_shift($data);

foreach($data as $part){
     $middle = explode(":",$part);
     $myarray[trim($middle[0])] = trim($middle[1]);
}

print_r($myarray);

As well as use curl_setopt($_h, CURLOPT_NOBODY, 1); if you need to return only header.

More info can be found here

http://altafphp.blogspot.com/2012/04/get-http-headers-of-any-site-using-curl.html

Comments

2

cURL already supports a callback function for parsing the headers.

CURLOPT_HEADERFUNCTION : A callback accepting two parameters. The first is the cURL resource, the second is a string with the header data to be written. The header data must be written by this callback. Return the number of bytes written.

function handle_headers($curl, $header_line) 
{
    list($name, $value) = explode(": ", $header_line, 2);
    //do something with name/value...
    return strlen($header_line);
}

curl_setopt($curl, CURLOPT_HEADERFUNCTION, "handle_headers");

Comments

0

You could also just split it into maximum of 2

explode("\r\n\r\n",$result,2);

and remember to setup followlocation and max redirect if you want the actual url to fetch

curl_setopt($curl, CURLOPT_FOLLOWLOCATION, TRUE);
curl_setopt($curl, CURLOPT_MAXREDIRS, 5);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.