Back to Top

Magento2 – How To Set Curl Headers And Options

Updated 18 December 2017

In this blog we will discuss how to set curl headers and other options in Magento2.
I am assuming that you already know how to use curl in Magento2.
If you don’t know how to use curl in Magento2, You can refer to the blog How To Use Curl In Magento 2

Initialize Curl in Magento2

/**
 * @param \Magento\Framework\HTTP\Client\Curl $curl
 */
public function __construct(
    \Magento\Framework\HTTP\Client\Curl $curl
) {
    $this->curl = $curl;
}

Set Basic Authorization in Curl – Magento2

In Magento2 we can set basic authorization using 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 Curl Headers in Magento2

There are two methods in Magento2 to set curl headers.

  • addHeader
  • setHeaders

Setting curl header using addHeader method
addheader method accepts two parameters. First is curl header name and second is curl header value.

Searching for an experienced
Magento 2 Company ?
Find out More
$this->curl->addHeader("Content-Type", "application/json");
$this->curl->addHeader("Content-Length", 200);

Setting curl header using setHeaders method
setHeaders method accepts one parameters as array.

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

Set Curl Options in Magento2

There are two methods in Magento2 to set curl options.

  • setOption
  • setOptions

Setting curl option using setOption method
setOption method accepts two parameters. First is curl option name and second is curl option value.

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

Setting curl options using setOptions method
setOptions method accepts one parameters as array.

$headers = [CURLOPT_RETURNTRANSFER => true, CURLOPT_PORT => 8080];
$this->curl->setHeaders($headers);

Set Cookies in Curl – Magento2

There are two methods in Magento2 to set curl cookies.

  • addCookie
  • setCookies

Setting curl cookie using addCookie method
addCookie method accepts two parameters. First is cookie name and second is cookie value.

$this->curl->addCookie("test-cookie-days", "100");
$this->curl->addCookie("test-cookie-name", "name");

Setting curl cookie using setCookies method
setCookies method accepts one parameters as array.

$cookies = ["test-cookie-days" => "100", "test-cookie-name" => "name"];
$this->curl->setCookies($cookies);

Please note that you need to call all these methods before calling get or post method.
I hope this blog will help you. 🙂
If you have any doubt or query, comment below.

. . .

Leave a Comment

Your email address will not be published. Required fields are marked*


3 comments

  • Lalit Jeengar
    • Webkul Support
      • Lalit Jeengar
  • Back to Top

    Message Sent!

    If you have more details or questions, you can reply to the received confirmation email.

    Back to Home