PHP cURL: CLI DtDNS Updater 2

From The Uniform Server Wiki
Jump to navigation Jump to search

 

MPG UniCenter

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);
?>

Top

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

<?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 = 'http://www.dtdns.com/api/autodns.cfm'; // Real URL
$url = 'http://localhost:82/autodns.php';        // Test URL

// Build complete URL
$str= $url."?id=".$id_val."&pw=".$pw_val."&ip=".$ip_val."&client=".$client_val;

// Access page using Curl
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$str);
curl_exec($ch);
curl_close($ch);

?>

 

  • First four lines URL encode the values we wish to send.
  • Two URLs are defined, these are switched after testing (reverse the commants)
  • String $str is assembled into correct format host page name-value pairs.
  • The string variable $str is inserted into the Curle URL option.


Test:

  • Run both servers
  • Type http://localhost/test14.php into your browser
  • Result: Shown below

Results:

Information

Host     (id)    = uni.dtdns.net
Password (pw)    = me123
Ip       (ip)    = 11.22.33.44
Client   (client)= UniServerV1

Host uni.dtdns.net now points to 11.22.33.44.

 

  • Note 1:

Assumes variable $information is set to true; in file C:\curl_2\UniServer\www\autodns.php otherwise the information will not be displayed.

Top

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

<?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."&ip=".$ip_val."&client=".$client_val;

// Access page using Curl
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,$str);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);   // no verify
curl_exec($ch);
curl_close($ch);

?>

 

  • First four lines URL encode the values we wish to send.
  • Two URLs are defined (Note http changed to https), these are switched after testing (reverse the commants)
  • Our simulation server requires port number 446
  • String $str is assembled into correct format host page name-value pairs.
  • The string variable $str is inserted into the Curle URL option.
  • New line added sets CURLOPT_SSL_VERIFYPEER to false

Test:

  • Run both servers
  • Type http://localhost/test15.php into your browser
  • Results: Identical to Example 14

Top

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.

Top

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

<?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."&ip=".$ip_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 output 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";
}
?>

 

  • Added line CURLOPT_RETURNTRANSFER to force curl_exec to output string.
  • curl_exec output stored in variable $buffer
  • Use preg match to test for string now points to

Test:

  • Run both servers
  • Type http://localhost/test16.php into your browser
  • Results: Update successful -Displayed

Top

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";
}
?>

Top

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.


Top