zoukankan      html  css  js  c++  java
  • PHP阿里云云解析签名, 通过API 绑定域名到动态 ip

    <?php
    date_default_timezone_set("GMT");
    
    //绑定 ip 到域名
    Ali::Obj()->UpdateDomainRecord();
    
    class Ali
    {
        private $accessKeyId  = "OJR9w5xfGaz1jVDg";
        private $accessSecrec = "xOKRdKk0EI7F0hdtnEnIAVLVxAe4LI";
        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"    =>  "hankangkang.com"
            );
            $val =  $this->requestAli($requestParams);
            $this->outPut($val);
        }
    
        /**
         * 更新 ip
         */
        public function UpdateDomainRecord()
        {
            $ip = $this->ip();
            $requestParams = array(
                "Action"        =>  "UpdateDomainRecord",
                "RecordId"      =>  "71927383",
                "RR"            =>  "@",
                "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-dTH:i:s"),
                "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;
        }
    }
  • 相关阅读:
    Linux-线程同步(day14续)
    Linux之线程(day14)
    Linux-网络编程-UDP网络编程(day13续2)
    ES6 模块加载
    let与var声明区别
    vue 常用指令v-if v-else v-show v-for
    动态路由的意义,以及路由重定向
    前端路由的理解
    socpe 与 包的引入
    VUE 组件注册(全局、局部)
  • 原文地址:https://www.cnblogs.com/hanyouchun/p/5382709.html
Copyright © 2011-2022 走看看