CURL – Client Url Library for PHP
CURL is a very essential library in PHP. In some website, like linkedin.com if you have an account in there you’ll see, there is an option to import contacts from yahoo, gmail or hotmail. If you put your account name and password they will import the contacts from your account.
Do you know how’ll they do so. Yeah, there may be many libraries. But this type of work could be done using the CURL libary. I’ve done this type of works 2 months ago. So, I think I should share it to novice or intermediate PHP developer.
What is CURL?
- It is a library created by Daniel Stenberg.
- It allows you to connect and communicate to many different types of servers with many different types of protocols.
- libcurl currently supports the http, https, ftp, gopher, telnet, dict, file, and ldap protocols
Before using CURL, must check that it’s installed in server or your localhost. Here, I’ve given a simple example. This example uses the cURL functions to fetch the example.com homepage into a file:
$ch = curl_init('http://www.example.com/'); $fp = fopen('example_homepage.txt', 'w'); curl_setopt($ch, CURLOPT_FILE, $fp); curl_setopt($ch, CURLOPT_HEADER, 0); curl_exec($ch); curl_close($ch); fclose($fp);
- First initialize a cURL session using the curl_init()
- Set all your options for the transfer via the curl_setopt()
- Now you can execute the session with the curl_exec()
- Finally finish off your session using the curl_close().
For more information: please see PHP Manual.