curl和ping - 如何检查网站是向上还是向下? [英] curl and ping - how to check whether a website is either up or down?

查看:614
本文介绍了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.

推荐答案

这样的工作应该可以工作

something like this should work

    $url = 'yoururl';
    $ch = curl_init($url);
    curl_setopt($ch, CURLOPT_NOBODY, true);
    curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
    curl_exec($ch);
    $retcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
    curl_close($ch);
    if (200==$retcode) {
        // All's well
    } else {
        // not so much
    }

这篇关于curl和ping - 如何检查网站是向上还是向下?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆