What is Curl in php? how to use curl in php?

how to enable cURL in WAMP and XAMPP server.


cURL is a library that lets you make HTTP requests in PHP ,In the part of web application development cURL must be enable in our web server, Because it is important to handle third party api request like SMS Gateway many more task you can easily handle with cURL.

Enabling cURL in WAMP Server

Step.1: Open the WAMP\bin\php\(php version)\php.ini file in any text editor.
Step.2: Search for curl, Uncomment the below line in the php.ini file by removing the semicolon (;).

;extension=php_curl.dll
Step.3: Open the WAMP\bin\Apache\(apache version)\bin\php.ini file in any text editor.
Step.4: Search for curl, Uncomment the below line in the php.ini file by removing the semicolon (;).

;extension=php_curl.dll
Note: Don?t forget to restart WAMP Server.

Enabling cURL in XAMPP Server

Step.1: Open the xampp\apache\bin\php.ini file in any text editor.
Step.2: Search for curl, Uncomment the below line in the php.ini file by removing the semicolon (;).

;extension=php_curl.dll
Note: Don?t forget to restart XAMPP Server.

Enabling cURL in Ubuntu Apache Server

Step.1: First install curl

sudo apt-get install php5-curl
Step.2: Restart Apache server.

sudo /etc/init.d/apache2 restart
OR
sudo service apache2 restart
Double check, is curl enable in php.ini file or not
Step.3: Open the etc/(php version)/(apache version)/php.ini file.
Step.4: Check following line should be uncomment, (;) should be removed.

extension=php_curl.dll

what is PHP cURL?

http://www.techflirt.com/php/php-curl/index.html

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.

cURL support in php is introduced from php version 4.0.2

curl is an open source command line tool and library for transferring data with URL syntax, supporting DICT, FILE, FTP, FTPS, Gopher, HTTP, HTTPS, IMAP, IMAPS, LDAP, LDAPS, POP3, POP3S, RTMP, RTSP, SCP, SFTP, SMB, SMTP, SMTPS, Telnet and TFTP. curl supports SSL certificates, HTTP POST, HTTP PUT, FTP uploading, HTTP form based upload, proxies, HTTP/2, cookies, user+password authentication , file transfer resume, proxy tunneling and more.

How to install cURL Library in PHP

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.


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.
  • File name : index.php

    <?php
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://itechtuto.com");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    print $result;
    ?>

    Above code is fetching homepage of url http://itechtuto.com. Below thing we are doing in above code.

  • Initialising curl.
  • Setting option URL to itechtuto.com.
  • Setting option to transfer data as string.
  • Executing Curl and taking output string in a variable.
  • Closing the curl handle.
  • printing result.

  • cURL Function

    PHP cUrl library provides 29 function.

  • curl_init
  • curl_setopt & curl_setopt_array
  • curl_exec
  • curl_getinfo
  • curl_close
  • curl_error
  • 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://itechtuto.com'); //initialise
    ?>

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

    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://itechtuto.com"); //Setting option url
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //setting option Return Transfer
    ?>

    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://www.techtuto.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://itechtuto.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://www.techflirt.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.itechtuto.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.


    PHP Curl Examples

    To use curl we need to follow below steps:

  • Init curl
  • Set options.
  • Execute curl
  • Get result
  • Basic PHP Curl Examples

    Below are some basic php curl examples.

  • 1. Retrieving Webpage
  • 2. Calling Rest API
  • 3. Sending POST request.
  • 4. Sending Receiving XML
  • 5. Uploading File
  • 6. Passing Custom HTTP header.
  • Retrieving Webpage in PHP Using Curl

    The most basic example of the curl is retrieving webpage using curl. Below is the code to fetch google.com homepage:


    <?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;
    ?>

    Calling a Rest API

    Rest APIs are nothing but a http request. So you can call rest API by curl using same way which you apply for retrieving the webpage. In most of the cases rest API send output in json format, but not all the time. In some cases rest api need some extra header like authentication, response type etc.

    In below example we are calling amazon auto complete api and searching for word facebook. Output of the below code is JSON so we are decoding json in PHP array/object.

    <?php
    $curl = curl_init();
    curl_setopt($curl, CURLOPT_URL, "http://completion.amazon.com/search/complete?search-alias=aps&client=amazon-search-ui&mkt=1&q=facebook");
    curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curl);
    curl_close($curl);
    print_r(json_decode($result));//return amazone autocomplete suggestion
    ?>





    Previous Next


    Trending Tutorials




    Review & Rating

    0.0 / 5

    0 Review

    5
    (0)

    4
    (0)

    3
    (0)

    2
    (0)

    1
    (0)

    Write Review Here