greenbandit
greenbandit

Reputation: 2275

get correct headers with curl

currently im using this code to get the headers:

get_headers( $url, 1 ); 

wondering how i can do with curl (more faster); this is my curl code:

$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_exec($curl);
$info= curl_getinfo($curl);
curl_close($curl);

but i got "another" headers, i need the 'Location' header to see where go a bit.ly url or another redirect service.

Thanks in Advance.

Upvotes: 0

Views: 875

Answers (2)

Charles
Charles

Reputation: 51411

You want to turn the CURLOPT_FOLLOWLOCATION option is set to true.

edit:

Whoops, looks like you also need CURLOPT_RETURNTRANSFER to get the content back from curl_exec and CURLOPT_HEADER to make sure headers are included in that.

Upvotes: 1

alex
alex

Reputation: 490123

If you want to extract the Location header, use this regex...

preg_match_all('/^Location: (?P<location>.*?)$/m', $headers, $matches);

var_dump($matches['location'][0]);

Ideone.

If you want to stop cURL from following the Location header, check out Charles' answer.

Upvotes: 1

Related Questions