Article Detail

PHP写一个函数来判断这个网站是否能正常打开

方法1, $url = ‘http://www.lizhenqiu.com'; $ch = curl_init(); $timeout = 10; curl_s...

PHP 阅读 39.4万 3 分钟阅读 2015-12-03 10:36
GDScript3
53 行
方法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>
Comments 评论区
广西南宁市 2015-12-03 10:38

$ch = curl_init ();
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);

| | #0
广西南宁市 2015-12-03 10:39

<?php
$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';
}?>

| | #0
Comment Form 留下评论
正在回复 #0
粘贴图片、拖拽文件,或点上面的按钮上传 图片会自动插入 [img] 标签,其他附件会自动插入 [attach] 标签。
正在上传...
提交前会先拉起旧项目同款第三方人机验证。

不再要求填写昵称;reply 会生成一条带楼层回链的新评论,quote 会附带完整引用块,并保持评论锚点跳转。

lizhenqiu blog is powered by lizhenqiu.com Version 6.9

Processed in 0.0085 second(s) W3C

本博客的所有原创作品采用 知识共享 署名-非商业性使用-相同方式共享 2.5 协议 进行许可

本站由 七七牛 云存储 阿阿里云 计算与安全服务 拍又拍云 CDN 加速 百百度智能 AAMH 布布集网 AI指南针AI

桂公网安备 45010302000998号 桂ICP备15007619号-1 中国互联网举报中心 建议使用谷歌浏览器浏览
Navigation 文章导航
⌂ ↓ ↑
100%
图片预览
Gallery 图集
0 张图片

正在整理正文和评论里的图片。

当前页还没有可展示的图片。
Quick Comment 快速评论

直接输入内容,提交时仍会走当前页面的人机验证。

正在上传...
操作提示