我是靠谱客的博主 云华Z,最近开发中收集的这篇文章主要介绍PHP阿里云云解析签名, 通过API 绑定域名到动态 ip,觉得挺不错的,现在分享给大家,希望可以做个参考。

概述

PHP阿里云云解析签名, 通过API 绑定域名到动态 ip

<?php
date_default_timezone_set("GMT");

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

class Ali
{
    private $accessKeyId = "LTAI4FfNGoaPzNMqVfMWyUia";
    private $accessSecrec = "Gz1esMWHjayrZcWju5lUj3E3pSzuVP";
    private static $obj = null;

    public static function Obj()
    {
        if (is_null(self::$obj)) {
            self::$obj = new self();
        }
        return self::$obj;
    }

    /**
     *    起始
     */
    public function start()
    {
        switch ('Create')//$_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" => 'vipzx.com.cn'//$_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" => 'vipzx.com.cn',//$_POST["domain"],
            "RR"         => 'china',// $_POST["host"],
            "Type"       => 'A',//$_POST["type"],
            "Value"      => '123.57.11.58'// $_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-dTH:i:sZ"),
            "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);
    }

}


最后

以上就是云华Z为你收集整理的PHP阿里云云解析签名, 通过API 绑定域名到动态 ip的全部内容,希望文章能够帮你解决PHP阿里云云解析签名, 通过API 绑定域名到动态 ip所遇到的程序开发问题。

如果觉得靠谱客网站的内容还不错,欢迎将靠谱客网站推荐给程序员好友。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(448)

评论列表共有 0 条评论

立即
投稿
返回
顶部