PHP写一个函数来判断这个网站是否能正常打开
发布时间:2015-12-03, 10:36:30 分类:PHP | 编辑 off 网址 | 辅助
正文 1197字数 383,632阅读
方法1,
$url = ‘http://www.lizhenqiu.com';
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
if(false == $contents)
{
echo ‘Curl error: ‘ . curl_error($ch);
}
else
{
….
}
方法2
curl_getinfo($ch, CURLINFO_HTTP_CODE);
方法3
<?php
ini_set('default_socket_timeout', 3); /*超时控制(3秒)*/
$url = '这里填对地址就pass,错就Timeout';
if($data = file_get_contents($url)) {
echo 'Pass';
}else {
echo 'Timeout';
}
//End_php
方法4(js)
<script language= "javascript">
function getURL(url) {
var xmlhttp = new ActiveXObject( "Microsoft.XMLHTTP");
xmlhttp.open("GET", url, false);
xmlhttp.send();
if(xmlhttp.readyState==4) {
if(xmlhttp.Status != 200) alert("不存在");
return xmlhttp.Status==200;
}
return false;
}
</script>
<a href= "http://www.lizhenqiu.com " onclick= "return getURL(this.href) "> csdn </a>
Run code
Cut to clipboard
(支付宝)给作者钱财以资鼓励 (微信)→
有过 2 条评论 »
curl_setopt($ch, CURLOPT_URL, 'http://www.google.com.hk');
curl_setopt($ch, CURLOPT_TIMEOUT, 200);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_NOBODY, FALSE);
#curl_setopt( $ch, CURLOPT_POSTFIELDS, "username=".$username."&password=".$password );
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, FALSE);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'GET');
curl_exec($ch);
$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
$url = '42.121.59.156';
$ch = curl_init();
$timeout = 10;
curl_setopt ($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt ($ch, CURLOPT_CONNECTTIMEOUT, $timeout);
$contents = curl_exec($ch);
if(false == $contents || curl_getinfo($ch,CURLINFO_HTTP_CODE)!=200)
{
echo $url.' Curl error: ' . curl_getinfo($ch,CURLINFO_HTTP_CODE).curl_error($ch);
}
else
{
echo 'ok';
}?>