Search by

denetorus / galaxy-http-client

Denetorus

Standalone cURL HTTP client with raw responses and structured GalaxyResult outcomes.

Package info

github.com/Denetorus/GalaxyHttpClient

pkg:composer/denetorus/galaxy-http-client

Statistics

Installs: 9

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-11-30 16:57 UTC

This package is auto-updated.

Last update: 2026-11-30 16:59:03 UTC


README

Русский · ADR-001

Standalone synchronous cURL HTTP client for PHP >=8.4. Namespace: Galaxy\HttpClient. MIT. Copyright (c) 2026 Mylnikov Denis.

Installation

Version 0.2.0 is available from Packagist:

composer require denetorus/galaxy-http-client:0.2.0

Runtime requirements: PHP >=8.4, ext-curl and GalaxyResult ^0.3.0 from Packagist. Result is a mandatory Composer dependency even when using only raw responses. No GalaxyApplication, GalaxyValidation, framework container or HTTP server is required by the library. Test tooling additionally needs proc_open, local sockets and ext-openssl for an ephemeral self-signed certificate. There are no development package dependencies.

Example

Save as example.php beside vendor. Run php example.php URL, supplying your test HTTP endpoint.

<?php
require __DIR__ . '/vendor/autoload.php';

use Galaxy\HttpClient\GHttpClient;

$url = $argv[1] ?? throw new InvalidArgumentException('Supply a test HTTP endpoint.');
$client = new GHttpClient(connectTimeout: 10, timeout: 60);
$response = $client->request('GET', $url, ['query' => ['page' => 1]]);
echo 'HTTP ', $response->status, PHP_EOL;

$result = $client->requestResult('POST', $url, ['json' => ['value' => 42]]);
echo 'Result ', $result->getBusinessCode(), ', console ', $result->getConsoleCode(), PHP_EOL;

Do not send secrets to an untrusted endpoint. Error response data still contains raw headers and body; select what may be exposed before returning it to an external caller.

Two result contracts

GHttpClient implements Contract\HttpClientInterface:

request(string $method, string $url, array $options = []): Response\HttpResponse
requestResult(string $method, string $url, array $options = []): Galaxy\Result\GResult

get/post/put/patch/delete($url, $options = []) are wrappers for raw request(). HttpResponse has readonly status, headers (lowercase names => lists of strings) and body (bytes). getHeader() searches without case sensitivity. Only the final header block is retained. HTTP 4xx/5xx are ordinary raw responses. Transport failures throw TransportException. requestResult() converts transport failures and non-2xx responses into GResult errors; configuration errors and JSON encoding errors still throw. Only 2xx is successful, including empty 204. Response data contains status/headers/body even on HTTP failure; transport failure data is empty. JSON is not decoded automatically, and application-level errors inside HTTP 200 belong to the caller.

Options and transport

Option Contract
query Array, RFC3986 encoding appended before the fragment; nested arrays use PHP encoding
headers Name => string/list of strings; repeated values preserved, control/CRLF injection rejected
json Any JSON-encodable value including null; default Content-Type application/json
form Array encoded as application/x-www-form-urlencoded, RFC1738
body Raw bytes including an empty string; caller supplies the content type
connectTimeout / timeout Finite positive seconds <=2147483.647, defaults 10/60, millisecond resolution
redirects Boolean, default false; opt-in follows only the same scheme/host/effective port
maxRedirects Integer 0..20, default 5

json/form/body are mutually exclusive by key presence, even for null. Explicit headers override default content types. Unknown options/invalid values throw InvalidArgumentException; JSON encoding failure throws JsonException. Methods are uppercased and restricted to nonempty ASCII letters. Only absolute HTTP(S) URLs without embedded credentials are accepted. TLS peer/hostname verification is mandatory; insecure options and raw CURLOPT overrides are not exposed. CA/proxy configuration belongs to the PHP/cURL environment.

301/302 POST and 303 non-HEAD redirects switch to GET without body/body-related headers. 307/308 preserve method/body. The total timeout covers all hops. Cross-origin and malformed redirects fail before another request. TransportException exposes ErrorKind (timeout/connection/tls/other) and curlCode; its message omits URL, headers, credentials, response body and raw cURL diagnostics.

GResult codes

Code Meaning
100 Connection/DNS failure
101 Transport timeout or HTTP 408/504
102 TLS verification/negotiation failure
103 Other transport failure
104 Redirect policy/limit failure
110 HTTP 401/403
111 HTTP 429
112 HTTP 503
113 Other HTTP 5xx
114 Other non-2xx response

Range 100–150 belongs to this component; other codes in it are unused. consoleCode defaults to 1 for errors. Context contains category (timeout/unavailable/upstream_response) and curlCode or httpStatus. The client does not choose an application's outgoing HTTP status. HttpResult::createFromResponse() / HttpResult::createFromTransportException() expose the same mapper.

Boundaries and checks

Responses are buffered; there is no size limit, streaming, retry, cache, cookie jar or asynchronous execution. Destination access policy, credentials, business API interpretation and public response filtering belong to the host.

composer install --no-plugins --no-scripts
composer test
php tests/create-consumer.php CONSUMER_ROOT PACKAGE_ROOT
php tests/consumer.php CONSUMER_ROOT
php tests/run.php CONSUMER_ROOT/vendor/autoload.php --installed

The generated consumer is a local path mirror for an unpublished package; install its dependencies with Composer before the last two commands. GalaxyResult comes from Packagist. Installed mode disables source autoload overrides. The suite uses real loopback HTTP and rejects an untrusted self-signed TLS endpoint, without public API calls. Synthetic servers, certificate files and server logs are stopped/removed by the tests. CI PHP 8.4 repeats source and independent-consumer checks, Composer validation, platform requirements and strict optimized PSR-4.

License: MIT.