How to use PSR HTTP standards to upgrade your code

This page summarizes the projects mentioned and recommended in the original post on dev.to

Our great sponsors
  • WorkOS - The modern identity platform for B2B SaaS
  • InfluxDB - Power Real-Time Data Analytics at Scale
  • SaaSHub - Software Alternatives and Reviews
  • catfacts-example

    A short example on how to use PSR HTTP standards for writing PHP Packages

  • If you want to check out this code in its entirely you can head to this repo. It's in a useable state, but the API does not contain a lot of facts.

  • http-message

    The purpose of this PSR is to provide a set of common interfaces for HTTP messages as described in RFC 7230 and RFC 7231

  • PSR-7, the standard for requests and responses

  • WorkOS

    The modern identity platform for B2B SaaS. The APIs are flexible and easy-to-use, supporting authentication, user identity, and complex enterprise features like SSO and SCIM provisioning.

    WorkOS logo
  • cat-facts

    Daily cat facts! 🐱

  • declare(strict_types=1); namespace Rocksheep\CatFacts; use Exception; use Http\Discovery\Psr17FactoryDiscovery; use Http\Discovery\Psr18ClientDiscovery; use JsonException; use Psr\Http\Client\ClientExceptionInterface; use Psr\Http\Client\ClientInterface; use Psr\Http\Message\RequestFactoryInterface; class HttpClient { private ClientInterface $client; private RequestFactoryInterface $requestFactory; protected string $baseUrl = 'https://cat-fact.herokuapp.com'; public function __construct( ?ClientInterface $client = null, ?RequestFactoryInterface $requestFactory = null ) { $this->client = $client ?: Psr18ClientDiscovery::find(); $this->requestFactory = $requestFactory ?: Psr17FactoryDiscovery::findRequestFactory(); } /** * @throws JsonException * @throws Exception */ public function sendRequest(string $method, string $uri): array { $request = $this->requestFactory->createRequest($method, sprintf('%s/%s', $this->baseUrl, ltrim($uri, '/'))); try { $response = $this->client->sendRequest($request); } catch (ClientExceptionInterface $e) { throw new Exception('Oh well'); } if ($response->getStatusCode() >= 400) { throw new Exception('Too bad'); } $responseBody = (string) $response->getBody(); return json_decode($responseBody, false, 512, JSON_THROW_ON_ERROR); } }

NOTE: The number of mentions on this list indicates mentions on common posts plus user suggested alternatives. Hence, a higher number means a more popular project.

Suggest a related project

Related posts