当前位置: 代码迷 >> 综合 >> 【原创】php 阿里云云解析 动态域名 PHP 通过阿里云API接口实现 DDNS 动态解析
  详细解决方案

【原创】php 阿里云云解析 动态域名 PHP 通过阿里云API接口实现 DDNS 动态解析

热度:99   发布时间:2024-03-07 05:23:04.0

1.解析先,创建一个域名 tj.test.com

2.通过api 云解析调试界面获取 tj.test.com 的RecordId

 

 

3.编辑php源码:填写好 accessKeyId  accessSecrec  DomainName  RecordId  RR  

4. 提交运行php ,更新成功

5.后台查看更新成功。

 

6.php更新A解析源码,记得修改相应的信息。

<?php
date_default_timezone_set("GMT");//绑定 ip 到域名
Ali::Obj()->UpdateDomainRecord();class Ali
{private $accessKeyId  = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";private $accessSecrec = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";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"    =>  "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.com");$val =  $this->requestAli($requestParams);$this->outPut($val);}/*** 更新 ip*/public function UpdateDomainRecord(){$ip = $this->ip();$requestParams = array("Action"        =>  "UpdateDomainRecord","RecordId"      =>  "11111111111111111111111111111111111111","RR"            =>  "tj","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;}
}

7. 获取 域名对应的RecordId 源码示例:

aliyun alidns DescribeDomainRecords --region cn-hangzhou --RegionId cn-hangzhou --DomainName xxxxxxxxx.com
<?php
use AlibabaCloud\Client\AlibabaCloud;
use AlibabaCloud\Client\Exception\ClientException;
use AlibabaCloud\Client\Exception\ServerException;// Download:https://github.com/aliyun/openapi-sdk-php
// Usage:https://github.com/aliyun/openapi-sdk-php/blob/master/README.mdAlibabaCloud::accessKeyClient('<accessKeyId>', '<accessSecret>')->regionId('cn-hangzhou')->asDefaultClient();try {$result = AlibabaCloud::rpc()->product('Alidns')// ->scheme('https') // https | http->version('2015-01-09')->action('DescribeDomainRecords')->method('POST')->host('alidns.aliyuncs.com')->options(['query' => ['RegionId' => "cn-hangzhou",'DomainName' => "xxxxxxxxxxxxxxx.com",],])->request();print_r($result->toArray());
} catch (ClientException $e) {echo $e->getErrorMessage() . PHP_EOL;
} catch (ServerException $e) {echo $e->getErrorMessage() . PHP_EOL;
}