PHP fsockopen() 非常慢
我正在使用 fsockopen() 调用列表中的多个连接来查看各种 ip/主机和端口的在线状态...
<?php
$socket = @fsockopen($row[2], $row[3], $errnum, $errstr, 1);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online';}
fclose($socket);
如果有效,我不会抱怨这一点,但我有大约 15 个 ip/端口我正在列表中检索(php for() 命令..)。我想知道是否有更好的方法来做到这一点?这条路很慢!?!服务器大约需要 1-2 分钟才能返回所有内容的响应。
更新:
<?php
$socket = @fsockopen("lounge.local", "80", $errnum, $errstr, 30);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online'; }
?>
它将显示在列表中:“ReadyNAS AFP Readynas.local:548 online”
我不知道还有什么可以告诉你吗?加载结果集合需要很长时间......
I'm using fsockopen() to call a number of connections in a list to see the online status of various ip/host and ports ...
<?php
$socket = @fsockopen($row[2], $row[3], $errnum, $errstr, 1);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online';}
fclose($socket);
if works, I'm not complaining about that, but I have approximately 15 ip/ports that i'm retrieving in a list (php for() command..). I was wondering if there is a better way to do this? This way is VERY slow!?! It is taking about 1-2 minutes for the server to come back with a response for all of them..
Update:
<?php
$socket = @fsockopen("lounge.local", "80", $errnum, $errstr, 30);
if ($errnum >= 1) { $status = 'offline'; } else { $status = 'online'; }
?>
It will display in a list: "ReadyNAS AFP readynas.local:548 online"
I don't know what more I can tell you? It just takes forever to load the collection of results...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
根据我自己的经验:
此代码:
相比,速度较慢
与在 PHP 5.4 中测试的代码 。如果同时进行多个连接,可以保留主机解析结果并重新使用它,以进一步减少脚本执行时间,例如:
From my own experience:
This code:
is slower compared to:
Tested in PHP 5.4. If doing many connections at the same time one could keep host resolution result and re-use it, to further reduce script time execution, for example:
如果您打算“ping”某个 URL,我建议使用curl 来执行此操作,为什么?你可以使用curl并行发送ping,看看这个-> http://www.php.net/manual/en/function .curl-multi-init.php。在之前的项目中,它应该向我们的服务器提供实时数据,我们用来 ping 主机以查看它们是否还活着,而 Curl 是唯一对我们有帮助的选择。
这是一个建议,可能不是解决您问题的正确方法。
If you plan to "ping" some URL, I would advise doing it with curl, why? you can use curl to send pings in parallel, have a look at this -> http://www.php.net/manual/en/function.curl-multi-init.php. In a previous project, it was supposed to feed Real Time Data to our server, we used to ping hosts to see if they are alive or not and Curl was the only option that helped us.
Its an advice, may not be a right solution for your problem.
fsockopen()
的最后一个参数是超时,将其设置为一个较低的值以使脚本更快完成,如下所示:The last parameter to
fsockopen()
is the timeout, set this to a low value to make the script complete faster, like this:您是否比较过
fsockopen(servername)
与fsockopen(ip-address)
的结果?如果超时参数没有改变任何事情,则问题可能出在您的名称服务器中。如果使用 IP 地址的 fsockopen 速度更快,则必须修复名称服务器,或将域添加到 /etc/hosts 文件中。Have you compared the results of
fsockopen(servername)
versusfsockopen(ip-address)
? If the timeout parameter does not change a thing, the problem may be in your name server. If fsockopen with an IP address is faster, you'll have to fix your name server, or add the domains to /etc/hosts file.我建议做一些不同的事情。
将此主机放入数据库的表中,如下所示:
并将状态检查部分移至 cron 脚本中,每 5 分钟或您想要的频率运行一次。
该脚本将检查主机:端口并更新每条记录的状态和时间戳,在您的页面中,您只需执行数据库查询并显示主机、其状态以及上次检查的时间(例如:1分钟前等... )
这样你的页面就会加载得很快。
I would recommend doing this a bit different.
Put this hosts in a table in a database something like:
And move the status checking part in a cron script that you run it once every 5 minutes or how often you want.
This script will check the host:port and update status and timestamp for each record and in your page you will just do a db query and show the host, its status and when was last checked (something like: 1minute ago, etc...)
This way your page will load fast.
根据php 手册,有一个超时参数。尝试将其设置为较低的值。
编辑:要添加丹尼尔的答案,nmap 可能是最好使用的工具。使用 cron 作业将其设置为每 X 分钟扫描并更新您的记录。类似的东西
According to the php manual, there's a timeout parameter. Try setting it to a lower value.
Edit: To add to Daniel's answer, nmap might be the best tool to use. Set it up with a cron job to scan and update your records every X minutes. Something like
我遇到了一个问题,fsockopen 请求很慢,但 wget 确实很快。就我而言,发生这种情况是因为主机名同时具有 ipv4 和 ipv6 地址,但 ipv6 已关闭。因此,每次 ipv6 请求需要 20 秒左右才会超时。
I had an issue where fsockopen requests were slow, but wget was really snappy. In my case, it was happening because the hostname had both an ipv4 and ipv6 address, but ipv6 was down. So it took 20 or so seconds on each request for the ipv6 to time out.