Client::request
任意のメソッドでリクエストを送ります
説明
public array Client::request( string $method, string $url [, ?string $body = null [, array $headers = array() ] ] )
GETとPOST以外のメソッド(PUT、PATCH、DELETE、HEADなど)で使います。本文はそのまま送ります。
array('status' => int, 'body' => string)を返します。通信に失敗したときはFegg\Http\ClientExceptionを、httpでもhttpsでもないURLには\InvalidArgumentExceptionを投げます(エラーを参照)。
引数
| 名前 | データ型 | デフォルト | 備考 |
|---|---|---|---|
| $method | string | HTTPメソッド | |
| $url | string | http://またはhttps://のURL | |
| $body | string|null | null | 本文。そのまま送ります |
| $headers | array | array() | リクエストヘッダー。1つずつ'Name: value'の形で書きます |
例
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) {
// ...
}