使用 PHP 解析 fsockopen 中的 HTTP 标头?
我设置了一个脚本,出于必要的原因,它既获取 HTTP 响应标头,又获取使用 fsock 的 GET 请求的内容。
function checkUrl($host,$url,$port) {
$fp = fsockopen($host, $port, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET $url HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$response = fgets($fp, 1024);
print(substr($response,9,3));
}
fclose($fp);
}
}
如果我简单地全部回显,我会调用它并获取所有正确的数据。但实际上我需要从函数返回的只是 HTTP 状态代码。
即 404 或 200 或 301 等
但是上面的代码确实给出了错误代码,但最后却有一堆乱码,当我限制为 3 个字符时我不明白!
例如,
404, 2BM_n: Encype HThe tp-me=srcsrclanstaPre> lanmg=[0][1][2][3][4][5][6][7][8][9][10[11[12 swt.i> ypeeleamiize#99eco#66ade#33izeine#CCize { #66izeeig tmardespath=th=th=th=th=th=th=spardeolordeignign bocol widwidwid col bler> td Sorabl> e> rdeolordespath=th=th= bo spardeoloe="lanSen>
这让我相信我的响应实际上比字符串更复杂,对吧?标头有什么特别的地方,还是我误解了 fgets 的工作原理?非常感谢任何帮助
I have a script set up that for reasons of necessity gets both the HTTP Response header and then content of a GET request using fsock.
function checkUrl($host,$url,$port) {
$fp = fsockopen($host, $port, $errno, $errstr, 10);
if (!$fp) {
echo "$errstr ($errno)<br />\n";
} else {
$out = "GET $url HTTP/1.1\r\n";
$out .= "Host: $host\r\n";
$out .= "Connection: Close\r\n\r\n";
fwrite($fp, $out);
while (!feof($fp)) {
$response = fgets($fp, 1024);
print(substr($response,9,3));
}
fclose($fp);
}
}
I call it and get all the proper data back if I simply echo it all out. But actually all I need to return from the function is the HTTP STATUS Code.
i.e. 404 or 200 or 301 etc
But the code above gives the error code sure, but then with a load of gibberish at the end which I don't understand when I have limited to 3 chars!
e.g.
404, 2BM_n: Encype HThe tp-me=srcsrclanstaPre> lanmg=[0][1][2][3][4][5][6][7][8][9][10[11[12 swt.i> ypeeleamiize#99eco#66ade#33izeine#CCize { #66izeeig tmardespath=th=th=th=th=th=th=spardeolordeignign bocol widwidwid col bler> td Sorabl> e> rdeolordespath=th=th= bo spardeoloe="lanSen>
Which leads me to believe that my response is actually more complex than just a string right? Something special with the header or am I misunderstanding how fgets is working? Any help much appreciated
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
目前,您正在迭代标题/响应的每一行,即使您只对第一行感兴趣。请尝试以下操作:
如果您只对标头感兴趣而不是响应,我建议您发出
HEAD
请求,而不是GET
请求。除非有真正令人信服的理由不这样做,否则我建议您使用
CURL
发出此请求,而不是尝试处理 PHP 应用程序逻辑中的所有低级内容。At the moment you're iterating over each line of the header/response even though you're only interested in the first line. Try this instead:
If you're only interested in the headers and not the response I'd suggest that you make a
HEAD
request instead of aGET
one.Unless there's a really compelling reason not to I'd suggest that you make this request using
CURL
rather than trying to handle all of the low level stuff within your PHP app logic.问题是您正在打印每个 1024 个字符块的子字符串,而不仅仅是第一个。解决办法是不做循环。更改为:
甚至只是这样
,真的,因为您只需要前 13 个字符,而不是前 1024 个:
The problem is you are printing out that substring for every block of 1024 characters instead of just the first. The solution is to not do the loop. Change this:
To just this:
Or even just this, really, since you only need the first 13 characters, not the first 1024: