title: 判断一个网站和网页是否能打开
author: Love02xp
date: 2019-02-27 19:45:47
category: [编程代码]
tags: [代码,编程,php]
draft: false
cover:
preview: php判断一个网站和网页是否能打开,缺点有很大延时
##### [阅读原文]()
<!--more-->
#### 方法一:
<?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');
?>