How to Make a cURL Request in Magento 2 the RIGHT WAY

While integrating third party APIs call, developers often make a cURL request in Magento 2. You can make a REST web API call using cURL request. Magento 2 provides Magento\Framework\HTTP\Client\Curl class to make a cUrl request. However, developers directly make a cURL request using plain PHP cURL command, which is discouraged.

If you scan your code against Magento 2 coding standard it will show you warning like “The use of function curl_init() is discouraged”.

Today we will see the right way of make a cURL request in Magento 2 using inbuild Magento\Framework\HTTP\Client\Curl class.
 

Make a POST cURL Request

<?php
namespace Codextblog\Demo\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\HTTP\Client\Curl;

class Inventory extends AbstractHelper
{

/**
* @var Curl
*/
protected $curl;

public function __construct(
  Context $context,
  Curl $curl,
) {
  parent::__construct($context);
  $this->curl = $curl;
}

public function makeACurlRequest() {

  $URL = 'www.example.com';
  $username = 'username';
  $password = 'password';
  $jsonData = '{}'

  //set curl options
  $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password);
  $this->curl->setOption(CURLOPT_HEADER, 0);
  $this->curl->setOption(CURLOPT_TIMEOUT, 60);
  $this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
  //set curl header
  $this->curl->addHeader("Content-Type", "application/json");
  //post request with url and data
  $this->curl->post($URL, $jsonData);
  //read response
  $response = $this->curl->getBody();
  return $response;
}	
}		

Make a GET cURL Request

<?php
namespace Codextblog\Demo\Helper;

use Magento\Framework\App\Helper\AbstractHelper;
use Magento\Framework\HTTP\Client\Curl;

class Inventory extends AbstractHelper
{

/**
* @var Curl
*/
protected $curl;

public function __construct(
  Context $context,
  Curl $curl,
) {
  parent::__construct($context);
  $this->curl = $curl;
}

public function makeACurlRequest() {

  $URL = 'www.example.com';
  $username = 'username';
  $password = 'password';

  //set curl options
  $this->curl->setOption(CURLOPT_USERPWD, $username . ":" . $password);
  $this->curl->setOption(CURLOPT_HEADER, 0);
  $this->curl->setOption(CURLOPT_TIMEOUT, 60);
  $this->curl->setOption(CURLOPT_RETURNTRANSFER, true);
  $this->curl->setOption(CURLOPT_CUSTOMREQUEST, 'GET');
  //set curl header
  $this->curl->addHeader("Content-Type", "application/json");
  //get request with url
  $this->curl->get($URL);
  //read response
  $response = $this->curl->getBody();
  return $response;
}	
}

For more options of cURL request please visit Magento 2 devdocs.

Want to ask a question or leave a comment?

Leave a Comment

(0 Comments)

All the comments are goes into moderation before approval. Please do not spam. Your email address will not be published. Required fields are marked *

Enjoy this post? Please support Us!

Follow us on twitter for daily new updates..

 

Want to become
a Magento 2 Expert?

If Yes! Then Subscribe to our newsletter and get weekly article to you email id.
Subscribe Here
SUBSCRIBE NOW
close-link

To Avoid Spam Downloads, We Want Your Email

We will send you download link right
away. Please submit form below.
SEND ME DOWNLOAD LINK
Close