Implementing DDNS with CloudXNS API
DDNS maps a user's dynamic IP address to a fixed domain name resolution service. Each time the user connects to the network, the client program sends the host's dynamic IP address to the server program located on the service provider's host via information transmission. The server program is responsible for providing DNS services and implementing dynamic domain name resolution.

Currently, there are many merchants in China that support DDNS resolution, such as Oray (HuaShengKe). If you do not want to use third-party DDNS services, you can easily set it up yourself, for example, by using the CloudXNS API to implement DDNS.
1. Preparation
- Requirement: A Zhanmei Small Host runs 24/7 at home and needs to connect to the public network to provide some services. However, the operator's IP is dynamic and changes at any time, so DDNS is needed.
- System: CentOS 6 X64
- Environment: PHP 5.6
- Domain: One domain name, already using CloudXNS DNS.
2. Install CloudXNS API PHP SDK
Since I know a little PHP, I chose the CloudXNS API PHP SDK. You need to set up the PHP environment first. Enter the site root directory and execute the following commands:
### Download CloudXNS API PHP SDK
wget https://github.com/CloudXNS/CloudXNS-API-SDK-PHP/archive/master.zip
### Unzip
unzip master.zip
### Move
mv CloudXNS-API-SDK-PHP-master/* ./
3. Install SDK
CloudXNS-API-SDK-PHP requires Composer. If you have not installed Composer yet, please refer to: Install Composer on CentOS. Then, execute composer install in the site root directory to complete the SDK installation. Note: The SDK is hosted abroad, so the installation speed is very slow.
4. Add Execution Script
The CloudXNS API PHP SDK contains a complete DEMO for reference. Save the following code as ddns.php. Please modify setApiKey and setSecretKey to the values obtained from your CloudXNS backend. Also, change test.hixz.org to the DNS record you need to update in CloudXNS.
<?php
// Get public IP
$ip = file_get_contents("https://blog.xiaoz.org/ip/userip.php");
require_once './vendor/autoload.php';
$api = new \CloudXNS\Api();
$api->setApiKey('xxxxxxxx');
$api->setSecretKey('xxxxxxxx');
$api->setProtocol(true);
/**
* DDNS Quick Modify DNS Record
* @param string $domain Domain containing the host record
* @param string $ip IP value, multiple IPs separated by |, e.g., 1.1.1.1|2.2.2.2, can be empty
* @param integer $line_id Line ID, default is 1, can be empty
*/
echo $api->ddns->ddns('test.hixz.org.', $ip, 1);
echo $ip;
?>
5. Scheduled Task
We can use Linux's crontab to run the script once every hour to update the public IP in time, as follows:
### Add scheduled task
crontab -e
### Add the job, execute the script every hour
10 * * * * cd /data/wwwroot/test.xiaoz.top/ && /usr/local/php/bin/php ddns.php >> /home/ddns.log 2>&1
### Reload crontab
service crond reload
10 * * * * means at the 10th minute of every hour. /data/wwwroot/test.xiaoz.top/ is the website root directory, and /usr/local/php/bin/php is the installation path of PHP.
6. Test
After the script executes successfully, due to DNS caching, it generally takes about 10 minutes to take effect. We can use the ping command to test if it matches your public IP. If they match, the DDNS resolution is successful, as shown in the screenshot below.

This article references: CloudXNS API PHP SDK. Original article, please indicate the source when reprinting.