If you want to know how we can use Curl in Magento2 then You are in the right place. I am going to explain how you can use it.
check the below code
/** * @var \Magento\Framework\HTTP\Client\Curl */ protected $_curl; /** * @param Context $context * @param \Magento\Framework\HTTP\Client\Curl $curl */ public function __construct( Context $context, \Magento\Framework\HTTP\Client\Curl $curl ) { $this->_curl = $curl; parent::__construct($context); } public function execute() { //if the method is get $this->_curl->get($url); //if the method is post $this->_curl->post($url, $params); //response will contain the output in form of JSON string $response = $this->_curl->getBody(); }
$url => contains the endpoint URL.
$params => it’s an array if you want to attach extra parameters in URL.
$response => it will contain the output in JSON form.
If you want to set Authorization, Headers, Options, and cookies in your curl request then you can check out this blog:- Click Here
I hope you like this Blog Thanks😀.
7 comments
Magento\Framework\HTTP\Adapter\Curl you can open this class and can check write method for making curl request, and read method to read the response.
https://webkul.com/blog/magento2-set-curl-headers-options/
here we have written how to set headers and options.