Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
Public Member Functions | Data Fields
CurlClient Class Reference

Public Member Functions

 get ($url, $data=[], $headers=[])
 
 delete ($url, $headers=[])
 
 post ($url, $data, $headers=[])
 
 put ($url, $data, $headers=[])
 
 invokeApi ($url, $additionalCurlOpts, $headers=[])
 

Data Fields

const EMPTY_REQUEST_BODY = 'Empty body'
 

Detailed Description

Generic cURL client wrapper for get/delete/post/put requests

Definition at line 11 of file CurlClient.php.

Member Function Documentation

◆ delete()

delete (   $url,
  $headers = [] 
)

Perform HTTP DELETE request

Parameters
string$url
array$headers
Returns
string

Definition at line 42 of file CurlClient.php.

43  {
44  $curlOpts = [];
45  $curlOpts[CURLOPT_CUSTOMREQUEST] = \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_DELETE;
46 
47  $resp = $this->invokeApi($url, $curlOpts, $headers);
48  return $resp["body"];
49  }
invokeApi($url, $additionalCurlOpts, $headers=[])
Definition: CurlClient.php:98

◆ get()

get (   $url,
  $data = [],
  $headers = [] 
)

Perform HTTP GET request

Parameters
string$urlResource URL like /V1/Resource1/123
array$data
array$headers
Returns
string

Definition at line 23 of file CurlClient.php.

24  {
25  if (!empty($data)) {
26  $url .= '?' . http_build_query($data);
27  }
28 
29  $curlOpts = [];
30  $curlOpts[CURLOPT_CUSTOMREQUEST] = \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_GET;
31  $resp = $this->invokeApi($url, $curlOpts, $headers);
32  return $resp["body"];
33  }
invokeApi($url, $additionalCurlOpts, $headers=[])
Definition: CurlClient.php:98

◆ invokeApi()

invokeApi (   $url,
  $additionalCurlOpts,
  $headers = [] 
)

Makes the REST api call using passed $curl object

Parameters
string$url
array$additionalCurlOptscURL Options
array$headers
Returns
array
Exceptions

Definition at line 98 of file CurlClient.php.

99  {
100  // initialize cURL
101  $curl = curl_init($url);
102  if ($curl === false) {
103  throw new \Exception("Error Initializing cURL for baseUrl: " . $url);
104  }
105 
106  // get cURL options
107  $curlOpts = $this->getCurlOptions($additionalCurlOpts, $headers);
108 
109  // add CURL opts
110  foreach ($curlOpts as $opt => $val) {
111  curl_setopt($curl, $opt, $val);
112  }
113 
114  $response = curl_exec($curl);
115  if ($response === false) {
116  throw new \Exception(curl_error($curl));
117  }
118 
119  $resp = [];
120  $headerSize = curl_getinfo($curl, CURLINFO_HEADER_SIZE);
121  $resp["header"] = substr($response, 0, $headerSize);
122  $resp["body"] = substr($response, $headerSize);
123 
124  $resp["meta"] = curl_getinfo($curl);
125  if ($resp["meta"] === false) {
126  throw new \Exception(curl_error($curl));
127  }
128 
129  curl_close($curl);
130 
131  $meta = $resp["meta"];
132  if ($meta && $meta['http_code'] >= 400) {
133  throw new \Exception($resp["body"], $meta['http_code']);
134  }
135 
136  return $resp;
137  }
$response
Definition: 404.php:11

◆ post()

post (   $url,
  $data,
  $headers = [] 
)

Perform HTTP POST request

Parameters
string$url
array | string$data
array$headers
Returns
string

Definition at line 59 of file CurlClient.php.

60  {
61  $curlOpts = [];
62  $curlOpts[CURLOPT_CUSTOMREQUEST] = \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_POST;
63  $headers[] = 'Content-Length: ' . strlen($data);
64  $curlOpts[CURLOPT_POSTFIELDS] = $data;
65 
66  $resp = $this->invokeApi($url, $curlOpts, $headers);
67  return $resp["body"];
68  }
invokeApi($url, $additionalCurlOpts, $headers=[])
Definition: CurlClient.php:98

◆ put()

put (   $url,
  $data,
  $headers = [] 
)

Perform HTTP PUT request

Parameters
string$url
array | string$data
array$headers
Returns
string

Definition at line 78 of file CurlClient.php.

79  {
80  $curlOpts = [];
81  $curlOpts[CURLOPT_CUSTOMREQUEST] = \Magento\Framework\Webapi\Rest\Request::HTTP_METHOD_PUT;
82  $headers[] = 'Content-Length: ' . strlen($data);
83  $curlOpts[CURLOPT_POSTFIELDS] = $data;
84 
85  $resp = $this->invokeApi($url, $curlOpts, $headers);
86  return $resp["body"];
87  }
invokeApi($url, $additionalCurlOpts, $headers=[])
Definition: CurlClient.php:98

Field Documentation

◆ EMPTY_REQUEST_BODY

const EMPTY_REQUEST_BODY = 'Empty body'

Definition at line 13 of file CurlClient.php.


The documentation for this class was generated from the following file: