An example of how to use PHP to check whether a website is down

墨辰丷
Release: 2023-03-26 18:22:02
Original
1322 people have browsed it

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 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";
?>
Copy after login

Running results: Website OK

Related recommendations:

PHP check Whether the website is down Example code

Use python batchCheck the availability of the website

Example on how to check in php whether the website is down

##

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

Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template