Back to Top

How To Use Curl In Magento 2

Updated 20 January 2023

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😀.

Searching for an experienced
Magento 2 Company ?
Find out More
. . .

Leave a Comment

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


7 comments

  • Esha
    • ashutosh srivastava (Moderator)
  • Deepak
    • ashutosh srivastava (Moderator)
  • Idham Choudry
    • Webkul Support
      • Webkul Support
  • Back to Top

    Message Sent!

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

    Back to Home