PHP cURL: CLI Set-up
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
I use the term running Curl in CLI mode very loosely strictly speaking it should be running Curl using PHP scripts in CLI mode.
Anyway running PHP in CLI mode allows Curl to be run independent of a server. This part of the tutorial shows how to convert existing PHP server scripts (code) for CLI operation. In addition how to configure PHP CLI to run cURL and make it portable.
I personally dislike theory with no practical objective in mind. Hence the objective is to lay foundations for a plugin to automatically update a DtDNS account with your current IP address.
Location of Scripts
Although its obvious I will state it anyway for portability all scripts must be located somewhere in folder UniServer. These scripts are generally intended to be run locally hence should not be placed in the server's root folder www.
Ideally try to avoid long paths to scripts by keeping folder depths small this gives a small increase in speed and saves typing.
Preparation
Before starting this tutorial there is some ground work to do, first we want somewhere to put our code and write some code to work with.
Plugin
Our new plugin will eventually reside in folder plugins hence this is a good place to test new code. Any new plugin should have its own folder, choose a name that is appropriate to your project. For this tutorial dtdns_updater seems like a reasonable choice.
In folder C:\curl_1\UniServer\plugins create a new folder named dtdns_updater
Code
Before running an IP updater we need to determine our current IP address and check to see if it has changed. There is no point in running an updater if our IP address has not changed.
We already have some tested code written for a server see example 4 reproduced below for connivance.
<?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($ch); curl_close($ch); if (empty($buffer)){ print "Need to recover from this!<br />"; } else{ print "There was data returned using curl.<br />"; print "Buffer content = ".$buffer."<br />"; // 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; } } ?> |
Create a new text file in folder C:\curl_1\UniServer\plugins\dtdns_updater and name it test14.php add the above code.
Code conversion
Generally speaking converting Server code snippets to CLI code is nothing more that replacing all line breaks <br /> with a new line character \n. Below I have shown the code after conversion:
test14.php:
<?php $ch=curl_init(); curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); curl_setopt($ch,CURLOPT_RETURNTRANSFER,1); $buffer = curl_exec($ch); curl_close($ch); if (empty($buffer)){ print "Need to recover from this!\n"; } else{ print "There was data returned using curl.\n"; print "Buffer content = ".$buffer."\n"; // 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; } } ?> |
That competes the preparation.
Batch file to run code
The above script can be run from other scripts. Alternatively run from a command line, however cURL has not been installed hence you need to spefify full paths for example type this into a command prompt:
C:\curl_1\UniServer\usr\local\php\php.exe C:\curl_1\UniServer\plugins\dtdns_updater\test14.php
Clearly not very practical, OK you could create a desktop shortcut! Not very portable.
My preference is to use a batch file, its portable and configurable.
Create a new text file Run.bat in folder dtdns_updater with the following content:
COLOR B0 @echo off cls rem ### working directory current folder pushd %~dp0 ..\..\usr\local\php\php.exe test14.php rem ### restore original working directory pause popd EXIT
The code moves up two folder levels (..\..\) and traverses down to the executable php.exe a single parameter is passed to this program test14.php
That single line runs our test script, note (..\..\) is a relative path to maintain portability.
PHP CLI INI
If you were to run the above code chances are a number of errors are reported. This is beause PHP has searched for a configuration file named php-cli.ini since this does not exist it searches for one named php.ini which it finds and uses.
Hidden gotcha
However even if the script runs there is a hidden gotcha. This configuration file php.ini is specific to a web server hence the errors or possible error at a later date.
Create a new text file named php-cli.ini in folder C:\curl_1\UniServer\usr\local\php with the following content:
[PHP] extension=fred extension=php_curl.dll extension_dir = "./extensions" |
|
Run batch file
Double click on Run.bat there is no need to have the servers running that's the whole point of running in CLI mode.
You will receive an error similar to this:
PHP Warning: PHP Startup: Unable to load dynamic library './extensions\fred' - The specified module could not be found. in Unknown on line 0 Need to recover from this!
What’s important is the error message stating it cannot load library fred this confirms the CLI configuration is being picked up correctly.
Edit php-cli.ini and remove the line extension=fred its fulfilled its purpose and no longer required.
Save the file and re-run Run.bat you will receive the following resut:
Need to recover from this! Press any key to continue . . . |
The message indicates either nothing was returned or as in this case the remote server (simulation server curl_2) is not running |
Simulation Server
Our simulation server contains the following file C:\curl_2\UniServer\www\remote_page.php with content as shown below:
<?php //Gets the IP address $ip = getenv("REMOTE_ADDR") ; Echo "Your IP is " . $ip; ?> |
|
|
There was data returned using curl. Buffer content = Your IP is 127.0.0.1 127.0.0.1Press any key to continue . . . |
Modify code for live test
Edit file test14.php comment out third line and add a new fourth line as shown below:
<?php $ch=curl_init(); //curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); curl_setopt($ch,CURLOPT_URL,'http://checkip.dyndns.org/');
With your PC connected to the Internet run Run.bat result will be similar to this:
There was data returned using curl. Buffer content = <html><head><title>Current IP Check</title> </head><body>Current IP Address: 81.170.25.59</body></html> 81.170.25.59Press any key to continue . . .
Note variable $buffer contains the page code downloaded which is printed as is. The IP address is extracted from this into vaiable $ip and printed.
That completes testing all that remains is to clean-up the code for later use see below:
<?php $ch=curl_init(); //curl_setopt($ch,CURLOPT_URL,'http://localhost:82/remote_page.php'); curl_setopt($ch,CURLOPT_URL,'http://checkip.dyndns.org/'); curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,5); 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."\n"; // Test code ?>
Summary
The above has shown how to configure cURL on Uniform Server for portable operation using PHP in CLI mode.
Introduced the concept of a simulation server where all testing can be performed locally before committing to live servers and performing real tests.
Above all its not just theory you can confirm code examples really work on live local servers. While experimenting there is a real objective in mind, currently we have a partial solution for an IP updater. The net page covers DtDNS updater using a CLI script.