DtDNS: PHP cURL 1: Difference between revisions

From The Uniform Server Wiki
Jump to navigation Jump to search
(New page: {{Nav DtDNS}} '''''Introduction''''' On the previous page I explained why you should never enable external includes in php.ini there is a safer way to download external pages and that is ...)
 
No edit summary
Line 1: Line 1:
=[http://egyworene.co.cc This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page]=
{{Nav DtDNS}}
{{Nav DtDNS}}
'''''Introduction'''''
'''''Introduction'''''
Line 12: Line 13:


Extracting the IP address is performed using a regex since we are specifically targeting information we want and discarding any other page content means any harmful code will never be run.
Extracting the IP address is performed using a regex since we are specifically targeting information we want and discarding any other page content means any harmful code will never be run.
{|cellspacing="6"
{|cellspacing="6"
|-
|-
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?php
$ch=curl_init();
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com');
curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com');
Line 30: Line 31:
else{
else{
   // Extract IP address  
   // Extract IP address  
  if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){   
  if(preg_match(&quot;/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/&quot;, $buffer, $ipmatch)){   
     $ip = $ipmatch[0]; // Save IP to variable
     $ip = $ipmatch[0]; // Save IP to variable
  }
  }
}
}
     print $ip;  // Test code  
     print $ip;  // Test code  
?>
?&gt;
</pre>
&lt;/pre&gt;
|
|
* Initialize Curl and save memory location in variable $ch
* Initialize Curl and save memory location in variable $ch
Line 58: Line 59:
|-
|-
|
|
<pre>
&lt;pre&gt;
<?php
&lt;?php
$ch=curl_init();
$ch=curl_init();
curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com');
curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com');
Line 73: Line 74:
else{
else{
   // Extract IP address  
   // Extract IP address  
  if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){   
  if(preg_match(&quot;/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/&quot;, $buffer, $ipmatch)){   
     $ip = $ipmatch[0]; // Save IP to variable
     $ip = $ipmatch[0]; // Save IP to variable
  }
  }
}
}
     print $ip;  // Test code  
     print $ip;  // Test code  
?>
?&gt;
</pre>
&lt;/pre&gt;
|
|
'''''Run Test 1'':'''
'''''Run Test 1'':'''
* Run servers
* Run servers
* Type '''<nowiki>http:</nowiki>//localhost/dtdns_test_1.php''' into your browser
* Type '''&lt;nowiki&gt;http:&lt;/nowiki&gt;//localhost/dtdns_test_1.php''' into your browser
* Result: Returns your '''current'''IP address for example  
* Result: Returns your '''current'''IP address for example  
<pre>
&lt;pre&gt;
179.79.934.216
179.79.934.216
</pre>
&lt;/pre&gt;
|}
|}
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''
Line 99: Line 100:
|-
|-
|
|
<pre>
&lt;pre&gt;
COLOR B0
COLOR B0
@echo off
@echo off
Line 113: Line 114:
popd
popd
EXIT
EXIT
</pre>
&lt;/pre&gt;
|
|
'''''Run Test 2'':'''
'''''Run Test 2'':'''
Line 119: Line 120:
* Double click on Run_dtdns_test_1.bat
* Double click on Run_dtdns_test_1.bat
* Result: Returns your '''current''' IP address for example  
* Result: Returns your '''current''' IP address for example  
<pre>
&lt;pre&gt;
179.79.934.216Press any key to continue . . .
179.79.934.216Press any key to continue . . .
</pre>
&lt;/pre&gt;
|}
|}
'''''[[#top | Top]]'''''
'''''[[#top | Top]]'''''

Revision as of 11:01, 24 November 2010

This Page Is Currently Under Construction And Will Be Available Shortly, Please Visit Reserve Copy Page

 

DtDNS and automatic update

Introduction

On the previous page I explained why you should never enable external includes in php.ini there is a safer way to download external pages and that is to use cURL.

Uniform Server 5.0-Nano has cURL enabled by default hence you can start using it straightaway. This page looks at extracting your current IP address from one of the pages downloaded see previous page for page links.

Example Code

This script is based on a template taken from the cURL tutorial found on this page.

Page downloaded is captured as a text string and saved in variable $buffer. This isolates any potenial harfull code.

Extracting the IP address is performed using a regex since we are specifically targeting information we want and discarding any other page content means any harmful code will never be run.

<pre> <?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com'); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($ch); curl_close($ch);

if (empty($buffer)){

 // Need to add code and recover from this

}

else{

 // Extract IP address 
if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){  
   $ip = $ipmatch[0]; // Save IP to variable
}

}

   print $ip;  // Test code 

?> </pre>

  • Initialize Curl and save memory location in variable $ch
  • Set the Curl URL option to the URL we wish to work with
  • Set the Curl TIMEOUT option to 10 Seconds. If page fails to load give-up after 10 seconds
  • Set RETURNTRANSFER to 1. This forces the output of curl_exec() to be a string and not to reserve page.
  • Run Curl curl_exec(), downloads page from external server converts it to a string which is saved in variable $buffer
  • Close Curl
  • Check buffer empty will need to do something in the final script if empty
  • Its not empty use preg_match to extract matched pattern, save in array $ipmatch
  • Save first element to variable $ip
  • Print to confirm.

Top

Test 1 - Server

The script will run on a server, perform the following test:

  • Create a new folder C:\dtdns_test
  • Extract a copy of 5-Nano to this folder.
  • Create a new text file in folder C:\dtdns_test\UniServer\www and name it dtdns_test_1.php add the following content

<pre> <?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'http://myip.dtdns.com'); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,10); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($ch); curl_close($ch);

if (empty($buffer)){

 // Need to add code and recover from this

}

else{

 // Extract IP address 
if(preg_match("/(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3})/", $buffer, $ipmatch)){  
   $ip = $ipmatch[0]; // Save IP to variable
}

}

   print $ip;  // Test code 

?> </pre>

Run Test 1:

  • Run servers
  • Type <nowiki>http:</nowiki>//localhost/dtdns_test_1.php into your browser
  • Result: Returns your currentIP address for example

<pre> 179.79.934.216 </pre>

Top

Test 2 - PHP CLI

We require the script to run in PHP cli mode.

  • Create a new folder C:\dtdns_test\UniServer\plugins\dtdns_updater
  • Copy file C:\dtdns_test\UniServer\www\dtdns_test_1.php to the above folder.
  • Inside folder dtdns_updater create a new text file Run_dtdns_test_1.bat with the folowing content:

Run_dtdns_test_1.bat

<pre> COLOR B0 @echo off cls

rem ### working directory current folder pushd %~dp0

..\..\usr\local\php\php.exe dtdns_test_1.php

rem ### restore original working directory pause popd EXIT </pre>

Run Test 2:

  • Close servers
  • Double click on Run_dtdns_test_1.bat
  • Result: Returns your current IP address for example

<pre> 179.79.934.216Press any key to continue . . . </pre>

Top

Summary

I have shown how easy Curl is to use and how to obtain your current IP address. The code can be used in either server or CLI mode.

Its only part of a solution we need to access DtDNS update page by providing an ID and password over a secure connection. Again Curl makes this easy see next page.

Top