HTTP PHP

[!NOTE]
This script is still under development.
composer require adampatterson/http
Usage
Basic Usage
use Http\Http;
use GuzzleHttp\Cookie\CookieJar;
$response = Http::get('https://example.com/api/users');
$response = Http::asJson()->post('https://example.com/api/users', ['name' => 'John']);
$response = Http::asFormParams()->post('https://example.com/api/users', ['name' => 'John']);
$response = Http::withHeaders(['X-Custom' => 'value'])->get('https://example.com/api/users');
$response = Http::withToken('your-token')->get('https://example.com/api/users');
$response = Http::withCookies(['session_id' => 'abc123'], 'example.com')
->get('https://example.com/api/users');
$jar = CookieJar::fromArray(['session_id' => 'abc123'], 'example.com');
$response = Http::withCookieJar($jar)->get('https://example.com/api/users');
Response Helpers
The HttpResponse object provides several helpers to inspect the response:
use Http\Http;
$response = Http::get('https://example.com/api/users');
$response->status();
$response->body();
$response->array();
$response->object();
$response->collect();
$response->header('Content-Type');
$response->headers();
$response->isSuccess();
$response->isOk();
$response->isRedirect();
$response->isClientError();
$response->isServerError();
Method Proxying
If you need to access a method on the underlying Guzzle response that is not explicitly defined in HttpResponse, it will be automatically proxied:
$version = $response->getProtocolVersion();
Request/Response Flow
At a high level, the package separates request building from response consumption:
- The
Http facade forwards static calls to MakeHttpRequest.
MakeHttpRequest collects fluent configuration (asJson, withHeaders, withToken, withCookies, etc.) and verb payload options.
send() merges options, parses query params from the URL, executes the Guzzle request, and captures transfer stats.
- The raw PSR-7 response is wrapped in
HttpResponse.
HttpResponse exposes helper methods (body, json, status, etc.) and proxies unknown methods to Guzzle.
This flow keeps transport concerns in MakeHttpRequest and read helpers in HttpResponse.
Tests
composer install
composer test
Code Coverage
To run tests with code coverage, ensure you have Xdebug installed and run:
composer test-coverage
Local Dev
Without needing to modify the composer.json file. Run from the theme root, this will symlink the package into the theme's vendor directory.
ln -s ~/Sites/packages/http/ ./vendor/adampatterson/http
Otherwise, you can add the local package to your composer.json file.
{
"repositories": [
{
"type": "path",
"url": "/Sites/packages/http"
}
]
}