零贰'Blog

零贰'Blog - 记录日常点点滴滴..........

判断一个网站和网页是否能打开

代码编程php
[阅读原文]()

方法一:

<?php 
$web_url = 'http://192.168.88.1';
$ch = curl_init($web_url);  
curl_setopt($ch, CURLOPT_HEADER, 0);  
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);   
$contents = curl_exec($ch);
curl_close($ch);
if($contents === false){
    echo('打不开!');
}else{
    echo('能打开!');
} 
?>
 

方法二:

<?php
//如果显示为200则正常,如果显示其它值表示不正常;$timeout后面的3是设置超时秒数。
 function httpcode($url){
  $ch = curl_init();
  $timeout = 3;
  curl_setopt($ch,CURLOPT_FOLLOWLOCATION,1);
  curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
  curl_setopt($ch, CURLOPT_HEADER, 1);
  curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
  curl_setopt($ch,CURLOPT_URL,$url);
  curl_exec($ch);
  return $httpcode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
  curl_close($ch);
}
echo "111:".httpcode('http://192.168.88.1');
?>