Chinaunix首页 | 论坛 | 博客
  • 博客访问: 162700
  • 博文数量: 46
  • 博客积分: 10
  • 博客等级: 民兵
  • 技术积分: 466
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-31 23:12
个人简介

知乎:http://www.zhihu.com/people/chanpinjinglizhilu

文章分类

全部博文(46)

分类: PHP

2015-01-12 14:06:54

php 检测网站是否能访问,网站状态检测。如果出现问题,邮件通知。

1.实现网站检测代码

点击(此处)折叠或打开

  1. /**
  2.  * Author: magic ysjheeqg#163.com
  3.  * Date: 15-1-2
  4.  * Time: 11:48
  5.  * 网站状态检测
  6.  */
  7. header("Content-type: text/html; charset=utf-8");
  8. include('functions.php');

  9. $email_config = array(
  10.     'smtp_host' => 'smtp.163.com',
  11.     'smtp_port' => '25',
  12.     'from_email' => 'ysjheeqg@163.com',
  13.     'from_name' => 'LaiKinsan',
  14.     'smtp_user' => 'yulongshuiji',
  15.     'smtp_pass' => '',
  16.     'reply_email' => 'ysjheeqg@163.com',
  17.     'reply_name' => 'LaiKinsan',
  18.     'email_arr' =>array(
  19.         '7878233@qq.com',
  20.         '8292389@qq.com',
  21.     ),
  22.     'test_email' => '777777@qq.com',
  23. );

  24. check_status("");


  25. //region 检测网站状态
  26. function check_status($url)
  27. {
  28.     global $email_config;
  29.     $ch = curl_init();
  30.     if (!$ch) {
  31.         die("没开curl模块功能");
  32.     }
  33.     curl_setopt($ch, CURLOPT_URL, $url);
  34.     curl_setopt($ch, CURLOPT_HEADER, 1); //获取http头信息
  35.     curl_setopt($ch, CURLOPT_NOBODY, 1); //不返回html的body信息
  36.     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); //返回数据流,不直接输出
  37.     curl_setopt($ch, CURLOPT_TIMEOUT, 30); //超时时长,单位秒
  38.     curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  39.     curl_exec($ch);
  40.     $info = curl_getinfo($ch);
  41.     curl_close($ch);
  42.     $string = $info['http_code'];

  43.     if ($string === 200) {
  44.         echo "ok";
  45.     } else {
  46.         foreach($email_config['email_arr'] as $k =>$v){
  47.             $res = send_mail($v, '', '网站问题通知邮件', '这是一封通知邮件,如果收到了说明网站访问有问题,问题网址'.$url, '', $email_config);
  48.             if ($res == 1) {
  49.                 echo "测试邮件已经发往你的邮箱";
  50.             } else {
  51.                 echo "测试邮件发送失败";
  52.             }
  53.         }

  54.     }
  55. }

2.服务器设置定时执行程序。


3.代码下载地址


阅读(1533) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~