PHP cURL: CLI DtDNS Updater 2
PHP cURL : Introduction | Basics | Authentication | SSL | GET & POST | GET POST SSL AUTH | CLI Set-up | CLI DtDNS Updater 1 | CLI DtDNS Updater 2
|
|
UniServer 5-Nano PHP cURL. |
cURL CLI DtDNS Updater - Part 2
On the previous page we finished with a PHP script that simulates responses from the DtDNS update page. Using this simulation page we can develop a Curl script to update our IP address.
Background
Essentially we already have a solution, all that is required is to access DtDNS page using this URL format:
http://www.dtdns.com/api/autodns.cfm?id=hostname&pw=password&ip=address&client=name https://www.dtdns.com/api/autodns.cfm?id=hostname&pw=password&ip=address&client=name |
We have tested this on our simulation page using:
http://localhost:82/autodns.php?id=uni.dtdns.net&pw=me123&ip=11.22.33.44&client=UniServerV1 |
Al that is required is to send our data using the GET method we have covered this see example 10 reproduced below:
<?php $name_val = urlencode('MPG RIC'); $password_val = urlencode('fred 123'); $message_val = urlencode('This is a test & = ?'); $str= "?Name=".$name_val."&Password=".$password_val."&Message=".$message_val; print $str; $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_form.php'.$str); curl_exec($ch); curl_close($ch); ?> |
Example 14 - cURL GET
The above template is easily converted, change the variable names to match and add a new variable. Separate out the page URL.
Create a new text file in folder C:\curl_1\UniServer\www and name it test14.php add the following content
|
|
Results:
|
Assumes variable $information is set to true; in file C:\curl_2\UniServer\www\autodns.php otherwise the information will not be displayed. |
Example 15 - cURL GET + SSL
The above example sends the password unencrypted not a good idea. The update page can use https hence convert the scrip for SSL operation.
First copy file C:\curl_2\UniServer\www\autodns.php to folder C:\curl_2\UniServer\ssl
Edit file C:\curl_2\UniServer\ssl\.htaccess disable authentication as shown (Leftover from another example)
#AuthName "Uniform Server - Secure Server Access" #AuthType Basic #AuthUserFile C:/curl_2/UniServer/htpasswd/ssl/.htpasswd #Require valid-user
Create a new text file in folder C:\curl_1\UniServer\www and name it test15.php add the following content
|
Test:
|
Realistic test
Switch the additional information off edit file file C:\curl_2\UniServer\ssl\autodns.php set variable $information to false
Test:
- Run both servers
- Type http://localhost/test15.php into your browser
- Results:
Host uni.dtdns.net now points to 11.22.33.44.
Capture returned page
We want to capture the returned page (effectively a single line) into a variable to test its contents.
Example 16 - Test returned page
Create a new text file in folder C:\curl_1\UniServer\www and name it test16.php add the following content
|
Test:
|
Back to CLI
The script has been tested and developed using our test server time to test in CLI mode.
Copy file C:\curl_1\UniServer\www\test16.php to folder C:\curl_1\UniServer\plugins\dtdns_updater
Create a new batch file C:\curl_1\UniServer\plugins\dtdns_updater\Run_test16.bat with the following content:
COLOR B0 @echo off cls rem ### working directory current folder pushd %~dp0 ..\..\usr\local\php\php.exe test16.php rem ### restore original working directory pause popd EXIT
Test
- Run batch file test16.bat
- Result:
Update successfulPress any key to continue . . .
That completes testing on our simulation server.
Live test
If you have an account at DtDNS you can perform a live test, note IP is optional hence I have removed that section and swapped in the real URL the script looks like:
Just substitute your real id (host name) and password.
<?php $id_val = urlencode('uni.dtdns.net'); // User Host on DtDNS $pw_val = urlencode('me123'); // User password $ip_val = urlencode('11.22.33.44'); // Optional $client_val = urlencode('UniServerV1'); // Optional - But added it $url = 'https://www.dtdns.com/api/autodns.cfm'; // Real URL //$url = 'https://localhost:446/autodns.php'; // Test URL // Build complete URL $str= $url."?id=".$id_val."&pw=".$pw_val."&client=".$client_val; // Access page using Curl SSL $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,$str); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); // no verify curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); // timeout set to 5 sceonds curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); // force curl_exec to ouput string $buffer = curl_exec($ch); // run above, save returned page to buff curl_close($ch); // Test returned page if(preg_match("/now\spoints\sto/",$buffer)){ // Test for "now points to" print " Update successful"; // Crude but works } else{ // Failed print "Update failed"; } ?> |
Summary
The above provides a working template for a PHP CLI script DtDNS IP updater.
This can be integrated with Example 4 which provides a template to find your IP address as seen from the Internet.