动态IP解析阿里云域名

<?php
//使用这个代码前先在阿里云创建你的一级或二级域名等(没有在阿里云购买域名的先买一个)
//填写好 accessKeyId  accessSecrec  DomainName  RecordId  RR
date_default_timezone_set(“GMT”);
//绑定 ip 到域名
Ali::Obj()->UpdateDomainRecord();
class Ali
{
    private $accessKeyId  = “LTAI4GA3WTAVdVvxxxxxxxxxxxx”;//在阿里云里申请创建AccessKey管理 https://ram.console.aliyun.com/manage/ak
    private $accessSecrec = “SrDLneG73d8gVAdkwyzxxxxxxxxxxxx”;//在阿里云里申请AccessKey管理 https://ram.console.aliyun.com/manage/ak
    private static $obj  = null;
    public static function Obj ()
    {
        if(is_null(self::$obj))
        {
            self::$obj = new self();
        }
        return self::$obj;
    }
    public function DescribeDomainRecords()
    {
        $requestParams = array(
            “Action”    =>  “DescribeDomainRecords”,
            “DomainName”    =>  “dpjson.top”//填写你的阿里购买的一级域名
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val);
    }
    /**
     * 更新 ip
     */
    public function UpdateDomainRecord()
    {
        $ip = $this->ip();
        $requestParams = array(
            “Action”        =>  “UpdateDomainRecord”,
            “RecordId”      =>  “21070xxxxxxxxxx4576”,//打开浏览器F12进入开发模式后 鼠标选中左上角的箭头 箭头变成蓝色后选中你对应的域名 然后抓取data-row-key这个ID
            “RR”            =>  “test”,//填写你的域名一级或二级的主机记录,一级为@,二级随便起,这里我填的是test,解析成功后如test.xxx.xxx
            “Type”          =>  “A”,
            “Value”         =>  $ip,
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val.”  “.$ip);
    }
    private function requestAli($requestParams)
    {
        $publicParams = array(
            “Format”        =>  “JSON”,
            “Version”       =>  “2015-01-09”,
            “AccessKeyId”   =>  $this->accessKeyId,
            “Timestamp”     =>  date(“Y-m-d\TH:i:s\Z”),
            “SignatureMethod”   =>  “HMAC-SHA1”,
            “SignatureVersion”  =>  “1.0”,
            “SignatureNonce”    =>  substr(md5(rand(1,99999999)),rand(1,9),14),
        );
        $params = array_merge($publicParams, $requestParams);
        $params[‘Signature’] =  $this->sign($params, $this->accessSecrec);
        $uri = http_build_query($params);
        $url = ‘http://alidns.aliyuncs.com/?’.$uri;
        return $this->curl($url);
    }
    private function ip()
    {
        $ip = $this->curl(“http://httpbin.org/ip“);
        $ip = json_decode($ip,true);
        return $ip[‘origin’];
    }
    private function sign($params, $accessSecrec, $method=”GET”)
    {
        ksort($params);
        $stringToSign = strtoupper($method).’&’.$this->percentEncode(‘/’).’&’;
        $tmp = “”;
        foreach($params as $key=>$val){
            $tmp .= ‘&’.$this->percentEncode($key).’=’.$this->percentEncode($val);
        }
        $tmp = trim($tmp, ‘&’);
        $stringToSign = $stringToSign.$this->percentEncode($tmp);
        $key  = $accessSecrec.’&’;
        $hmac = hash_hmac(“sha1”, $stringToSign, $key, true);
        return base64_encode($hmac);
    }
    private function percentEncode($value=null)
    {
        $en = urlencode($value);
        $en = str_replace(“+”, “%20”, $en);
        $en = str_replace(“*”, “%2A”, $en);
        $en = str_replace(“%7E”, “~”, $en);
        return $en;
    }
    private function curl($url)
    {
        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL,$url );
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1 );
        $result=curl_exec ($ch);
        return $result;
    }
    private function outPut($msg)
    {
        echo date(“Y-m-d H:i:s”).”  “.$msg.PHP_EOL;
    }
}
图片[1]-动态IP解析阿里云域名-忆思阁博客-专注精品资源分享
图片[2]-动态IP解析阿里云域名-忆思阁博客-专注精品资源分享
图片[3]-动态IP解析阿里云域名-忆思阁博客-专注精品资源分享
© 版权声明
THE END
喜欢就支持一下吧
点赞1 分享
评论 抢沙发

请登录后发表评论