zoukankan      html  css  js  c++  java
  • 阿里云修改域名对应的ip地址php实现

    <?php
    date_default_timezone_set("GMT");
    class Ali
    {
    private $accessKeyId = "";//自己的ID
    private $accessSecrec = "";//自己的secrec
    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" => "eu80.com"
    );
    $val = $this->requestAli($requestParams);
    $val = json_decode($val,true);
    return $val;
    }

    /**
    * 更新 ip
    * @param $string
    */
    public function UpdateDomainRecord($second)
    {
    $ip = $this->getIp();
    //判断当前IP是否与配置IP一致,若不一致则进行修改
    $dnsList = $this->DescribeDomainRecords();
    //获取当前二级域名的记录
    foreach ($dnsList["DomainRecords"]['Record'] as $key => $value) {
    if($value["RR"]==$second);
    $dns = $value;
    }
    if($ip != $dns["Value"]){
    $requestParams = array(
    "Action" => "UpdateDomainRecord",
    "RecordId" => $dns["RecordId"],
    "RR" => $dns["RR"],
    "Type" => $dns['Type'],
    "Value" => $ip,
    );
    $val = $this->requestAli($requestParams);
    $this->outPut($val." ".$ip);
    }else{
    echo "当前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 getIp()
    {
    $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;
    }
    }
    //绑定 ip 到域名
    Ali::Obj()->UpdateDomainRecord("*");

  • 相关阅读:
    使用comet架构实现了一个基于网页的视频监控prototype!!!!哇哈哈庆祝一下
    Pixysoft.Framework.Noebe.Datamining 数据挖掘开发实录
    论创业成功!让大家的青春充满着无限美好的回忆
    新年第一篇 数据库备份恢复系统上线的挫折
    .Net FrameWork 4.0中使用EF向数据库插入数据报datatime2类型错误的解决办法
    RoRoWoBlog 开源博客系统介绍
    第一次偶然出现的“System.Data.Entity.dll”类型的异常
    序列化类型 System.Data.Entity.DynamicProxies 的对象时检测到循环引用
    我也来说说Entity Frame Work 4中的数据库优先和代码优先两种方式(2)
    Asp.net MVC 2 + Castle + NHibernate 项目实战(1)
  • 原文地址:https://www.cnblogs.com/sunke/p/5629178.html
Copyright © 2011-2022 走看看