. Updated Daily. Editions SDA India   SDA Indonesia
BUSINESS ENTERPRISE SOLUTIONS ARCHITECTURE INFORMATION SECURITY WIRELESS & MOBILITY DATA & STORAGE DEVELOPMENT HARDWARE













News

Wednesday, 7 February 2007

Article: cURL with PHP

 

 

Ian Gilfillan over at the PHP Builder talks about using cURL with PHP. For the unacquainted, Client URL or curl is a PHP extension, which allows you to communicate with other servers, using a range of protocols.

Ian tells you about libcurl, the libraries behind the PHP cURL extension. Libcurl supports many protocols including HTTP, HTTPS, FTP, TELNET, FILE, LDAP, DICT, GOPHER, HTTPS, HTTPS certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, cookies, and user: password authentication.

But cURL does not come with PHP by default, he says. PHP needs at least CURL 7.0.2-beta or higher, PHP 4.2.3, requires at least CURL 7.9.0 or higher, PHP 4.3.0 needs CURL 7.9.8 or higher and PHP 5.0.0 needs at least version 7.10.5 or greater, he verifies. He shows you how to use a cURL script in the following:

A First cURL Script

With an example code he demonstrates how to create a simple and recipient script in cURL. He provides details of the script saying that the curl1.php script connects to the URL supplied (recipient.php), handing over control to that script. This is not particularly useful, but it introduces you to three of the main functions—curl_init(), which returns a cURL handle results usually being assigned to a variable called , curl_exec(), which executes the session you have set up, and curl_close(), which frees all resources associated with the session. curl_exec() that returns a boolean in the above context, but most often you will ask it to return the output from the call.


A Simple cURL Script to Look Up the Meaning of a Word

One of the protocols cURL supports is ‘dict’, he says. Taking an example code he creates a simple tool to return the definition of a word from dict.org's database, connecting to dict.org with the dict protocol. He explains that using something similar you can create an online dictionary, or integrates a word search into other applications. He further informs you that this article is not about using forms to submit values to a PHP script. He assumes you already know how to do that, and can build upon this skeleton, taking proper care to validate all variables.

Accessing Password-protected Pages

The next scripts assume that the example.co.za/protected directory requires a username and password to access, and he demonstrates the two simple ways to pass the authentication data to the remote server. Firstly, he uses the CURLOPT_USERPWD option and than he shows an alternative way to handle the password protection by putting the username and password in the URL, in the same way as you can do in a browser.

Returning File Information from the Remote Server

cURL allows you to return some potentially useful information, such as the HTTP_CODE for example, 200 for success, 403 for forbidden, 500 for internal server error, he informs. See the full list here. He also displays the full list with a sample output.

Posting Variables to a Remote Script

With the help of code examples he shows you how to POST values to a remote script. It also allows you to script your interaction with a remote form.

FTP'ing a File to a Server

In the final step, he gives an example on how to FTP a file to a server.

 

Read the article

 
 
print save email comment

print

save

email

comment

 
 

Search SDA Asia

Free eNewsletter

SDA Asia Magazine Free Download
 
 
 
Copyright @ 2009 SDA Asia Magazine - All Right Reserved Privacy Policy | Terms of Use