/
SerjPopov
/
cloud-mail-ru-php
Обзор
Документация
Войти
/
SerjPopov
/
cloud-mail-ru-php
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
src/HttpClient.php
98 строк
3 KB
biodata
Исправлено получение токена. Обновлена библиотека Guzzle. Обновлен docker контейнер. Протестировано на PHP 8.2.
03 фев 2024, 20:16
03 фев 2024, 20:16
605acc0
Код
Авторство
О чём код?
<?php namespace SergPopov\CloudMailRu; use GuzzleHttp\{Client, Cookie\CookieJar, Exception\GuzzleException}; use Psr\Http\Message\ResponseInterface; /** * Class HttpClient * @package SergPopov\CloudMailRu */ class HttpClient { public Client $client; private CookieJar $cookie; public function __construct() { $this->client = new Client(); $this->cookie = new CookieJar(); } /** * @param string $url * @param array $postParams * @param array $headers * @return ResponseInterface * @throws CloudMailRuException */ public function requestPost(string $url, array $postParams, array $headers = []): ResponseInterface { try { return $this->client->request('POST', $url, [ 'form_params' => $postParams, 'cookies' => $this->cookie, 'verify' => false, 'headers' => $headers, ]); } catch (GuzzleException $e) { throw new CloudMailRuException($e->getMessage()); } } /** * @param string $url * @param mixed $body * @param array $headers * @return ResponseInterface * @throws CloudMailRuException */ public function requestPut(string $url, $body, array $headers = []): ResponseInterface { try { return $this->client->request('PUT', $url, [ 'body' => $body, 'cookies' => $this->cookie, 'verify' => false, 'headers' => $headers, ]); } catch (GuzzleException $e) { throw new CloudMailRuException($e->getMessage()); } } /** * @param string $url * @param array $headers * @return ResponseInterface * @throws CloudMailRuException */ public function requestGet(string $url, array $headers = []): ResponseInterface { try { return $this->client->request('GET', $url, [ 'cookies' => $this->cookie, 'verify' => false, 'headers' => $headers, ]); } catch (GuzzleException $e) { throw new CloudMailRuException($e->getMessage()); } } /** * @param ResponseInterface $response * @return string * @throws CloudMailRuException */ public function checkResponse(ResponseInterface $response): string { if ($response->getStatusCode() == 200) { return $response->getBody()->getContents(); } else { throw new CloudMailRuException('Bad response ' . $response->getStatusCode()); } } }