In this article, you will be learning how one can use curl in Magento 2 development.
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);
FAQs
cURL in Magento2 or client for URL is a command-line tool. It is used to interact with web pages through HTTP requests to external servers and getting responses. For example, cURL can send secure requests to third-party payment gateways. The cURL can be run by creating a command through the cURL command syntax.
cURL vs HTTP cannot be compared as cURL is a command line used to make different types of HTTP requests and receive responses. HTTP is a protocol for client and server communication. When cURL requires data, it sends an HTTP request through the command line, directing the servers to initiate HTTP data transfers.
cURL or Postman both can be better depending on the requirements. cURL is a simple tool that accelerates HTTP requests without any complicated configuration choices, making it quick and efficient. On the other hand, Postman is more flexible and versatile which allows developers to create custom APIs according to their requirements.
No, cURL is not a REST API. cURL is a command-line that is used to send requests and receive network responses. REST API, on the other hand, is an application program designed for the REST software architecture. You can use cURL to send REST requests, but it isn’t the API itself.
Post a Comment
Got a question? Have a feedback? Please feel free to leave your ideas, opinions, and questions in the comments section of our post! ❤️