PHP cURL

cURL

cURL stand for Client URL. cURL is a library to transfer data via various protocol like http, ftp, tftp etc. By using cURL we can send HTTP request using various method like GET, POST etc. First cURL library was released in 1997. cURL project was originally started by Daniel Stenberg. cURL project has 2 part, cURL command line tool and libcurl library. Command line tool is generally available in Linux distributions for transferring data on various protocol like HTTP, HTTPS, FTP etc. Libcurl is library which support data transfer on different protocol.

Libcurl is available with various language including PHP. For more information about the cURL project you can visit cURL or cURL page.

All cURL feature in PHP is available through libcurl. In the series of PHP cURL we will explore around the various aspect of libcurl in php.

Below are the general thing which we can do using php curl library

  1. Downloading HTML from URL in our PHP Code.
  2. Sending POST request on any URL. 
  3. Calling Rest full API.

cURL support in php is introduced from php version 4.0.2

Install cURL Library in PHP

PHP has extension which need to be installed and enable to use cURL features. But to use the php cURL functionality you need to install libcurl package on your system/server. Most of the Linux system come with the by default installation of libcurl. Also if you are going to use PHP using XAMPP or WAMPP then also you no need to think about the libcurl package installation.

To check whether curl is enable or not you can use phpinfo() command. If your phpinfo page has below section appeared then curl is enable with your PHP.

php-curl

If above section is not available then you need to enable curl. Below are the steps to enable curl extension in php

  • If you are using WAMPP or XAMPP then open your php.ini file and uncomment below line
    ;extension=php_curl.dll
  • If you have manually installed PHP then you can install php_curl library and then add below line in your php.ini
    extension=php_curl.dll
  • If you are using php by compiling the source code then please compile with parameter –with-curl.

Hope we are all set to go. Let us see some magic with php curl:

PHP Curl Example

Below is a very basic code written using libcurl.

Above code is fetching homepage of url http://www.google.co.in .  Below thing we are doing in above code.

  1. Initialising curl.
  2. Setting option URL to google.com.
  3.  Setting option to transfer data as string.
  4. Executing Curl and taking output string in a variable.
  5. Closing the curl handle.
  6. printing result.

In above code we have used some methods like curl_init , curl_setopt, curl_close which are function available with curl library of php.

cURL Function

PHP cUrl library provides 29 function. You can get full list of php curl function here. Let us look at some of imporent cURL function:

  1. curl_init
  2. curl_setopt & curl_setopt_array
  3. curl_exec
  4. curl_getinfo
  5. curl_close
  6. curl_error
  7. curl_errorno

1) curl_init : curl_init function is used to initialise  the new session. curl_init returns a cURL handle. Curl handle is passed in various other curl method call like setting option, executing the curl session etc.  Curl_init function has a optional URL parameter.  Below is the example of curl_init:

Below is example of initialising the curl with URL

Above code is equivalent to below

2) curl_setopt and set_opt_array : Both method are used to set option like http header, timeout  with your curl session. In curl_setopt need to pass options one by one however in curl_setopt_array option more than one option can be passed simultaneously. Both curl_setopt and curl_setopt_array need curl session to be initialised.

Here you can get complete list of option available with php curl library.

3) curl_exec : Curl_exec function is to execute the curl session. Returns true if session is executed succesffully otherwise false. But if you will set option CURL_RETURNTRANSFER true then it will return output of the curl session.

4) curl_getinfo : Method will return detail information about curl transfer session. For example:

Above code will return below detail output regarding this session:

5) curl_close : Function used to close curl transfer session. Function require curl handle as input parameter.

6) curl_error : Function return last error of the curl_session. If error will not occur then it will return empty string.

7) Curl_errorno : Function return last error number of curl_session.