curl and ping - how to check whether a website is either up or down?

后端 未结 7 2156
再見小時候
再見小時候 2020-11-28 12:51

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 th

7条回答
  •  庸人自扰
    2020-11-28 13:11

    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
        }
    

提交回复
热议问题