使用CloudXNS API实现DDNS(动态解析)

/ 0评 / 0

DDNS是将用户的动态IP地址映射到一个固定的域名解析服务上,用户每次连接网络的时候客户端程序就会通过信息传递把该主机的动态IP地址传送给位于服务商主机上的服务器程序,服务器程序负责提供DNS服务并实现动态域名解析。

ddns
目前国内支持DDNS解析的商家也比较多,比如花生壳等。如果你想自己不想使用第三方厂家提供的DDNS服务,完全可以自己折腾,比如使用CloudXNS API实现DDNS。

一、开始准备

二、安装CloudXNS API PHP SDK

选择CloudXNS API PHP SDK,所以你要提前把PHP环境搭建好咯,进入站点根目录依次执行下面的命令:

<span class="hljs-comment"><span class="com">### 下载CloudXNS API PHP SDK</span></span><span class="pln">
wget https</span><span class="pun">:</span><span class="com">//github.com/CloudXNS/CloudXNS-API-SDK-PHP/archive/master.zip</span>
<span class="hljs-comment"><span class="com">### 解压</span></span><span class="pln">
unzip master</span><span class="pun">.</span><span class="pln">zip
</span><span class="hljs-comment"><span class="com">### 移动</span></span><span class="pln">
mv </span><span class="typ">CloudXNS</span><span class="pun">-</span><span class="pln">API</span><span class="pun">-</span><span class="pln">SDK</span><span class="pun">-</span><span class="pln">PHP</span><span class="pun">-</span><span class="pln">master</span><span class="com">/* ./</span>

三、安装SDK

CloudXNS-API-SDK-PHP需要用到Composer,如果您还没有安装Composer,请安装Composer,接着在站点根目录执行composer install即可完成SDK安装。吐槽:SDK在国外,安装速度太慢了。

四、添加执行脚本

CloudXNS API PHP SDK里面有完整的DEMO,需要的可以参考。将下面的代码另存为ddns.php,setApiKey和setSecretKey请注意修改为自己在CloudXNS后台获取的。其中test.hixz.org请修改为您在CloudXNS需要更新的解析记录。

<span class="hljs-meta"><span class="pun"><?</span><span class="pln">php</span></span>
    <span class="hljs-comment"><span class="com">//获取公网IP</span></span><span class="pln">
    $ip </span><span class="pun">=</span><span class="pln"> file_get_contents</span><span class="pun">(</span><span class="hljs-string"><span class="str">"https://www.xiaoz.me/ip/userip.php"</span></span><span class="pun">);</span>

    <span class="hljs-keyword"><span class="pln">require_once</span></span> <span class="hljs-string"><span class="str">'./vendor/autoload.php'</span></span><span class="pun">;</span><span class="pln">
    $api </span><span class="pun">=</span> <span class="hljs-keyword"><span class="kwd">new</span></span><span class="pln"> \CloudXNS\Api</span><span class="pun">();</span><span class="pln">
    $api</span><span class="pun">-></span><span class="pln">setApiKey</span><span class="pun">(</span><span class="hljs-string"><span class="str">'xxxxxxxx'</span></span><span class="pun">);</span><span class="pln">
    $api</span><span class="pun">-></span><span class="pln">setSecretKey</span><span class="pun">(</span><span class="hljs-string"><span class="str">'xxxxxxxx'</span></span><span class="pun">);</span><span class="pln">
    $api</span><span class="pun">-></span><span class="pln">setProtocol</span><span class="pun">(</span><span class="hljs-keyword"><span class="kwd">true</span></span><span class="pun">);</span>
    <span class="hljs-comment"><span class="com">/**
     * DDNS快速修改解析记录
     * </span><span class="hljs-doctag"><span class="com">@param</span></span><span class="com"> string $domain 包含主机记录的域名
     * </span><span class="hljs-doctag"><span class="com">@param</span></span><span class="com"> string $ip IP值 多个以|分割如1.1.1.1|2.2.2.2 可为空
     * </span><span class="hljs-doctag"><span class="com">@param</span></span><span class="com"> integer $line_id 线路id 默认为1,可为空
     */</span></span>
    <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $api</span><span class="pun">-></span><span class="pln">ddns</span><span class="pun">-></span><span class="pln">ddns</span><span class="pun">(</span><span class="hljs-string"><span class="str">'test.hixz.org.'</span></span><span class="pun">,</span><span class="pln">$ip</span><span class="pun">,</span><span class="hljs-number"><span class="lit">1</span></span><span class="pun">);</span>
    <span class="hljs-keyword"><span class="pln">echo</span></span><span class="pln"> $ip</span><span class="pun">;</span>
<span class="hljs-meta"><span class="pun">?></span></span>

五、定时任务

我们可以用Linux的crontab定时任务每小时运行一次脚本,及时更新公网IP,如下:

<span class="hljs-comment"><span class="com">### 添加定时任务</span></span><span class="pln">
crontab </span><span class="pun">-</span><span class="pln">e
</span><span class="hljs-comment"><span class="com">### 加入计划任务,没小时执行一次脚本</span></span>
<span class="lit">10</span> <span class="pun">*</span> <span class="pun">*</span> <span class="pun">*</span> <span class="pun">*</span> <span class="hljs-built_in"><span class="pln">cd</span></span> <span class="pun">/</span><span class="pln">data</span><span class="pun">/</span><span class="pln">wwwroot</span><span class="pun">/</span><span class="pln">test</span><span class="pun">/</span> <span class="pun">&&</span> <span class="str">/usr/</span><span class="hljs-built_in"><span class="kwd">local</span></span><span class="pun">/</span><span class="pln">php</span><span class="pun">/</span><span class="pln">bin</span><span class="pun">/</span><span class="pln">php ddns</span><span class="pun">.</span><span class="pln">php </span><span class="pun">>></span> <span class="str">/home/</span><span class="pln">ddns</span><span class="pun">.</span><span class="pln">log </span><span class="lit">2</span><span class="pun">>&</span><span class="lit">1</span>
<span class="hljs-comment"><span class="com">### 重载crontab</span></span><span class="pln">
service crond reload</span>

10 指每小时的10分,/data/wwwroot/test/为网站根目录,/usr/local/php/bin/php是PHP所在的安装路径。

六、测试

脚本执行成功后,由于DNS缓存原因,一般10分钟左右生效,我们可以用ping命令测试是否和自己的公网IP一致,如果一致,说明DDNS解析成功,如下截图。

ddns_195527

发表评论