First of all let’s start with the basic question.
What is cURL?
cURL stands for client URL which can also be written as curl.
curl is a command line tool to transfer data to and from a server. curl is also used to get/send information using APIs.
How to Use cURL in Magento 2?
Let's have a look into that.
Instead of using default PHP curl we can use Magento’s class Magento\Framework\HTTP\Client\Curl to work with HTTP protocol using cURL library.In other words Magento made it easy to use curl by implementing cURL class methods.
First, let’s create an instance of Magento\Framework\HTTP\Client\Curl.
<?php
/**
* Constructor.
*
* @param Magento\Framework\HTTP\Client\Curl $curl
*/
publicfunction__construct(
Magento\Framework\HTTP\Client\Curl$curl
){
$this->curl=$curl;
}
|
Instance of curl will be created by above code which is then used to access methods of Magento\Framework\HTTP\Client\Curl class
How to Make a GET request using curl
<?php
// get method
$this->curl->get($url);
// output of curl request
$response=$this->curl->getBody();
|
By using the Instance we created earlier $this->curl we will call get method
get() accepts one parameter $url which is the endpoint url.
$response will contain the response of the curl request.
How to Make a POST Request using curl
<?php
// post method
$this->curl->post($url,$params);
// output of curl requestt
$response=$this->curl->getBody();
|
$url Contain the endpoint url.
$params is an array of data that is being sent via the POST request
$response will contain the response of the curl request.
Additionally, we can also set headers, basic authorization, additional curl options and cookies in the cURL request.
We have to add these methods before the GET or POST method.
We will learn in detail how to set headers, basic authorization, additional curl options, and cookies in curl request.
How to Set curl Headers
There are two methods we can use to set the curl headers:
- addHeader
- setHeaders
Both addHeader() and setHeader() will add a header and value to the response.
The difference between set and add shows up when the header is there.
addHeader() adds an additional value, whereas setHeader() overwrites the existing value.
How to Set curl Header Using addHeader Method
addHeader() accepts two parameters. The first parameter is header name and second is the header value.
<?php
$this->curl->addHeader("Content-Type","application/json");
$this->curl->addHeader("Content-Length",200);
|
How to Set curl Header Using setHeaders Method
setHeader() accepts an array as a parameter.
<?php
$headers=["Content-Type"=>"application/json","Content-Length"=>"200"];
$this->curl->setHeaders($headers);
|
How to Set Basic Authentication in curl
Set the basic authorization using setCredentials().
<?php
$userName="User_Name";
$password="User_Password";
$this->curl->setCredentials($userName,$password);
|
It is equivalent to setting CURLOPT_HTTPHEADER value:
"Authorization : "."Basic ".base64_encode($userName.":".$password)
|
How to Set curl Option Using setOption Method
The setOption() accepts two parameters. The cURL option name and the cURL option value.
<?php
$this->curl->setOption(CURLOPT_RETURNTRANSFER,true);
$this->curl->setOption(CURLOPT_PORT,8080);
$this->curl->setOption(CURLOPT_TIMEOUT, 60);
|
How to Set curl option using setOptions Method
The setOptions() accepts a single parameter as an array.
<?php
$options=[CURLOPT_RETURNTRANSFER=>true,CURLOPT_PORT=>8080];
$this->curl->setOptions($options);
|
How to Set Cookies in curl
There are two methods we can use to set cookies in curl:
- addCookie
- setCookies
How to Set curl Cookies Using addCookie Method
The addCookie() accepts two parameters. The first parameter is the cookie name and the second parameter is cookie value.
<?php
$this->curl->addCookie("cookie-days","100");
$this->curl->addCookie("cookie-name", "Custom_Cookie");
|
How to Set curl Cookies Using setCookies Method
The setCookies() accepts one parameter as an array.
<?php
$cookies=["cookie-days"=>"100","cookie-name"=>"Custom_Cookie"];
$this->curl->setCookies($cookies);
|
Example of curl in Magento 2
<?php
namespace Mageants\TestCurl\Helper;
use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\HTTP\Client\Curl;
class Test extends AbstractHelper
{
/**
* @var Curl
*/
protected $curl;
public function __construct(
Curl $curl
) {
$this->curl = $curl;
}
public function testCurlRequest() {
$URL = 'www.example.com';
//set curl options
$this->curl->setOption(CURLOPT_HEADER, 0);
$this->curl->setOption(CURLOPT_TIMEOUT, 60);
//set curl header
$this->curl->addHeader("Content-Type", "application/json");
//get request with url
$this->curl->get($URL);
//Post request with url and parameter of an array type
$this->curl->post($URL,[<data>]);
//read response
$response = $this->curl->getBody();
return $response;
}
}
|
Wrapping Up: Using cURL in Magento 2
We hope the above guideline helps you to clearly understand How to Use cURL in Magento 2.
One can use the default cURL class for API Call and also instead of using the default PHP curl one can use Magento’s class.
We hope you have learned something useful from this article.
In case of any kind of problem with the above code implementation, you can feel free to contact us.