开发者

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 "anothe开发者_开发百科r" headers, i need the 'Location' header to see where go a bit.ly url or another redirect service.

Thanks in Advance.


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.


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜