config = $config; $this->serializer = $serializer; $this->lastRequests = $lastRequests; } /** * Send POST request to url. * * @param string $url * @param array $data * @return array|bool|float|int|string|null * @throws \Magento\Framework\Exception\LocalizedException */ public function connect(string $url, array $data = []) { $data['v'] = 1; $query = http_build_query($data); $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_POST, count($data)); curl_setopt($ch, CURLOPT_POSTFIELDS, $query); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); $res = curl_exec($ch); if (curl_errno($ch)) { $errorMessage = 'cURL Error: ' . curl_error($ch); $this->lastRequests->add($url, $data, ['error' => $errorMessage]); if ($this->config->isDebugMode()) { throw new \Exception($errorMessage, 1); } } curl_close($ch); if (false === $res) { $this->lastRequests->add($url, $data, ['error' => "Cannot connect to $url"]); throw new LocalizedException(__('Cannot connect to %1', $url)); } $res = $this->serializer->unserialize($res); $this->lastRequests->add($url, $data, $res); return $res; } }