camoo/curl-http-client
最新稳定版本:1.1.4
Composer 安装命令:
composer require camoo/curl-http-client
包简介
Simple Curl Http Client
README 文档
README
Simple Curl Http Client build with the clean code architecture approach. PSR-7 standard
Usage
without dependency injection
use Camoo\Http\Curl\Infrastructure\Client; use Camoo\Http\Curl\Domain\Entity\Configuration; $configuration = Configuration::create(); // activate debug // $configuration->setDebug(true); $client = new Client($configuration); $uri = 'https://api.example.com/v1/users'; $response = $client->get($uri); $status = $response->getStatusCode(); $body = (string)$response->getBody(); // get all headers $headers = $response->getHeaders(); // get single header $header = $response->getHeader('foo'); // get status code $code = $response->getStatusCode();
With dependency injection
## in Module use Camoo\Http\Curl\Domain\Client\ClientInterface; use Camoo\Http\Curl\Infrastructure\Client; $this->bind(ClientInterface::class)->to(Client::class); ## in Adapter port final class TemplateRepository implements TemplateRepositoryInterface { private const URI = 'https://api.example.com/v2/template'; private const SUCCESS_STATUS = 201; public function __construct(private readonly ClientInterface $client) { } public function save(Template $template): bool { $response = $this->client->post(self::URI, $template->toArray()); return $response->getStatusCode() === self::SUCCESS_STATUS; } public function getById(string $id): Template { $uri = self::URI. '?id=' . $id; $response = $this->client->get($uri); if ($response->getStatusCode() !== self::SUCCESS_STATUS){ throw new NotFoundTemplate(sprintf('Template with id %s not found!', $id)); } $body = (string)$response->getBody(); return Template::fromArray(json_decode($body, true)); } # ... }
统计信息
- 总下载量: 5.33k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 0
- 点击次数: 0
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2022-12-25