使用curl获取正确的标题
目前我使用此代码来获取标题:
get_headers( $url, 1 );
想知道如何使用curl(更快);这是我的curl代码:
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_exec($curl);
$info= curl_getinfo($curl);
curl_close($curl);
但是我得到了“另一个”标头,我需要“Location”标头来查看bit.ly url或另一个重定向服务的去向。
提前致谢。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您想提取
Location
标头,请使用此正则表达式...Ideone。
如果您想阻止 cURL 跟随
Location
标头,请查看 查尔斯的回答。If you want to extract the
Location
header, use this regex...Ideone.
If you want to stop cURL from following the
Location
header, check out Charles' answer.您想要打开
CURLOPT_FOLLOWLOCATION
选项 设置为 true。编辑:
哎呀,看起来您还需要
CURLOPT_RETURNTRANSFER
从curl_exec
获取内容,并CURLOPT_HEADER
以确保标头包含在其中。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 fromcurl_exec
andCURLOPT_HEADER
to make sure headers are included in that.