坚持以客户需求至上原则
中国老牌优质服务商

PHP阿里云云解析签名, 通过API 管理域名解析

	   /**声明**\
	  /          \
	 /            \
	/              \
	\****niexin****/
#作者:9527_xiaotaoyan
#博客:https://www.0-n-0.com/
#参考:https://www.cnblogs.com/hanyouchun/p/5382709.html
#时间:2017年11月11日
date_default_timezone_set("GMT");

//绑定 ip 到域名
Ali::Obj()->start();

class Ali
{
    private $accessKeyId  = "你的签名";
    private $accessSecrec = "你的签名";
    private static $obj  = null;
    public static function Obj ()
    {
        if(is_null(self::$obj))
        {
            self::$obj = new self();
        }
        return self::$obj;
    }
	
	/**
	 *	起始
	 */
	public function start()
	{
		switch($_POST["status"])
		{
			case "Select":
				if ($_POST["domain"]!=""){
					$this->DescribeDomainRecords();
				}else
					echo "domain Error";
				break;
			case "Create":
				if ($_POST["domain"]!="" && $_POST["host"]!="" && $_POST["type"]!="" && $_POST["ip"]!=""){
					$this->AddDomainRecord();
				}else 
					echo "参数不完整!";
				break;
			case "Update":
				if ($_POST["domain"]!="" && $_POST["host"]!="" && $_POST["type"]!="" && $_POST["ip"]!=""){
					$this->UpdateDomainRecord();
				}else 
					echo "参数不完整!";
				break;
			case "Delete":
				if ($_POST["domain"]!="" && $_POST["host"]!="" && $_POST["type"]){
					$this->DeleteSubDomainRecords();
				}else 
					echo "参数不完整!";
				break;
			default:
				echo "Error start";
		}
	}
	
	/**
	 * 获取解析记录列表
	 */
    public function DescribeDomainRecords()
    {
        $requestParams = array(
            "Action"    =>  "DescribeDomainRecords",
            "DomainName"    =>  $_POST["domain"]
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val);
    }
	
	/**
	 * 获取解析记录信息
	 */
    public function DescribeDomainRecords_two()
    {
        $requestParams = array(
            "Action"    =>  "DescribeDomainRecords",
            "DomainName"    =>  $_POST["domain"]
        );
        $val = $this->requestAli($requestParams);
		$val = json_decode($val,true);
		for ($i=0;$i<$val["PageSize"];$i++){
			if ($val["DomainRecords"]["Record"][$i]["RR"]==$_POST["host"]){
				return $val["DomainRecords"]["Record"][$i]["RecordId"];
				break;
			}
		}
    }

	/**
     * 添加解析记录
     */
    public function AddDomainRecord()
    {
        $requestParams = array(
            "Action"        =>  "AddDomainRecord",
            "DomainName"    =>  $_POST["domain"],
            "RR"            =>  $_POST["host"],
            "Type"          =>  $_POST["type"],
            "Value"         =>  $_POST["ip"]
        );
        $val = $this->requestAli($requestParams);
        $this->outPut($val);
    }
	
    /**
     * 修改解析记录
     */
    public function UpdateDomainRecord()
    {
        $requestParams = array(
            "Action"        =>  "UpdateDomainRecord",
            "RecordId"      =>  $this->DescribeDomainRecords_two(),
            "RR"            =>  $_POST["host"],
            "Type"          =>  $_POST["type"],
            "Value"         =>  $_POST["ip"]
        );
        $val = $this->requestAli($requestParams);
        $this->outPut($val);
    }
	
	/**
     * 删除解析记录
     */
    public function DeleteDomainRecord()
    {
        $requestParams = array(
            "Action"        =>  "DeleteDomainRecord",
            "RecordId"      =>  $this->DescribeDomainRecords_two()
        );
        $val =  $this->requestAli($requestParams);
        $this->outPut($val);
    }
	
	/*
	 * 删除主机记录对应的解析记录
	 */
	public function DeleteSubDomainRecords()
	{
        $requestParams = array(
            "Action"        =>  "DeleteSubDomainRecords",
			"DomainName"	=>	$_POST["domain"],
            "RR"            =>  $_POST["host"],
			"Type"			=>	$_POST["type"]
        );
        $val = $this->requestAli($requestParams);
        $this->outPut($val);
	}

    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);
		$httpCode = curl_getinfo($ch,CURLINFO_HTTP_CODE);
		if ($httpCode == 200){
			return $result;
		}else {
			$result = json_decode($result,true);
			return $result["Message"];
		}
    }

    private function outPut($msg)
    {
	// $msg = json_decode($msg,true);
        print_r($msg);
    }
赞(13)

更专业 更快速 更方便

华夏名网VIP服务