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
| Name | Data type | Default | Remarks |
|---|---|---|---|
| $method | string | HTTP method | |
| $url | string | http:// or https:// URL | |
| $body | string|null | null | Body, sent as it is |
| $headers | array | array() | 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) {
// ...
}