Home  >  Article  >  Backend Development  >  PHP example code to check whether the website is down

PHP example code to check whether the website is down

小云云
小云云Original
2018-02-01 13:55:162076browse

This article mainly introduces the method of PHP to check whether the website is down. It analyzes the related operation skills of PHP based on the curl session to check the website status based on specific examples. Friends in need can refer to it. I hope it can help everyone.

<?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";
?>

Run result: Website OK

Related recommendations:

SHELL script implements service downtime monitoring and automatic restart

The above is the detailed content of PHP example code to check whether the website is down. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn