├── LICENSE ├── README.md └── alidns.php /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2022 Star Yu 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # alidns-api-php 2 | DDNS for RouterOS 3 | 4 | 如有问题请去我的博客留言:https://www.77bx.com/aliyun-ddns-api.html 5 | -------------------------------------------------------------------------------- /alidns.php: -------------------------------------------------------------------------------- 1 | $value) { 64 | $canonicalizedQueryString .= '&' . percentEncode($key) . '=' . percentEncode($value); 65 | } 66 | $stringToBeSigned = 'POST&%2F&' . percentEncode(substr($canonicalizedQueryString, 1)); 67 | $signature = base64_encode(hash_hmac('sha1', $stringToBeSigned, $accessKeySecret. '&', true)); 68 | return $signature; 69 | } 70 | 71 | function geturl($public, $request, $accessKeySecret){ 72 | $params = array_merge($public, $request); 73 | $params['Signature'] = sign($params, $accessKeySecret); 74 | $uri = http_build_query($params); 75 | $url = 'https://alidns.aliyuncs.com/?'.$uri; 76 | return $url; 77 | } 78 | 79 | function ssl_post($url){ 80 | $curl = curl_init(); 81 | curl_setopt($curl, CURLOPT_URL, $url); 82 | curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0); 83 | curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0); 84 | curl_setopt($curl, CURLOPT_POST, 1); 85 | curl_setopt($curl, CURLOPT_TIMEOUT, 30); 86 | curl_setopt($curl, CURLOPT_HEADER, 0); 87 | curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); 88 | $tmpInfo = curl_exec($curl); 89 | if (curl_errno($curl)) { 90 | echo 'Errno'.curl_error($curl); 91 | } 92 | curl_close($curl); 93 | return $tmpInfo; 94 | } 95 | 96 | $public = array( 97 | 'Format' => 'json', 98 | 'Version' => '2015-01-09', 99 | 'AccessKeyId' => $accessKeyId, 100 | 'SignatureMethod' => 'HMAC-SHA1', 101 | 'Timestamp' => $Timestamp, 102 | 'SignatureVersion' => '1.0', 103 | 'SignatureNonce' => generateByMicrotime() 104 | ); 105 | $search = array( 106 | 'Action' => 'DescribeDomainRecords', 107 | 'DomainName' => $domain, 108 | 'PageSize' => '500', 109 | 'RRKeyWord' => $record, 110 | 'Type' => $type 111 | ); 112 | 113 | //搜索record相关的记录列表 114 | $data = json_decode(ssl_post(geturl($public,$search, $accessKeySecret)),true); 115 | 116 | if(empty($data['DomainRecords'])){ 117 | exit('1'); 118 | }else{ 119 | foreach($data['DomainRecords']['Record'] as $value){ 120 | $record_arr = array(); 121 | if($value['RR'] == $record){ 122 | $record_id = $value['RecordId']; 123 | $record_arr = $value; 124 | break; 125 | } 126 | } 127 | 128 | if(empty($record_id)){ 129 | $add = array( 130 | 'Action' => 'AddDomainRecord', 131 | 'DomainName' => $domain, 132 | 'RR' => $record, 133 | 'Type' => $type, 134 | 'Value' => $ip, 135 | 'TTL' => '600', 136 | ); 137 | $data = json_decode(ssl_post(geturl($public,$add, $accessKeySecret)),true); 138 | if(empty($data['RecordId'])){ 139 | exit('1'); 140 | }else{ 141 | exit('0'); 142 | } 143 | }else{ 144 | if($record_arr['Value'] == $ip){ 145 | exit('0'); 146 | }else{ 147 | $edit = array( 148 | 'Action' => 'UpdateDomainRecord', 149 | 'RecordId' => $record_id, 150 | 'RR' => $record, 151 | 'Type' => $type, 152 | 'Value' => $ip, 153 | 'TTL' => '600', 154 | ); 155 | $data = json_decode(ssl_post(geturl($public,$edit, $accessKeySecret)),true); 156 | if(empty($data['RecordId'])){ 157 | exit('1'); 158 | }else{ 159 | exit('0'); 160 | } 161 | } 162 | } 163 | } --------------------------------------------------------------------------------