1

This is my curl code. How to write the same in Magento 2

$ch = curl_init();

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch,CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, $method);

if($method == 'POST') {

curl_setopt($ch, CURLOPT_POSTFIELDS,$data);

}

curl_setopt($ch, CURLOPT_FOLLOWLOCATION ,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, array( 'Content-Type: application/json', 'Accept: application/json', 'Authorization:'.$key ));
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 120);
$result = curl_exec($ch);
curl_close($ch);
return json_decode($result); 

Please help.

0

The below code is help to use curl in magento2.

$client = new \Zend_Http_Client();      
$client->setUri($url);
if($method == 'POST'){
    $client->setParameterPost($data);   
}
$headers = ["Content-Type" => "application/json", "Accept" => "application/json",CURLOPT_RETURNTRANSFER => true, CURLOPT_FOLLOWLOCATION => 1,"Authorization" => $key,"CURLOPT_CONNECTTIMEOUT"=>120,"CURLOPT_SSL_VERIFYPEER"=>false];
$client->setHeaders($headers );
$response = $client->request($method)->getBody(); 

You will print the response by print_r($response);

  • can you provide an example without using new object. – Rama Chandran M Jul 12 '18 at 6:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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