In Magento 2, We can get details about curl from Curl.php file at location Magento\Framework\HTTP\Adapter\Curl.
Magento gives native functionality for CURL instead of using default php curl_init().
We can send a request to third party services using curl by below way,
Curl with POST request data, we need to pass \Zend_Http_Client::POST,
Curl with PUT request data, we need to pass \Zend_Http_Client::PUT,
and for CURL with GET Request pass \Zend_Http_Client::GET argument in below function.
In Php file, I h ave given example code for Uses of GET, POST and PUT rest API call.
<?php
public function __construct(
\Magento\Framework\HTTP\Adapter\CurlFactory $curlFactory,
\Magento\Framework\Json\Helper\Data $jsonHelper
) {
$this->curlFactory = $curlFactory;
$this->jsonHelper = $jsonHelper;
}
/*
* POST CURL API
*/
public function callPostAPI() {
$url = 'YOUR_API_URL'; //pass dynamic url
$requstbody = ['param1'=>A1,'param1'=>A2];
/* Create curl factory */
$httpAdapter = $this->curlFactory->create();
/* Forth parameter is POST body */
$httpAdapter->write(\Zend_Http_Client::POST, $url, '1.1', ["Content-Type:application/json","TOKEN:tokenhashvalue"],json_encode($requstbody));
$result = $httpAdapter->read();
$body = \Zend_Http_Response::extractBody($result);
/* convert JSON to Array */
$response = $this->jsonHelper->jsonDecode($body);
echo '<pre>';print_r($response); // response of request api call in array format
}
/*
* PUT CURL API
*/
public function callPutAPI() {
$url = 'YOUR_API_URL'; //pass dynamic url
$requstbody = ['param1'=>A1,'param1'=>A2];
/* Create curl factory */
$httpAdapter = $this->curlFactory->create();
/* Forth parameter is POST body */
$httpAdapter->write(\Zend_Http_Client::PUT, $url, '1.1', ["Content-Type:application/json","TOKEN:tokenhashvalue"],json_encode($requstbody));
$result = $httpAdapter->read();
$body = \Zend_Http_Response::extractBody($result);
/* convert JSON to Array */
$response = $this->jsonHelper->jsonDecode($body);
echo '<pre>';print_r($response); // response of request api call in array format
}
/*
* GET CURL API
*/
public function callGetAPI() {
$url = 'YOUR_API_URL'; //pass dynamic url
/* query parameter for POST BODY */
$query = [
'key'=> '1w343wewesfsdfsa3245354545',
'text' => 'Test test',
];
$dynamicUrl = $url.'?'.http_build_query($query);
/* Create curl factory */
$httpAdapter = $this->curlFactory->create();
$httpAdapter->write(\Zend_Http_Client::GET, $dynamicUrl, '1.1', ["Content-Type:application/json","TOKEN:tokenhashvalue"]);
$result = $httpAdapter->read();
$body = \Zend_Http_Response::extractBody($result);
/* convert JSON to Array */
$response = $this->jsonHelper->jsonDecode($body);
echo '<pre>';print_r($response); // response of request api call in array format
}
Most of the Developer used Curl Request in Magento 2 by Default PHP curl_init() way, Its Native PHP code and In Magento 2 We need to use Standard Magento Format Curl Request for GET,POST and PUT Call.
Below is plain PHP curl request code,
$connection = curl_init();
curl_setopt($connection, CURLOPT_URL, $url);
curl_setopt($connection, CURLOPT_POST, true);
curl_setopt($connection, CURLOPT_POSTFIELDS, json_encode($body));
curl_setopt($connection, CURLOPT_HTTPHEADER, array('Content-Type:application/json'));
curl_setopt($connection, CURLOPT_RETURNTRANSFER, true);
curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, false);
$output = curl_exec($connection);
$error = curl_error($connection);

it will send data automatically ?