In magento 1 you can make get request like this: First Authorizing object-
public function authorize($url)
{
$cookie = $this->getCookieDir();
$user = $this->getApiUser();
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, '.....-API-client/1.0');
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_POSTFIELDS, http_build_query($user));
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$response = curl_exec($curl);
curl_close($curl);
return $response;
}
Then
public function get($url)
{
$cookie = $this->getCookieDir();
$curl = $this->initCh($url);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_USERAGENT, '.......-API-client/1.0');
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_COOKIEFILE, $cookie);
curl_setopt($curl, CURLOPT_COOKIEJAR, $cookie);
curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, 0);
$out = curl_exec($curl); # Initiate a request to the API and stores the response to variable
$code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
curl_close($curl);
return $out;
}
Now in Magento 2
\Magento\Framework\HTTP\Client\Curl::get($uri); calls makeRequest();
In makeRequest(); curl_init() is called and all curlOption() are set. So there shouldn't be anymore need to do like in Magento 1? If so how this is how it should be done? -
/**
* @param $url
* @return void
*/
public function doGet($url){
$user = $this->getApiUser();
$this->_curl->setHeaders(CURLOPT_POSTFIELDS, http_build_query($user));
//setting everything else
$this->_curl->get($url);
}