Skip to main content
Version: Fegg 5.x

Client::request

Send a request with any method

Description​

public array Client::request( string $method, string $url [, ?string $body = null [, array $headers = array() ] ] )

For methods other than GET and POST (PUT, PATCH, DELETE, HEAD ...). The body is sent as it is.

Returns array('status' => int, 'body' => string). A transport failure throws Fegg\Http\ClientException; a URL that is not http or https throws \InvalidArgumentException (see Errors).

Arguments​

NameData typeDefaultRemarks
$methodstringHTTP method
$urlstringhttp:// or https:// URL
$bodystring|nullnullBody, sent as it is
$headersarrayarray()Request headers, each as 'Name: value'

Example​

Controller
use Fegg\Http\Client;

$client = new Client();

$response = $client->request('PUT', 'https://api.example.com/items/42', json_encode($item), array(
'Content-Type: application/json',
));

$response = $client->request('DELETE', 'https://api.example.com/items/42');
if ($response['status'] !== 204) {
// ...
}