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.
$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.
3 comments
Hello Lalit,
Let us know the Magento version and code you are using.
Thanks for prompt reply i am using magento 2.1.5 and the code is below
this code is run in a catalog_product_save_after observer
$this->_Curl=$this->_objectManager->create(‘MagentoFrameworkHTTPClientCurl’);
$curlPostData=array(“sku”=>$sku);
$curlUserName=”usename”;
$curlPassword=”password”;
$contentType=”multipart/form-data”;
$curlURL=”http://domainname/api”;
$this->_Curl->setCredentials($curlUserName,$curlPassword);
$this->_Curl->addHeader(“Content-Type”, $contentType);
$this->_Curl->setOption(CURLOPT_RETURNTRANSFER, true);
$this->_Curl->setOption(CURLOPT_ENCODING, “”);
$this->_Curl->setOption(CURLOPT_MAXREDIRS, 10);
$this->_Curl->setOption(CURLOPT_TIMEOUT, 30);
$this->_Curl->setOption(CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);
$this->_Curl->post($curlURL,$curlPostData);
$response=$this->_Curl->getBody();