本帖最后由 lazypeople 于 2012-12-5 09:58 编辑
众所周知,在做互联网这块,需要一个报警系统,例如你在外面K歌的时候网站挂了,你可能还在玩的很high,那也不能总是一直坐在电脑前盯着。所以需要架设一个监控,监控报警应该分两个方面,首先需要搭建一个发送邮件,发送短信的接口,根据故障的级别选择是发短信还是发邮件。邮件这块相对比较简单,在sae上就可以自己创建一个发送邮件的接口。本文主要讲述一个如何在“省钱”的前提下给自己发送短信。
前提就是你是中国移动用户,且使用了飞信业务,当然这个业务也是不要钱的。直接上代码(我集中到自己的系统中了,项目是勇敢的lazyphp框架 http:///lazyphp/):
调用飞信的基类: 这个文件放在(/function/下)
- <?php
- /**
- * PHP飞信发送类
- *
- * @author quanhengzhuang <blog.quanhz.com>
- * @version 1.5.0
- */
- class PHPFetion
- {
-
- /**
- * 发送者手机号
- * @var string
- */
- protected $_mobile;
-
- /**
- * 飞信密码
- * @var string
- */
- protected $_password;
-
- /**
- * Cookie字符串
- * @var string
- */
- protected $_cookie = '';
-
- /**
- * Uid缓存
- * @var array
- */
- protected $_uids = array();
-
- /**
- * csrfToken
- * @var string
- */
- protected $_csrfToten = null;
-
- /**
- * 构造函数
- * @param string $mobile 手机号(登录者)
- * @param string $password 飞信密码
- */
- public function __construct($mobile, $password)
- {
- if ($mobile === '' || $password === '')
- {
- return;
- }
-
- $this->_mobile = $mobile;
- $this->_password = $password;
-
- $this->_login();
- }
-
- /**
- * 析构函数
- */
- public function __destruct()
- {
- $this->_logout();
- }
-
- /**
- * 登录
- * @return string
- */
- protected function _login()
- {
- $uri = '/huc/user/space/login.do?m=submit&fr=space';
- $data = 'mobilenum='.$this->_mobile.'&password='.urlencode($this->_password);
-
- $result = $this->_postWithCookie($uri, $data);
-
- //解析Cookie
- preg_match_all('/.*?\r\nSet-Cookie: (.*?);.*?/si', $result, $matches);
- if (isset($matches[1]))
- {
- $this->_cookie = implode('; ', $matches[1]);
- }
-
- $result = $this->_postWithCookie('/im/login/cklogin.action', '');
-
- return $result;
- }
-
- /**
- * 向指定的手机号发送飞信
- * @param string $mobile 手机号(接收者)
- * @param string $message 短信内容
- * @return string
- */
- public function send($mobile, $message)
- {
- if ($message === '')
- {
- return '';
- }
-
- //判断是给自己发还是给好友发
- if ($mobile == $this->_mobile)
- {
- return $this->_toMyself($message);
- }
- else
- {
- $uid = $this->_getUid($mobile);
-
- return $uid === '' ? '' : $this->_toUid($uid, $message);
- }
- }
-
- /**
- * 获取飞信ID
- * @param string $mobile 手机号
- * @return string
- */
- protected function _getUid($mobile)
- {
- if (emptyempty($this->_uids[$mobile]))
- {
- $uri = '/im/index/searchOtherInfoList.action';
- $data = 'searchText='.$mobile;
-
- $result = $this->_postWithCookie($uri, $data);
-
- //匹配
- preg_match('/toinputMsg\.action\?touserid=(\d+)/si', $result, $matches);
-
- $this->_uids[$mobile] = isset($matches[1]) ? $matches[1] : '';
- }
-
- return $this->_uids[$mobile];
- }
-
- /**
- * 获取csrfToken,给好友发飞信时需要这个字段
- * @param string $uid 飞信ID
- * @return string
- */
- protected function _getCsrfToken($uid)
- {
- if ($this->_csrfToten === null)
- {
- $uri = '/im/chat/toinputMsg.action?touserid='.$uid;
-
- $result = $this->_postWithCookie($uri, '');
-
- preg_match('/name="csrfToken".*?value="(.*?)"/', $result, $matches);
-
- $this->_csrfToten = isset($matches[1]) ? $matches[1] : '';
- }
-
- return $this->_csrfToten;
- }
-
- /**
- * 向好友发送飞信
- * @param string $uid 飞信ID
- * @param string $message 短信内容
- * @return string
- */
- protected function _toUid($uid, $message)
- {
- $uri = '/im/chat/sendMsg.action?touserid='.$uid;
- $csrfToken = $this->_getCsrfToken($uid);
- $data = 'msg='.urlencode($message).'&csrfToken='.$csrfToken;
-
- $result = $this->_postWithCookie($uri, $data);
-
- return $result;
- }
-
- /**
- * 给自己发飞信
- * @param string $message
- * @return string
- */
- protected function _toMyself($message)
- {
- $uri = '/im/user/sendMsgToMyselfs.action';
- $result = $this->_postWithCookie($uri, 'msg='.urlencode($message));
-
- return $result;
- }
-
- /**
- * 退出飞信
- * @return string
- */
- protected function _logout()
- {
- $uri = '/im/index/logoutsubmit.action';
- $result = $this->_postWithCookie($uri, '');
-
- return $result;
- }
-
- /**
- * 携带Cookie向f.10086.cn发送POST请求
- * @param string $uri
- * @param string $data
- */
- protected function _postWithCookie($uri, $data)
- {
- $fp = fsockopen('f.10086.cn', 80);
- fputs($fp, "\"static/image/smiley/default/titter.gif\"" smilieid="\"9\"" alt="\"\"" border="\"0\"">OST $uri HTTP/1.1\r\n");
- fputs($fp, "Host: f.10086.cn\r\n");
- fputs($fp, "Cookie: {$this->_cookie}\r\n");
- fputs($fp, "Content-Type: application/x-www-form-urlencoded\r\n");
- fputs($fp, "User-Agent: Mozilla/5.0 (Windows NT 5.1; rv:14.0) Gecko/20100101 Firefox/14.0.1\r\n");
- fputs($fp, "Content-Length: ".strlen($data)."\r\n");
- fputs($fp, "Connection: close\r\n\r\n");
- fputs($fp, $data);
-
- $result = '';
- while (!feof($fp))
- {
- $result .= fgets($fp);
- }
-
- fclose($fp);
-
- return $result;
- }
-
- }
自定义封装接口部分:
- <?php
- if( !defined('IN') ) die('bad request');
- include_once( AROOT . 'controller'.DS.'app.class.php' );
- include_once( AROOT . 'controller'.DS.'function.class.php' );
- include_once( AROOT . 'function'.DS.'phpfetion.php' );
- @session_start();
- class messageController extends appController
- {
- function __construct()
- {
- parent::__construct();
- $this->username = '手机号';
- $this->password = '飞信密码';
- $this->fetion = new PHPFetion($this->username, $this->password);
- }
-
- function index()
- {
- $message = v('message');
- if($message == null) {
- die('fobbiden!');
- }
- $token = '81e6ec089e7690638bd074f0b977cf38';//定义一个token防止被滥用
- if(v('token') != $token) {
- die('fobbiden!');
- }
- $tel = v('phone');
- if($tel == null) {
- $tel = '你的手机号';
- }
- $result = $this->fetion->send($tel, $message);
- }
- }
哈,这时候就自己封装好了一个短信接口了,例如上述文件你部署在http://xxxx.sinaapp.com,那么在任意地方你想调用的时候只需要按照如下的格式发送一个post请求就可以了。封装一个函数
- private function send_message($data)
- {
- $url = 'http://xxxx.sinaapp.com/?c=message&token=81e6ec089e7690638bd074f0b977cf38';
- $post_data = $data;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- $output = curl_exec($ch);
- if ($output === FALSE) {
- echo "cURL Error: " . curl_error($ch);
- }
- curl_close($ch);
- }
调用:
- $tel = '你的手机号';
- $message = '晚安。';
- $post_data = array(
- 'message' => $message,
- 'phone' => $tel,
- );
- $this->send_message($post_data);
以上的部分就是一个免费的短信发送接口了,可以做很多事情,例如你定时的给你的女友发发晚安,或者给你的朋友定时发发天气预报,可以结合sae的cron服务定时的触发你想要干的任意事情。
下面给出一个监控部分的代码,当然这个部分现在需要一个可以执行shell的环境下才能执行,可以用sws实现,当然用php也可以模拟实现,例如设置一个curl超时时间,如果超时就触发~
要监控的网站按照如下的格式写到一个txt文件:(文件名 check.txt)
- www.changes.com.cn,独立域名绑定测试
- lazypeople.sinaapp.com,sae app monitor
- jp.cname.,sae海外域名绑定节点
扫描代码:
- <?php
- $fp_in = fopen('check.txt', "r");
- while (!feof($fp_in)) {
- $line = fgets($fp_in);
- $array = explode(",", $line);
- $host = $array[0];
- if($host != '') {
- $intro = $array[1];
- $shell = "ping -c 2 -W 1 ".$host;
- $out = shell_exec($shell);
- $out = trim($out);
- $out_array = explode(",", $out);
- $recieve = trim($out_array[1]);
- $num = explode(" ", $recieve);
- if($num[0] < 2) {
- //alert
- $message = $intro.' Status:down!Ping info:'.$out;
- $post_data = array(
- 'message' => $message,
- 'phone' => '你的手机号',
- );
- if($out != '')
- {
- send_message($post_data);
- echo $host.' status:bad!<br/>';
- }
- } else {
- echo $host.' statusk!<br/>';
- }
- }
- }
-
- function send_message($data)
- {
- $url = 'http://xxxx.sinaapp.com/?c=message&token=81e6ec089e7690638bd074f0b977cf38';
- $post_data = $data;
- $ch = curl_init();
- curl_setopt($ch, CURLOPT_URL, $url);
- curl_setopt($ch, CURLOPT_HEADER, 0);
- curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
- curl_setopt($ch, CURLOPT_POST, 1);
- curl_setopt($ch, CURLOPT_POSTFIELDS, $post_data);
- $output = curl_exec($ch);
- if ($output === FALSE) {
- echo "cURL Error: " . curl_error($ch);
- }
- curl_close($ch);
- }
命名为check.php,下面只需要加一条cron定时触发就行了。
- */2 * * * * root /usr/bin/curl -m 500 "http://你的地址/check.php" >> /var/lazy/monitor.txt
希望对大家有帮助~
ps:打个广告!如果不是移动用户怎么办,那就是sae的短信服务了,没有运营商限制,只要一毛一条,高效高到达~http://sae.sina.com.cn/?m=apistore&a=detail&service_code=AF&type=0
|