Magento 2 Documentation  2.3
Documentation for Magento 2 CMS v2.3 (December 2018)
CurlClient.php
Go to the documentation of this file.
1 <?php
7 
12 {
13  const EMPTY_REQUEST_BODY = 'Empty body';
14 
23  public function get($url, $data = [], $headers = [])
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  }
34 
42  public function delete($url, $headers = [])
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  }
50 
59  public function post($url, $data, $headers = [])
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  }
69 
78  public function put($url, $data, $headers = [])
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  }
88 
98  public function invokeApi($url, $additionalCurlOpts, $headers = [])
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  }
138 
146  private function getCurlOptions($customCurlOpts = [], $headers = [])
147  {
148  // default curl options
149  $curlOpts = [
150  CURLOPT_RETURNTRANSFER => true, // return result instead of echoing
151  CURLOPT_SSL_VERIFYPEER => false, // stop cURL from verifying the peer's certificate
152  CURLOPT_FOLLOWLOCATION => false, // follow redirects, Location: headers
153  CURLOPT_MAXREDIRS => 10, // but don't redirect more than 10 times
154  CURLOPT_HTTPHEADER => [],
155  CURLOPT_HEADER => 1,
156  ];
157 
158  // merge headers
159  $headers = array_merge($curlOpts[CURLOPT_HTTPHEADER], $headers);
160  if (TESTS_XDEBUG_ENABLED) {
161  $headers[] = 'Cookie: XDEBUG_SESSION=' . TESTS_XDEBUG_SESSION;
162  }
163  $curlOpts[CURLOPT_HTTPHEADER] = $headers;
164 
165  // merge custom Curl Options & return
166  foreach ($customCurlOpts as $opt => $val) {
167  $curlOpts[$opt] = $val;
168  }
169 
170  return $curlOpts;
171  }
172 }
$response
Definition: 404.php:11
invokeApi($url, $additionalCurlOpts, $headers=[])
Definition: CurlClient.php:98