toolkit

Http

Outbound HTTP requests returning an HttpResponse.

A request starts at once and its response is read lazily, so requests started before the first read run together:

$pending = Arr::map($urls, fn($url) => Http::get($url));
$bodies = Arr::map($pending, fn($response) => $response->body);
Http:: Phlat\Toolkit\Http 7 methods

Requests 7

  • get Sends a GET request.
  • post Sends a POST request; an array $body is sent as JSON.
  • put Sends a PUT request; an array $body is sent as JSON.
  • patch Sends a PATCH request; an array $body is sent as JSON.
  • delete Sends a DELETE request.
  • purge Sends a PURGE request, as a cache server expects.
  • request Starts a request and returns before it finishes; reading the response waits for it, and a failure (HttpError) comes out of that read.

Requests

get

Http::get(string $url, array $headers = [], int $timeout = 30): HttpResponse

Sends a GET request.

Parameters (3)
$url string
$headers array = []
$timeout int = 30

Returns HttpResponse

phlat/src/Toolkit/Http.php:28
Http::post(string $url, mixed $body = null, array $headers = [], int $timeout = 30): HttpResponse

Sends a POST request; an array $body is sent as JSON.

Parameters (4)
$url string
$body mixed = null
$headers array = []
$timeout int = 30

Returns HttpResponse

phlat/src/Toolkit/Http.php:34

put

Http::put(string $url, mixed $body = null, array $headers = [], int $timeout = 30): HttpResponse

Sends a PUT request; an array $body is sent as JSON.

Parameters (4)
$url string
$body mixed = null
$headers array = []
$timeout int = 30

Returns HttpResponse

phlat/src/Toolkit/Http.php:40
Http::patch(string $url, mixed $body = null, array $headers = [], int $timeout = 30): HttpResponse

Sends a PATCH request; an array $body is sent as JSON.

Parameters (4)
$url string
$body mixed = null
$headers array = []
$timeout int = 30

Returns HttpResponse

phlat/src/Toolkit/Http.php:46
Http::delete(string $url, array $headers = [], int $timeout = 30): HttpResponse

Sends a DELETE request.

Parameters (3)
$url string
$headers array = []
$timeout int = 30

Returns HttpResponse

phlat/src/Toolkit/Http.php:52
Http::purge(string $url, array $headers = [], int $timeout = 30): HttpResponse

Sends a PURGE request, as a cache server expects.

Parameters (3)
$url string
$headers array = []
$timeout int = 30

Returns HttpResponse

phlat/src/Toolkit/Http.php:58
Http::request(string $method, string $url, mixed $body = null, array $headers = [], int $timeout = 30, int $redirects = 10, ?Closure $progress = null): HttpResponse

Starts a request and returns before it finishes; reading the response waits for it, and a failure (HttpError) comes out of that read.

Only http and https are spoken, redirects are followed up to $redirects times (0 hands back the 3xx), and $timeout caps the whole request. An array $body is sent as JSON; send a string to control the encoding yourself, e.g. Url::query($fields) for a form.

$progress is called as the body arrives with (bytes downloaded, bytes expected, the curl_getinfo() array - primary_ip is there once connected). Throw from it to abort: the transfer stops and that exception comes out of the read.

Parameters (7)
$method string
$url string
$body mixed = null
$headers array = []
$timeout int = 30
$redirects int = 10
$progress ?Closure = null

Returns HttpResponse

phlat/src/Toolkit/Http.php:74