I am trying to to create the curl request but it is not working. It is giving the error

Uncaught Error: Class 'Custom\Rma\Helper\Varien_Http_Adapter_Curl' not found


$header = array(
                "cache-control: no-cache",
                "content-type: application/json"
        $request = json_encode($sfData);
        $url = "url_here";
        $options = array(
            CURLOPT_HTTPHEADER => $header,
            CURLOPT_POSTFIELDS => $request
        $curl = new Varien_Http_Adapter_Curl();
        $curl->write(Zend_Http_Client::GET, $url, '1.0');
        $response = $curl->read();
        $err = $curl->error();
        if ($err) {
            return false;
        } else {                 
            return true;

As i want to change my code according to the ECG so i have converted the PHP curl functions to above code. And this code is not working.

  • The code you have used is for Magento 1. – sv3n Mar 5 '18 at 10:44
  • @sv3n this code is used in magento 2 – Nitin Pawar Mar 5 '18 at 10:53
  • I see, but the code you use is for Magento 1 ... so you'll see an error. – sv3n Mar 5 '18 at 11:25
  • @sv3n any idea how to use curl in magento 2. – Nitin Pawar Mar 5 '18 at 12:34

initialize variable

 * @var \Magento\Framework\HTTP\Client\Curl
protected $_curl;

initialize construct parameter

public function __construct(
    PsrLogger $logger,
    \Magento\Framework\HTTP\Client\Curl $curl
    $this->_curl = $curl;
    $this->_logger = $logger;

in your function

        $url = "https://url.com";
        //if the method is get
        //if the method is post
        $this->_curl->post($url, $params);
        //response will contain the output in form of JSON string
        $response = $this->_curl->getBody();
       // $responseArr = $response->__toArray();
        $em  =  array();
    catch (\Exception $e) {
        $this->_logger->critical('Error Curl', ['exception' => $e]);

check this its working for me. check debug.log to verify the response

  • Thanks for your answer, how can i get the curl_error. Is there another function? I want to return the false if curl_error – Nitin Pawar Mar 5 '18 at 10:57

You can call curl by injecting \Magento\Framework\HTTP\Client\Curl class.

For looking into how to call various methods of curl you can look into file vendor\magento\framework\HTTP\Client\Curl.php.

  • You can see some usage at: app/code/Magento/AdminNotification/Model/Feed.php. Apparently they use CurlFactory. – steros Mar 5 '18 at 10:12

Not the answer you're looking for? Browse other questions tagged or ask your own question.