
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 can we 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
Make 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.
Make 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 GET or POST method.
We will learn in detail how to set headers, basic authorization, additional curl options, and cookies in curl request
curl headers
These two methods are used 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.
Set curl header using add Header method
addHeader() accepts two parameters. 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); |
Set curl header using set Headers method
setHeader() accepts an array as a parameter.
<?php $headers=["Content-Type"=>"application/json","Content-Length"=>"200"]; $this->curl->setHeaders($headers); |
Set basic authorization 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) |
Set Curl option using set Option 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); |
Set Curl option using set Options method
The setOptions() accepts Single parameter as an array.
<?php $options=[CURLOPT_RETURNTRANSFER=>true,CURLOPT_PORT=>8080]; $this->curl->setOptions($options); |
Set cookies in curl
These two methods are used to set the curl cookies :
- addCookie
- setCookies
Set curl cookies using add Cookie method
The addCookie() accepts two parameters. First parameter is cookie name and second parameter is cookie value.
<?php $this->curl->addCookie("cookie-days","100"); $this->curl->addCookie("cookie-name", "Custom_Cookie"); |
Set curl cookies using set Cookies method
The setCookies() accepts one parameters 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,[]); //read response $response = $this->curl->getBody(); return $response; } } |
Conclusion:
We hope above guideline helps you to clearly understand How to Use curl in Magento 2.
One can use deafault curl class for API Call and also instead of using default PHP curl one can use Magento’s class. We hope you have learned something useful from this MageAnts article.
In case of any kind of problem with the above code implementation, you can contact us or let us know in comment section.