0

0

SAE域名绑定设置服务器宕机时自动修改A记录并飞信通知

php中文网

php中文网

发布时间:2016-05-25 16:40:31

|

2754人浏览过

|

来源于php中文网

原创

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的二级域名。并设置回调密钥。

黑灰色风格企业网站源码1.0_20250213
黑灰色风格企业网站源码1.0_20250213

hdhcms网站支持PC、手机版,同时后台支持公众号的接入,包括微信服务号订阅号,可以设置自动回复及服务号菜单及认证订阅号菜单。 1、网站上线方法: 1.1本网站运行环境为:IIS6.5+SQLITE 1.2将网站解压到网站目录 1.3数据库默认为SQLITE,包括在解压目录内,无须修改 1.4 完成上面的配置后通过所绑定的域名即可运行2网址访问及后台访问配置

下载

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 <= $list; $i++) {
            $uri = '/im/index/contactlistView.action';
            $data = 'idContactList=' . $i . '&type=group';
            $result = $this->_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 <= $page; $j++) {
                $uri = '/im/index/contactlistView.action';
                $data = 'idContactList=' . $i . '&page=' . $j;
                $result = $this->_postWithCookie($uri, $data);
                preg_match_all('/(.*?)/si', $result, $matches);
                if (!$matches[1][0]) {
                    break;
                }
                for ($x = 0; $x <= 9; $x++) {
                    if (!$matches[1][$x]) {
                        continue;
                    }
                    $shuchu.= $matches[1][$x] . " " . str_replace(" ", "", $matches[3][$x]) . "n";
                }
            }
        }
        return $shuchu;
    }
    public function getyou() {
        $list = $this->getgroup();
        for ($i = 0; $i <= $list; $i++) {
            $uri = '/im/index/contactlistView.action';
            $data = 'idContactList=' . $i . '&type=group';
            $result = $this->_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 <= $page; $j++) {
                $uri = '/im/index/contactlistView.action';
                $data = 'idContactList=' . $i . '&page=' . $j;
                $result = $this->_postWithCookie($uri, $data);
                preg_match_all('/(.*?)/si', $result, $matches);
                if (!$matches[1][0]) {
                    break;
                }
                for ($x = 0; $x <= 9; $x++) {
                    if (!$matches[1][$x]) {
                        continue;
                    }
                    $shuchu.= $matches[1][$x] . " " . str_replace(" ", "", $matches[3][$x]) . "n";
                }
            }
        }
        return $shuchu;
    }
    /** 
     * 携带Cookie向f.10086.cn发送POST请求
     * @param string $uri
     * @param string $data
     */
    protected function _postWithCookie($uri, $data) {
        $fp = fsockopen('f.10086.cn', 80);
        fputs($fp, "POST $uri HTTP/1.1rn");
        fputs($fp, "Host: f.10086.cnrn");
        fputs($fp, "Cookie: {$this->_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;
    }
}
?>


本站声明:本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn

热门AI工具

更多
DeepSeek
DeepSeek

幻方量化公司旗下的开源大模型平台

豆包大模型
豆包大模型

字节跳动自主研发的一系列大型语言模型

通义千问
通义千问

阿里巴巴推出的全能AI助手

腾讯元宝
腾讯元宝

腾讯混元平台推出的AI助手

文心一言
文心一言

文心一言是百度开发的AI聊天机器人,通过对话可以生成各种形式的内容。

讯飞写作
讯飞写作

基于讯飞星火大模型的AI写作工具,可以快速生成新闻稿件、品宣文案、工作总结、心得体会等各种文文稿

即梦AI
即梦AI

一站式AI创作平台,免费AI图片和视频生成。

ChatGPT
ChatGPT

最最强大的AI聊天机器人程序,ChatGPT不单是聊天机器人,还能进行撰写邮件、视频脚本、文案、翻译、代码等任务。

相关专题

更多
Python 序列化
Python 序列化

本专题整合了python序列化、反序列化相关内容,阅读专题下面的文章了解更多详细内容。

0

2026.02.02

AO3官网入口与中文阅读设置 AO3网页版使用与访问
AO3官网入口与中文阅读设置 AO3网页版使用与访问

本专题围绕 Archive of Our Own(AO3)官网入口展开,系统整理 AO3 最新可用官网地址、网页版访问方式、正确打开链接的方法,并详细讲解 AO3 中文界面设置、阅读语言切换及基础使用流程,帮助用户稳定访问 AO3 官网,高效完成中文阅读与作品浏览。

91

2026.02.02

主流快递单号查询入口 实时物流进度一站式追踪专题
主流快递单号查询入口 实时物流进度一站式追踪专题

本专题聚合极兔快递、京东快递、中通快递、圆通快递、韵达快递等主流物流平台的单号查询与运单追踪内容,重点解决单号查询、手机号查物流、官网入口直达、包裹进度实时追踪等高频问题,帮助用户快速获取最新物流状态,提升查件效率与使用体验。

27

2026.02.02

Golang WebAssembly(WASM)开发入门
Golang WebAssembly(WASM)开发入门

本专题系统讲解 Golang 在 WebAssembly(WASM)开发中的实践方法,涵盖 WASM 基础原理、Go 编译到 WASM 的流程、与 JavaScript 的交互方式、性能与体积优化,以及典型应用场景(如前端计算、跨平台模块)。帮助开发者掌握 Go 在新一代 Web 技术栈中的应用能力。

11

2026.02.02

PHP Swoole 高性能服务开发
PHP Swoole 高性能服务开发

本专题聚焦 PHP Swoole 扩展在高性能服务端开发中的应用,系统讲解协程模型、异步IO、TCP/HTTP/WebSocket服务器、进程与任务管理、常驻内存架构设计。通过实战案例,帮助开发者掌握 使用 PHP 构建高并发、低延迟服务端应用的工程化能力。

5

2026.02.02

Java JNI 与本地代码交互实战
Java JNI 与本地代码交互实战

本专题系统讲解 Java 通过 JNI 调用 C/C++ 本地代码的核心机制,涵盖 JNI 基本原理、数据类型映射、内存管理、异常处理、性能优化策略以及典型应用场景(如高性能计算、底层库封装)。通过实战示例,帮助开发者掌握 Java 与本地代码混合开发的完整流程。

5

2026.02.02

go语言 注释编码
go语言 注释编码

本专题整合了go语言注释、注释规范等等内容,阅读专题下面的文章了解更多详细内容。

62

2026.01.31

go语言 math包
go语言 math包

本专题整合了go语言math包相关内容,阅读专题下面的文章了解更多详细内容。

55

2026.01.31

go语言输入函数
go语言输入函数

本专题整合了go语言输入相关教程内容,阅读专题下面的文章了解更多详细内容。

27

2026.01.31

热门下载

更多
网站特效
/
网站源码
/
网站素材
/
前端模板

精品课程

更多
相关推荐
/
热门推荐
/
最新课程
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号