Example of how PHP checks whether a website is down
This example describes how to check whether a website is down in PHP. We will share this with you for your reference. The details are as follows:
<? Phpfunction Networkcheck ($ url) {$ agent = "Mozilla/5.0 (Windows NT 6.1; WOW64; rv: 42.0) Gecko/20100101 Firefox/42.0 "; // curl_init-initialize a curl session $ ch = curl_init (); // curl_setopt-sets the session parameter curl_setopt for a curl ($ ch, CURLOPT_URL, $ url ); curl_setopt ($ ch, CURLOPT_USERAGENT, $ agent); curl_setopt ($ ch, hour, 1); curl_setopt ($ ch, CURLOPT_VERBOSE, false); curl_setopt ($ ch, CURLOPT_TIMEOUT, 5); curl_set Opt ($ ch, CURLOPT_SSL_VERIFYPEER, FALSE); curl_setopt ($ ch, CURLOPT_SSLVERSION, 3); curl_setopt ($ ch, CURLOPT_SSL_VERIFYHOST, FALSE ); // curl_exec-execute a curl session $ page = curl_exec ($ ch); // curl_getinfo-get the information of a curl connection resource handle $ httpcode = curl_getinfo ($ ch, CURLINFO_HTTP_CODE ); // The function curl_close () is used to close a curl session. The unique parameter is the handle returned by the curl_init () function. Curl_close ($ ch); if ($ httpcode >=200 & $ httpcode <300) return true; else return false ;} // The function parameter is the URL path of the Website to be checked if (Networkcheck ("https://www.baidu.com") echo "Website OK"; else echo "Website DOWN";?>
Running result: Website OK