Quantcast
Channel: Webkul Blog
Viewing all articles
Browse latest Browse all 5556

How To Use Curl In Magento 2

$
0
0

How To Use Curl In Magento 2 -You are in right place if you want to use curl in magento style.i am going 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 => contain 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.

Hope so it will help you.


Viewing all articles
Browse latest Browse all 5556

Latest Images

Trending Articles



Latest Images