PHP cURL: Introduction

 

MPG UniCenter

UniServer 5-Nano
PHP cURL.

Introduction

Uniform Server 5.0-Nano has cURL enabled by default hence you can start using it straightaway. It allows your server to connect and communicate with other servers. Many different types of protocols are supported adding to its flexibility; of importance is the ability to automatically login to a remote server.

PHP can be run in CLI (command line interface) mode this also allows cURL to run in CLI mode significance of this no server is required.

This tutorial covers both Server and CLI Curl operation, emphasis is given to providing working examples making Curl a little easier to understand.

Top

UniServer

A quick note if you have never used Uniform Server before. It’s a throw away server, extract it to a folder of your choice and run. Experiment, get in there and have a play, if you break it! Not an issue just delete (throw away) the folder and start again.

  • You want a portable server! Extract it to any folder on a USB stick and! Well run it.
  • You want a production server! Well run it.
  • You want it to always start! Well runt it as a service (right mouse click install as service and you are away)
  • You need more than one server! For each new server, extract a new copy and move it (Advanced > Move servers multi-server operation ).
  • You want Cron! For details read the manual.
  • You want Security! Again read the manual depends what you require.

Yep! It’s a very flexibly throwaway server.

This tutorial uses several of the above features.

Top

Tutorial test Servers

This tutorial requires two test servers.

  • Create two new folders named c:\curl_1 and c:\curl_2 (its easier to follow the tutorial with these names however you can use whatever you like)
  • Extract a copy of Uniform Server 5-Nano to each of the above folders.

The server extracted to folder c:\curl_2 needs to be moved by this I mean servers (Apache MySQL) run on different ports.

Move Servers

  1. Assumes a new extracted server.
  2. Stop all running Uniform Servers and close their correspoding UniTray's
  3. Start UniTray, in folder c:\curl_2\UniServer double click on Start.exe tray icon created.
  4. Move servers, Left click tray icon > Advanced > click Move Servers multi-server operation.
  5. In the pop-up window at all prompts press enter to accept defaults.
  6. The tray icon will display 2, if you already use a server with this number repeat steps 4 and 5 each server must have a unique number.
  7. Repeat steps 4 and 5 see note.

Note: The reason I don't use icon 2 is because it is reserved for my Wiki. The next free server is icon 3, hence why I sugest you move the second server accordingly again its easier to follow the tutorial.

After performing the above steps test server curl_2 is now configured to run Apache on port = 82 and MySQL on port = 3308

You can check what server ports are being used by running Server Status from UniTray.

Top

cURL - Modes

Curl can be used in either of two modes Server or CLI this tutorial splits into two separate section as follows:

Server mode

Curl is all about accessing other server resources these can be located on the Internet, Intranet or even on your local PC.

Uniform Server 5-Nano allows you to run more than one server on the same PC. Advantage of this, external server resources can be simulated locally allowing you to perform tests within a locally envitioment.

Tutorial covers the following Curl Server topics:

Basics Introduction to Curl's basic functions for downloading and reserving a page. Instead of reserving a
page shows how to save it to a variable allowing page content to be manipulated before serving to a user.
Note: Example 4 - Download and save page to a variable provides a template to obtain your IP address
Authentication A server may protect its resources before serving a page requires a user (curl) to authenticate.
This page covers supping a name and password to gain access.
SSL This page repeats the above topics using encryption (https SSL)
GET & POST With Curl you can send data to another server using GET or POST this allows you to automatically fill in forms.
GET POST SSL AUTH Similar to above shows how to contend with authentication and SSL.

Top

cURL - CLI mode

Curl was originally a command line driven tool I have always found entering long command strings very painful and prone to error.

When Curl became a standard PHP module those days are long since gone. You can use scripts developed for a server and run them in CLI mode.

In terms of code there is little difference between CLI and running on a Server that said it’s easier on UniServer because it’s pre-configured to run straight out of the box. To run in CLI mode a small amount of configuration is required however it is worth the effort because you get a portable solution.

This tutorial covers the following Curl CLI topics: Tutorial covers the following Curl Server topics:

CLI Set-up How to configure Curl CLI for portability using PHP configuration file php-cli.ini. How to run CLI scripts using a Batch file
DtDNS Updater 1 Introduces a script to update your IP address at DtDNS. Includes a simulation script that produces DtDNS responses, validates all mandatory values.
DtDNS Updater 2 Concludes with a working template script to update IP address at DtDNS

Top

Summary

I have always hated writing preamble stuff never know what to say, well the next page gets straight down to business and looks at the minimum functions required to run Curl.

Top