What is PHP CURL?

CURL stand for Client URL. CURL is a library to switch information via more than a few protocols like http, ftp, tftp and many others. First cURL library was released in 1997. CURL challenge was initially introducing by Daniel Stenberg. CURL mission has 2 section, cURL command line tool and libcurl library. Command line device is ordinarily on hand in Linux distributions for transferring knowledge on various protocol like HTTP, HTTPS, FTP and so on. Libcurl is library which support data transfer on exclusive protocol.

PHP CURL is used in multiple languages like PHP, ASP.NET etc. But in this article, we are discussing the PHP CURL. CURL supported in PHP version 4.0.2. Here is some general thing which we can do using PHP CURL library.

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

Where to Use PHP CURL?

The CURL extension to PHP is designed to allow you to use a variety of web resources from within your PHP script. By means of utilizing cURL we are able to send HTTP request utilizing quite a lot of method like GET, POST and we can also be accessing to third party API systems.

How to Use PHP CURL?

Here we explain the PHP cURL POST and GET requests let’s start with the cURL POST request.

  • Steps for PHP cURL POST written below follow these steps to execute a successful request:
  • Initialize curl session
  • Define your URL where you want to post your request. We can also assign this URL to an object or we can directly enter into URL section in set option parameter.
  • Define curl options which you want to execute with post option some of the important give below or you can read about more functions from below link: http://php.net/manual/en/function.curl-setopt.php
  • When we set all the functions then it’s time to execute our curl.
  • Close the curl and echo your object to check their response.

 

  • //Step 1to initialize curl
    
          $ch = curl_init();
    
    //Step 2 to set url where you want to post
    
          $url = ‘http://www.localhost.com’;
    
    //Step 3 set curl functions which are needs to you
    
          Curl_setopt($ch,CURLOPT_URL,$url);
    
          Curl_setopt($ch,CURLOPT_POST,true);
    
    Curl_setopt($ch,CURLOPT_RETURNTRANSFER,true);
    
    Curl_setopt($ch,CURLOPT_POSTFIELD,’postv1 = value1&postv2 = value2’);
    
    //Step 4 to execute the curl
    
          $result = curl_exec($ch);
    
    //Step 5 close curl
    
          Curl_close($ch);

     

    Steps for PHP cURL GET written below follow these steps to execute a successful request:

  • Define your URL from where you want to GET data. We can also assign this URL to an object or we can directly enter into URL section in set option parameter.
  • Initialize curl session
  • Define curl options which you want to execute with post option some of the important give below or you can read about more functions from below link: http://php.net/manual/en/function.curl-setopt.php

NOTE: Remember one thing if you are working on localhost then set this function

CURLOPT_SSL_VERIFYPEER=>false

When we set all the functions now it’s time to execute our curl.

  • Close the curl and echo your object to check their response.

 

//Step 1 define your url here from where you get data as well as the objects which you want to get

       $api = ‘http://www.localhost.com’;

//Step 2 initialize curl

       $ch = curl_init();

//Step 3 set curl functions which are needs to you and you can also set an array of functions as well

       Curl_setopt($ch,CURLOPT_URL,$api);

       Curl_setopt_array($ch, array(

       CURLOPT_RETURNTRANSFER,true,
       CURLOPT_HTTPHEADER,’write your access token path or any required path’,

       CURLOPT_SSL_VERIFYPEER,false,

       ));

//Step 4 run curl execute

      $result = =curl_exec($ch);

//Step 5 close url

      Curl_close($ch);

 

Summary

After research and brainstorming of PHP cURL I found some important advantages of cURL with third party API’s like REST etc. If anyone needs help about PHP cURL and their implementation contact us through hello@localhost . For further details about PHP cURL you can also use these links for reference:

  • http://php.net/curl_setopt
  • http://hayageek.com/php-curl-post-get
  • https://en.wikipedia.org/wiki/CURL