sae域名绑定之后,一般是用cname方式将域名绑定到应用中。但是有时候我们需要用到a记录(比如说根域名,虽然在dnspod上可以设置cname记录,但很可能会影响到mx记录),而sae的ip地址经常改变,ping应用二级域名得到的ip没多久就失效了(前些天网站因此几天打不开都没发现,我用的是教育网,自己能打开,但是电信线路变了)。还好dnspod有个功能叫d监控,可以帮你监控网站能否正常打开。如果发现宕机,dnspod会用邮件、短信、微信等方式提醒你。这里用到的是它的另一个通知方式,那就是url回调通过dnspod 提供的d监控 url 回调功能,您可以让宕机或恢复信息提交到您指定的 url 上,从而更加灵活地处理各种通知信息。
我们可以通过宕机之后的url回调取得相关参数,并通过dnspod api实现自动修改记录的功能,再通过飞信发送宕机通知。
代码在后面,先说设置方法:
1.点此下载代码,修改其中的参数为你自己的。
2.将代码上传到网站。
3.在dnspod开启d监控,在通知设置中回调url一栏填入monitorcallback.php的地址,如http://blog.gimhoy.com/monitorcallback.php?rhost=hipic.sinaapp.com.其中rhost是sae的二级域名。并设置回调密钥。
4.enjoy~ dnspod-monitor-callback
monitorcallback.php,代码如下:
$domain_id, 'record_id' => $record_id, 'sub_domain' => $sub_domain, 'record_type' => 'a', 'record_line' => $record_line, 'ttl' => '600', 'value' => $newip ); $dnspod = new dnspod(); $response = $dnspod->api_call('record.modify', $data); if (isset($response['status']['code']) && $response['status']['code'] == 1) { $msg = $msg . $newip . '(已切换)'; } else { $msg = $msg . $newip . '(切换失败,错误代码' . $response['status']['code'] . ')'; } } //飞信通知 require_once 'fetion.class.php'; $fetion = new phpfetion($fetionnum, $fetionpwd); $result = $fetion->send($mobilenum, $msg); if (strpos($result, '短信发送成功!') || strpos($result, '发送消息成功!')) { $r = 成功。; } else { $r = 失败。; } $s = new saestorage(); $content = $s->read($logstordomain, $logname); $content = $content . $msg . '。飞信通知' . $r . ' '; //开源代码phprm.com $s->write($logstordomain, $logname, $content); // 处理完成 echo 'done';}dnspod . class . php/* * dnspod api php web 示例 * http://www.phprm.com/ * * copyright 2011, kexian li * released under the mit, bsd, and gpl licenses. **/class dnspod { public function api_call($api, $data) { if ($api == '' || !is_array($data)) { exit('内部错误:参数错误'); } $api = 'https://dnsapi.cn/' . $api; $data = array_merge($data, array( 'login_email' => 'dnspod登陆账号', 'login_password' => 'dnspod登陆密码', 'format' => 'json', 'lang' => 'cn', 'error_on_empty' => 'yes' )); $result = $this->post_data($api, $data); if (!$result) { exit('内部错误:调用失败'); } $results = @json_decode($result, 1); if (!is_array($results)) { exit('内部错误:返回错误'); } if ($results['status']['code'] != 1) { exit($results['status']['message']); } return $results; } private function post_data($url, $data) { if ($url == '' || !is_array($data)) { return false; } $ch = @curl_init(); if (!$ch) { exit('内部错误:服务器不支持curl'); } curl_setopt($ch, curlopt_url, $url); curl_setopt($ch, curlopt_post, 1); curl_setopt($ch, curlopt_header, 0); curl_setopt($ch, curlopt_returntransfer, 1); curl_setopt($ch, curlopt_ssl_verifypeer, 0); curl_setopt($ch, curlopt_ssl_verifyhost, 0); curl_setopt($ch, curlopt_postfields, http_build_query($data)); curl_setopt($ch, curlopt_useragent, 'gimhoy monitor/1.0 (contact@gimhoy.com)'); $result = curl_exec($ch); curl_close($ch); return $result; }}?>
fetion.class.php,代码如下:
_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('/.*?rnset-cookie: (.*?);.*?/si', $result, $matches); if (isset($matches[1])) { $this->_cookie = implode('; ', $matches[1]); } $result = $this->_postwithcookie('/im/login/cklogin.action', ''); return $result; } /** * 获取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 $mobile 手机号(接收者) * @param string $message 短信内容 * @return string */ public function send($mobile, $message) { if ($message === '') { return ''; } // 判断是给自己发还是给好友发 if ($mobile === $this->_mobile) { return $this->_tomyself($message); } else if (strlen($mobile) === 11) { $uid = $this->_getuid($mobile); } else { $uid = $mobile; } return $uid === '' ? $this->_addfriend($mobile) : $this->_touid($uid, $message); } protected function _getname() { $uri = '/im/index/index.action'; $result = $this->_postwithcookie($uri, '#'); // 匹配 preg_match('/(.*?)/si', $result, $matches); return $matches[2]; } /* * 通过手机号增加好友 * @param string $number 手机号(要加的好友手机) * @param string $nickname 你的名字,出现在对方的验证短信里 * @param string $buddylist 分组,默认为空 * @param string $localname 好友屏显名 * @return string */ protected function _addfriend($number) { $uri = '/im/user/insertfriendsubmit.action'; $data = 'nickname=' . urlencode($this->_getname()) . '&buddylist=1&localname=&number=' . $number . '&type=0'; $result = $this->_postwithcookie($uri, $data); return $result; } /** * 获取飞信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]; } /** * 向好友发送飞信 * @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; } protected function getgroup() { $uri = '/im/index/index.action'; $data = 'type=group'; $result = $this->_postwithcookie($uri, $data); // 匹配 preg_match_all('/contactlistview.action?idcontactlist=(d+)/si', $result, $matches); foreach ($matches[1] as $k => $v) { if ($k == 0) { $min = $v; $max = $v; } else if ($v != 9998 && $v != 9999) { $min = min($min, $v); $max = max($max, $v); } } return $max; } public function getyou1() { $list = $this->getgroup(); for ($i = 0; $i _postwithcookie($uri, $data); preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn); if (!$listn[2]) { continue; } $shuchu.= str_replace( , , $listn[2]) . ( . $listn[4] . )n; preg_match('/共(d+)页/si', $result, $zpage); preg_match('/共(d+)页/si', $result, $dpage); isset($zpage[1]) ? $page = $zpage[1] : $page = $dpage[4]; for ($j = 1; $j _postwithcookie($uri, $data); preg_match_all('/(.*?)/si', $result, $matches); if (!$matches[1][0]) { break; } for ($x = 0; $x getgroup(); for ($i = 0; $i _postwithcookie($uri, $data); preg_match('/(.*?)|(.*?)((.*?)/(.*?))/si', $result, $listn); if (!$listn[2]) { continue; } $shuchu.= str_replace( , , $listn[2]) . ( . $listn[4] . )n; preg_match('/共(d+)页/si', $result, $zpage); preg_match('/共(d+)页/si', $result, $dpage); isset($zpage[1]) ? $page = $zpage[1] : $page = $dpage[4]; for ($j = 1; $j _postwithcookie($uri, $data); preg_match_all('/(.*?)/si', $result, $matches); if (!$matches[1][0]) { break; } for ($x = 0; $x _cookie}rn); fputs($fp, content-type: application/x-www-form-urlencodedrn); fputs($fp, user-agent: mozilla/5.0 (windows nt 5.1; rv:14.0) gecko/20100101 firefox/14.0.1rn); fputs($fp, content-length: . strlen($data) . rn); fputs($fp, connection: closernrn); fputs($fp, $data); $result = ''; while (!feof($fp)) { $result.= fgets($fp); } fclose($fp); return $result; }}?>