0

I have created this code but now I want to convert this code to magento2 curl library

             $ch = curl_init(); 
            //curl_setopt($ch,CURLOPT_USERAGENT, $user_agent);
            curl_setopt($ch, CURLOPT_URL, $url);
            curl_setopt($ch,CURLOPT_HTTPHEADER,array('Content-type: application/x-www-form- 
             urlencoded'));
            curl_setopt($ch,CURLOPT_HTTP_VERSION,CURL_HTTP_VERSION_1_1);
            curl_setopt($ch,CURLOPT_POST, TRUE);
            curl_setopt($ch,CURLOPT_POSTFIELDS, $content);
            curl_exec($ch); 
            curl_close($ch);

1 Answer 1

0

Try the below code ..

<?php
    public function __construct(
        \Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
        \Magento\Framework\Json\Helper\Data $jsonHelper
    ) {
        $this->curlFactory = $curlFactory;
        $this->jsonHelper = $jsonHelper;
    }
     
    
    public function callPostAPI() {
     $dynamicUrl = $apiUrl . '?' . http_build_query($query);
     $httpAdapter = $this->curlFactory->create();
     $httpAdapter->write(
         \Zend_Http_Client::GET,
         $dynamicUrl,
         '1.1',
        ['Content-type: application/x-www-form- urlencoded']
    );
   $response = $httpAdapter->read();
   $body = \Zend_Http_Response::extractBody($response);
   $response = json_decode($body);
   print_r($response);
    
    ?>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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