In PHP, sometimes I want to send an HTTP request to a remote site just to look at the response headers, so I declare it all manually and use the fsock_open()
function. However, this goes much slower than calling file_get_contents()
with a remote URL (which loads the whole page content). Why is this? Is there a good alternative way to get just the response headers (to check if a page returns a 404 error, for example) that works as fast as file_get_contents()
?
为什么file_get_contents()比使用fsock_open()更快?
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
1条回答 默认 最新
- dsa5211314 2010-12-22 01:48关注
Have you just tried using curl? You can get back the headers and also the content, if desired.
$ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($ch, CURLOPT_MAXREDIRS, 10); $useragent = "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.9.0.5) Gecko/ 2008120122 Firefox/3.0.5"; curl_setopt($ch, CURLOPT_USERAGENT, $useragent); $output = curl_exec($ch); $info = curl_getinfo($ch);
This will chase redirects, which isn't always desired.
Look at http://php.net/manual/en/function.curl-getinfo.php to see what you get back in
$info
.You can try just doing a HEAD request instead of a GET (the
CURLOPT_NOBODY
option), but I have run into problems with some servers that deny HEADs for some reason.解决 无用评论 打赏 举报
悬赏问题
- ¥15 使用ESP8266连接阿里云出现问题
- ¥15 被蓝屏搞吐了,有偿求帮解答,Ai回复直接拉黑
- ¥15 BP神经网络控制倒立摆
- ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
- ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
- ¥30 Unity接入微信SDK 无法开启摄像头
- ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
- ¥20 cad图纸,chx-3六轴码垛机器人
- ¥15 移动摄像头专网需要解vlan
- ¥20 access多表提取相同字段数据并合并