Home >Backend Development >PHP Tutorial >Example of how to check if a website is down in PHP
This article mainly introduces the method of PHP to check whether the website is down, and analyzes the relevant operating skills of PHP based on the curl session to check the website status based on specific examples. Friends in need can refer to it
The example in this article describes how to use PHP to check whether the website is down. Share it with everyone for your reference, the details are as follows:
<?php function Networkcheck($url){ $agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) Gecko/20100101 Firefox/42.0"; //curl_init-初始化一个curl会话 $ch=curl_init(); //curl_setopt — 为一个curl设置会话参数 curl_setopt($ch, CURLOPT_URL,$url ); curl_setopt($ch, CURLOPT_USERAGENT, $agent); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch,CURLOPT_VERBOSE,false); curl_setopt($ch, CURLOPT_TIMEOUT, 5); curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt($ch,CURLOPT_SSLVERSION,3); curl_setopt($ch,CURLOPT_SSL_VERIFYHOST, FALSE); //curl_exec —执行一个curl会话 $page=curl_exec($ch); //curl_getinfo — 获取一个curl连接资源句柄的信息 $httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE); //curl_close()函数的作用是关闭一个curl会话,唯一的参数是curl_init()函数返回的句柄。 curl_close($ch); if($httpcode>=200 && $httpcode<300) return true; else return false; } //函数参数为要检查的网站的网址路径 if(Networkcheck("https://www.baidu.com")) echo "Website OK"; else echo "Website DOWN"; ?>
Running results: Website OK
The above is the detailed content of Example of how to check if a website is down in PHP. For more information, please follow other related articles on the PHP Chinese website!