PHP cURL Tutorial for Beginner to Pro Developer

php-curl

If you are linux user then you probably know about what is cURL. In our  tutorial “PHP cURL” we will learn what is curl. How to use curl in php, how to install curl library in php.

What is 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 page on Wiki 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 some 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

How to 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 phpinfo snapshot

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 :-)

First PHP Curl Example

Below is a very basic code written using libcurl.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.google.co.in");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
curl_close($curl);
print $result;
?>

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:
<?php
$curl = curl_init(); //initialise
?>

Below is example of initialising the curl with URL

<?php
$curl = curl_init('http://www.google.co.in'); //initialise
?>

Above code is equivalent to below
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.google.co.in");
?>

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.
<?php
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, "http://www.google.co.in"); //Setting option url
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //setting option Return Transfer
?>

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.
<?php
$curl = curl_init('http://beingacoder.com');
$result = curl_exec($curl); // will return true or false
curl_close($curl);
print $result;
?>

4) curl_getinfo : Method will return detail information about curl transfer session. For example:
<?php
$curl = curl_init('http://beingacoder.com');
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
$result = curl_exec($curl);
$info = curl_getinfo($curl);
print_r($info);
curl_close($curl);
?>

Above code will return below detail output regarding this session:

Array
(
    [url] => http://beingacoder.com/
    [content_type] => text/html; charset=UTF-8
    [http_code] => 200
    [header_size] => 579
    [request_size] => 56
    [filetime] => -1
    [ssl_verify_result] => 0
    [redirect_count] => 0
    [total_time] => 2.028261
    [namelookup_time] => 0.565708
    [connect_time] => 0.714323
    [pretransfer_time] => 0.714463
    [size_upload] => 0
    [size_download] => 52254
    [speed_download] => 25762
    [speed_upload] => 0
    [download_content_length] => -1
    [upload_content_length] => 0
    [starttransfer_time] => 1.42192
    [redirect_time] => 0
    [redirect_url] => 
    [primary_ip] => 104.28.6.101
    [certinfo] => Array
        (
        )

    [primary_port] => 80
    [local_ip] => 192.168.1.103
    [local_port] => 60286
)

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.

<?php
$curl = curl_init(‘http://404.techflirt.com’);
$result = curl_exec($curl);
if(false == $result){
echo curl_error($curl);
}
curl_close($curl);
?>

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