Home >Backend Development >PHP Tutorial >PHP 检测因特网址是否能正常打开代码

PHP 检测因特网址是否能正常打开代码

WBOY
WBOYOriginal
2016-06-13 12:29:111138browse

PHP 检测网址是否能正常打开代码

这是一个检测网址是否能正常打开的PHP代码,通过下面的代码检测一个网址是否能正常访问,如果正常则会返回http状态码200的值,如果为其它则不正常;这个代码我们可以用到很多地方,例如缓存友情链接的ICO图标时就可以用到,缓存时先检测网站是否正常,如果正常就缓存ICO图标,否则调用一个默认的图标文件。

<meta charset="utf-8"><?php 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);  }?>使用方法:<?php echo httpcode('http://www.onestopweb.cn');?> 如果显示为200则正常,如果显示其它值表示不正常;$timeout后面的3是设置超时秒数。

?

?效果图:

?

?

?

?

?

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