Web Request
A PSR-18 HTTP client on cURL, with its own PSR-7 messages, parallel requests and a mock client for tests, in one small package.
Why
Calling an API should not need a large dependency tree, and the code that does it should be easy to test. Most clients give you one of the two.
WebRequest implements the standards (PSR-7 messages, PSR-17 factories, a PSR-18 client), so the code that uses it depends on the interfaces, not on this package:
$responses = HttpPromise::all(
users: $httpClient->sendAsync(Request::getInstance(Uri::getInstance('https://api.example.com/users'))),
orders: $httpClient->sendAsync(Request::getInstance(Uri::getInstance('https://api.example.com/orders'))),
)->await();
- Parallel requests without an event loop.
sendAsync()returns a promise, and every pending request runs together on cURL multi when youawait(). - Test without the network.
MockClientreturns the response you give it and keeps the request it received. - Helpers for the usual bodies: JSON, form URL-encoded and multipart.
Overview
PHP WebRequest is a fully PSR-compliant HTTP client package that provides:
PSR Implementations
- ✅ PSR-7 - HTTP Message Interface (Request, Response, Stream)
- ✅ PSR-17 - HTTP Factories (Request, Response, Stream, etc.)
- ✅ PSR-18 - HTTP Client Interface
Additional Features
- Helper classes for common request types (JSON, Form URL Encoded, MultiPart)
- Support for parallel HTTP requests, with promises (
sendAsync()) or callbacks - Mock client for testing
Documentation
- HTTP Messages and Factories (PSR-7/PSR-17) - Create and manipulate HTTP requests, responses, and streams
- Sending HTTP Requests (PSR-18) - Send HTTP requests with customizable options (proxy, SSL, timeouts)
- Parallel Requests - Execute multiple HTTP requests concurrently, with promises (
sendAsync()) or callbacks - Testing with Mock Client - Test HTTP code without real network calls
- Request Helpers - Quickly build JSON, Form, and MultiPart requests
- Comparison with Guzzle - How WebRequest compares to Guzzle HTTP client
Basic Example
<?php
// Create a request
$uri = \ByJG\Util\Uri::getInstance('http://www.example.com/page');
$request = \ByJG\WebRequest\Psr7\Request::getInstance($uri);
// Send the request
$response = \ByJG\WebRequest\HttpClient::getInstance()->sendRequest($request);
// Process the response
$statusCode = $response->getStatusCode();
$body = $response->getBody()->getContents();
Installation
composer require "byjg/webrequest"
Running Tests
# Start the test server
docker-compose up -d
# Run the tests
vendor/bin/phpunit
# Stop the server
docker-compose down
The PSR-7 and PSR-17 conformance suites, the ones other implementations run (php-http/psr7-integration-tests and http-interop/http-factory-tests), run with the package tests. They need no test server, so they can also run alone:
vendor/bin/phpunit --testsuite Conformance