cURL and libcurl use in php

cURL and libcurl use in php

Introduction

That article is for those web-developers who want to automatize file transferring in the network or interact with other Internet services. For understanding that article you have to know the principle of client-server strategy working and PHP syntax.

cURL and libcurl are libraries that allow server file transferring to the remote computer, using a lot of Internet protocols. These libraries have a flexible setting and allow query executing. Using these libraries web-server can become a client of any service based on HTTP protocol, for example: XML-RPC, SOAP, or WebDAV.

cURL and libcurl

cURL is a shortening from "Client URLs". It was created by Daniel Stenberg in 1998. Libcurl is a library that provides simple API-interface to cURL functionality. That library is safe in the multipole area, IPv6-enabled, and supports constant connections.

cURL and libcurl can be used for information transferring using such protocols as HTTPS, FTP, FTPS, GOPHER, LDAP, DICT, TELNET and FILE. They support all *nix systems as well as Windows, OS/2, BeOS.

curl library is an Open Source product with original MIT/X license. It allows using that packet in commercial and noncommercial purposes. It is very important to understand that cURL has nothing in common with Curl Corporation.

cURL installation

Windows

As any complementary module cURL requires installed PHP distributive. For cURL installation copy files php4ts.dll, ssleay32.dll, php_curl.dll, msvcrt.dll from DLL catalog to the Windows system directory. As usual it is:

c:\windows\system for Windows 9x/Me
c:\winnt\system32 for Windows NT/2000
c:\windows\system32 for Windows XP.

After that you have to recomment the line

;extension=php_curl.dll 

in the php.ini file or load the module dynamically during the script working.

<?php  
dl
("php_curl.dll");  
?>

UNIX

cURL uses openssl library for SSL connections. That’s why you have to install SSL on the server. If openssl library isn’t found during the cURL installation, cURL will be installed without SSL connection support.

cURL installation has the following steps: ./configure, make, make install.

After that PHP must be recompiled with option --with-curl

Example of cURL use

cURL use is very simple. The following example calls the web-page and outputs to the stdout.

$ curl -L zend.com

Also there is an opportunity of using cURL with the help of PHP. Next example calls three pages and displays them on the screen:

<?php  
$var 
= echo shell_exec("/usr/bin/curl -L 
http://www.zend.com http://zend.com/developers.php http://zend.com/zend/tut/"
);  
?>

Example of cURL use in PHP

For libcurl using you have to:

  • Initialize cURL session
  • Set cURL options
  • Execute the query
  • Finish cURL session

Here we have some practical examples of cURL use for POST-query, HTTP- authentication, FTP-session generation.

<?php  
$url 
"http://www.amazon.com/exec/obidos/search-handle-form/002-5640957-2809605";  
$ch curl_init();  
curl_setopt($chCURLOPT_URL,$url); // set url to post to  
curl_setopt($chCURLOPT_FAILONERROR1);  
curl_setopt($chCURLOPT_FOLLOWLOCATION1);// allow redirects  
curl_setopt($chCURLOPT_RETURNTRANSFER,1); // return into a variable  
curl_setopt($chCURLOPT_TIMEOUT3); // times out after 4s  
curl_setopt($chCURLOPT_POST1); // set POST method  
curl_setopt($chCURLOPT_POSTFIELDS
"url=index%3Dbooks&field-keywords=PHP+MYSQL"); // add POST fields  
$result curl_exec($ch); // run the whole process  
curl_close($ch);   
echo 
$result;  
?>
<?php  
// HTTP authentication  
$url "http://www.example.com/protected/";  
$ch curl_init();      
curl_setopt($chCURLOPT_RETURNTRANSFER1);   
curl_setopt($chCURLOPT_URL$url);   
curl_setopt($chCURLOPT_USERPWD"myusername:mypassword");   
$result curl_exec($ch);   
curl_close($ch);   
echo 
$result;  
?>  
<?PHP  
// FTP this script to a server  
$fp fopen(__FILE__"r");  
$url "ftp://username:password@mydomain.com:21/path/to/newfile.php";  
$ch curl_init();      
curl_setopt($chCURLOPT_URL$url);   
curl_setopt($chCURLOPT_RETURNTRANSFER1);   
curl_setopt($chCURLOPT_UPLOAD1);   
curl_setopt($chCURLOPT_INFILE$fp);   
curl_setopt($chCURLOPT_FTPASCII1);   
curl_setopt($chCURLOPT_INFILESIZEfilesize(__FILE__));   
$result curl_exec($ch);   
curl_close($ch);   
?>

If you have some problems with cURL use you have to add the following lines before curl_close call for getting the report about the last executed query:

<?php   
print_r
(curl_getinfo($ch));   
echo 
"\n\ncURL error number:" .curl_errno($ch);   
echo 
"\n\ncURL error:" curl_error($ch);   
// ...close cURL handle ($ch) below  
?>

Using of cURL or libcurl depends on circumstances. If you write the script executed from the command line and your provider doesn’t support libcurl it is better to use cURL. In other cases use libcurl.


 

  • Top