How to use curl in Magento 2

In this article, you will be learning how one can use curl in Magento 2.

At first, you need to create an instance of “\Magento\Framework\HTTP\Client\Curl” in the Constructor as shown below:

/**
* @var \Magento\Framework\HTTP\Client\Curl
*/
protected $_curl;

/**
* Data constructor.
*
* @param \Magento\Framework\App\Helper\Context $context
* @param \Magento\Framework\HTTP\Client\Curl $curl
*/
public function __construct(
   \Magento\Framework\App\Helper\Context $context,
   \Magento\Framework\HTTP\Client\Curl $curl
) {
   $this->_curl = $curl;
   parent::__construct($context);
}

Make GET request using curl

//get method
$this->_curl->get($url);

//response will contain the output of curl request
$response = $this->_curl->getBody();

In the snippet given above,

  • $url : Contain the endpoint url.
  • $response : It will contain the response of the curl request.

Make POST request using curl

//post method
$this->_curl->post($url, $params);

//response will contain the output of curl request
$response = $this->_curl->getBody();

In the code given above,

  • $url : Contain the endpoint url.
  • $params : it’s an array, if you want to attach extra parameters in url.
  • $response :  It will contain the response of the curl request.

You can also add headers, basic authorization, additional curl options and cookies in the curl request. All you need to do is just add these methods before using get or post method. 

The detailed description on how to add headers, basic authorization, additional curl options, and cookies in curl request is given below.

Set curl headers

The two methods used to set the curl headers are:

  • addHeader
  • setHeaders

Set curl header using addHeader method

The addheader method accepts two parameters. First is the curl header name and second is the curl header value.

$this->_curl->addHeader("Content-Type", "application/json");
$this->_curl->addHeader("Content-Length", 200);

Set curl header using setHeaders method

The setHeaders method accepts one parameter as an array.

$headers = ["Content-Type" => "application/json", "Content-Length" => "200"];
$this->_curl->setHeaders($headers);

Set basic authorization in Curl

We can set the basic authorization using the setCredentials method.

$userName = "UserName";
$password = "Password";
$this->_curl->setCredentials($userName, $password);

It will be equivalent to setting CURLOPT_HTTPHEADER value

“Authorization : “. “Basic “.base64_encode($userName.”:”.$password)

Set additional curl options

The two methods used to set the curl options are:

  • setOption
  • setOptions

Set curl option using setOption method

The setOption method accepts two parameters. First is the curl option name and second is the curl option value.

$this->_curl->setOption(CURLOPT_RETURNTRANSFER, true);
$this->_curl->setOption(CURLOPT_PORT, 8080);

Set curl option using setOptions method

The setOptions method accepts one parameter as an array.

$options = [CURLOPT_RETURNTRANSFER => true, CURLOPT_PORT => 8080];
$this->_curl->setOptions($options);

Set cookies in curl

The two methods used to set the curl cookies in curl are:

  • addCookie
  • setCookies

Set curl cookies using addCookie method

The addCookie method accepts two parameters. First is the cookie name and second is the cookie value.

$this->_curl->addCookie("cookie-name-1", "123");
$this->_curl->addCookie("cookie-name-2", "test-cookie-value");

Set curl cookies using setCookies method

The setCookies method accepts one parameters as an array.

$cookies = ["cookie-name-1" => "100", "cookie-name-2" => "name"];
$this->_curl->setCookies($cookies);
  • Share :