This article mainly and we introduce PHP check whether the site is down, combined with specific examples of PHP based on a curl session of the Web site to check the status of the relevant operation skills, the need for friends can refer to, hope to help everyone.
<?phpfunction Networkcheck ($url) {$agent = "mozilla/5.0 (Windows NT 6.1; WOW64; rv:42.0) gecko/20100101 firefox/42.0 "; curl_init-Initializes a Curl session $ch =curl_init (); Curl_setopt-set session parameters for a 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-performs a curl session $page =curl_exec ($ch); Curl_getinfo-gets the information for a Curl connection resource handle $httpcode = Curl_getinfo ($ch, Curlinfo_http_code); The function of the Curl_close () function is to close a curl session, and the only argument 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 site to be inspected if (Networkcheck ("https://www.baidu.com")) echo "Website OK"; else echo "Website down";?
Run Result: Website OK
Related recommendations:
Shell script for service outage monitoring automatic restart