Coding, PHP & MySQL

Grabbing Content from Web Using PHP and cURL

cURL or/and libcurl are PHP libraries, which allows the user to write scripts or programs that can connect to and transfer files from a web server to a remote computer using the TCP/IP and HTTP protocols. Both of these libraries are almost the same and do the same basic thing. Both, cURL and libcurl, are very flexible when it comes to configuration options. Both the scripts can be used to send virtually any and all client-server requests.

cURL is shorthand for “Client URLs”. cURL was developed and written by Stanberg and Daniel in the year 1998. At that time, its sole existence was as a basic command line tool. Both of these libraries are IPv6 compliant and support importunate connections.

HTTP, FTP, FTPS, HTTPS, GOPHER, LDAP, TELNET, and other such protocols can be used for transferring files and data to and from the remote clients and web servers using cURL and libcurl. Since PHP, which can now can be installed and used on a number of operating systems, including Windows and Mac, the libraries can now be used by developers using such supported operating systems.

Installing cURL is not that difficult, for PHP version 4.2.3+, cURL version 7.9.0+ is needed.

Windows based Installation, as with other PHP libraries on Windows, requires external cURL extensions to be loaded in the in the extension directory, which is usually C:PHPext. Files needed for using curl are php_curl.dll, php4ts.dll, ssleay32.dll, and msvcrt.dll.

If you do not know the extension directory of your PHP installation, you can copy these files in to the system32 folder of your Windows NT/XP installation, which has a path C:Windowssystem32.

As a last step, before you start using cURL, you will have to edit the php.ini file. This file can be found in C:Windows (installation) folder. At first, you need to open the PHP.ini file in a suitable editor like notepad and uncomment the line, which states, extension=php_curl.dll.(Shown below)

PHP CURL

If you want an easier way to work with cURL, then you can follow the code mentioned below:


Just include the library within the code.

UNIX installation of the library will require the openSSL library to be installed. This is needed to be done since the cURL library uses SSL connection when transferring data and files from secured web servers and clients. Download, unzip, ./configure, make install and make. Once done, you will have to recompile PHP to include cURL support.

To check your cURL installations, use phpinfo()

A Simple PHP and cURL example:

In order to have communication with other remote servers and machines using cURL, we utilize the options that we have. Firstly we would set these cURL options. The order in which you set these in your code is irrelevant.

Second, we must execute these options in a cURL session that we opened. Third, we close this cURL session, execute the options in the cURL session, and close the curl session.

$connect_to = “www.thissite.com”;  // the website that you want to connect to…
$ch=curl_init(); //initialize the curl library and handle.
curl_setopt($ch, CURLOPT_URL, $connect_to); // set curl options to post to
curl_setopt($ch, CURLOPT_FAILONERROR, 1); //stop if an error occurs
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1); //set the allow redirects option
curl_setopt($ch, CURLOPT_RETURNTRANSFER,1); // return the response and store in //a variable
curl_setopt($ch, CURLOPT_TIMEOUT, 3); // set the curl option to time out after 4s

$result = curl_exec($ch); // execute the cURL request with the options set as above and //store the response in the variable $result
curl_close($ch); // once executed, close the cURL session
echo $result; // display the response received and stored in the var $result.

www.php.net, cURL www.sourceforge.net/projects/curl/