curl 和 ping - 如何检查网站是打开还是关闭?
我想使用 PHP 检查网站在特定实例上是否正常运行。我知道curl会获取文件的内容,但我不想阅读网站的内容。我只是想检查网站的状态。有什么办法可以检查网站的状态吗?我们可以使用 ping 来检查状态吗?对于我来说,从服务器获取状态信号(404、403 等)就足够了。一小段代码可能对我有很大帮助。
I want to check whether a website is up or down at a particular instance using PHP. I came to know that curl will fetch the contents of the file but I don't want to read the content of the website. I just want to check the status of the website. Is there any way to check the status of the site? Can we use ping to check the status? It is sufficient for me to get the status signals like (404, 403, etc) from the server. A small snippet of code might help me a lot.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
curl -Is $url
仅输出标头。grep HTTP
过滤 HTTP 响应标头。cut -d ' ' -f2
将输出修剪为第二个“单词”,在本例中为状态代码。例子:
curl -Is $url
outputs just the headers.grep HTTP
filters to the HTTP response header.cut -d ' ' -f2
trims the output to the second "word", in this case the status code.Example:
我是这样做的。我设置了用户代理以尽量减少目标禁止我的可能性,并且还禁用了 SSL 验证,因为我知道目标:
Here is how I did it. I set the user agent to minimize the chance of the target banning me and also disabled SSL verification since I know the target:
你见过 get_headers() 函数吗? http://it.php.net/manual/en/function.get -headers.php 。它似乎完全符合您的需要。
如果直接使用带有 -I 标志的curl,它将返回 HTTP 标头(404 等)而不是页面 HTML。在 PHP 中,等效项是
curl_setopt($ch, CURLOPT_NOBODY, 1);
选项。Have you seen the get_headers() function ? http://it.php.net/manual/en/function.get-headers.php . It seems to do exactly what you need.
If you use curl directly with the -I flag, it will return the HTTP headers (404 etc) instead of the page HTML. In PHP, the equivalent is the
curl_setopt($ch, CURLOPT_NOBODY, 1);
option.ping
不会做您正在寻找的事情 - 它只会告诉您机器是否已启动(并响应ping
)。但这并不一定意味着网络服务器已经启动。您可能想尝试使用 http_head 方法 - 它会检索网络服务器发回给您的标头。如果服务器发回标头,那么您就知道它已启动并正在运行。
ping
won't do what you're looking for - it will only tell you if the machine is up (and responding toping
). That doesn't necessarily mean that the webserver is up, though.You might want to try using the http_head method - it'll retrieve the headers that the webserver sends back to you. If the server is sending back headers, then you know it's up and running.
您无法使用
ping
测试网络服务器,因为它是不同的服务。服务器可能会运行,但网络服务器守护程序可能会崩溃。所以curl是你的朋友。忽略内容即可。You can not test a webserver with
ping
, because its a different service. The server may running, but the webserver-daemon may be crashed anyway. So curl is your friend. Just ignore the content.该函数检查 URL 是否存在。检查的时间最长为 300 毫秒,但您可以在 cURL 选项
CURLOPT_TIMEOUT_MS
中更改该参数。您可以检查任何 URL,从域、IP 地址到图像、文件等。我认为这是最快的方法并且被证明是有用的。
This function checks whether a URL exists or not. The time of the check is a maximum of 300ms, but you can change that parameter within the cURL option
CURLOPT_TIMEOUT_MS
You can check any URL, from domain, ip address to images, files, etc. I think this is the fastest way and proven useful.
像这样的东西应该有效
something like this should work