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

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

WBOY
Release: 2016-06-13 12:29:11
Original
1149 people have browsed it

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是设置超时秒数。
Copy after login

?

?效果图:

?

?

?

?

?

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