/
robokassa
/
sdk-php
Обзор
Документация
Войти
/
robokassa
/
sdk-php
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
main
src/Client/HttpClient.php
59 строк
2 KB
dmtew
fix: rename Src to src for PSR-4 compliance
29 сен 2025, 19:14
29 сен 2025, 19:14
fe0f8d4
Код
Авторство
О чём код?
<?php namespace Robokassa\Client; use GuzzleHttp\Client; use GuzzleHttp\Exception\RequestException; final class Response { /** @var string */ public $body; /** @var int */ public $status; public function __construct(string $body, int $status) { $this->body = $body; $this->status = $status; } } interface HttpClientInterface { public function get(string $url, array $headers = []): Response; public function post(string $url, string $body, array $headers = []): Response; } final class HttpClient implements HttpClientInterface { /** @var Client */ private $client; public function __construct(?Client $client = null) { $this->client = $client ?? new Client(['timeout' => 15]); } public function get(string $url, array $headers = []): Response { try { $r = $this->client->get($url, ['headers' => $headers]); return new Response((string)$r->getBody(), $r->getStatusCode()); } catch (RequestException $e) { $resp = $e->getResponse(); $msg = $resp ? (string)$resp->getBody() : $e->getMessage(); throw new \Exception('Ошибка HTTP GET: ' . $msg, 0, $e); } } public function post(string $url, string $body, array $headers = []): Response { try { $r = $this->client->post($url, [ 'body' => $body, 'headers' => $headers, ]); return new Response((string)$r->getBody(), $r->getStatusCode()); } catch (RequestException $e) { $resp = $e->getResponse(); $msg = $resp ? (string)$resp->getBody() : $e->getMessage(); throw new \Exception('Ошибка HTTP POST: ' . $msg, 0, $e); } } }