メインコンテンツまでスキップ
Version: Fegg 5.x

Client::post

POSTリクエストを送ります

説明​

public array Client::post( string $url, array|string $body [, array $headers = array() ] )

$bodyが配列なら、フォーム(application/x-www-form-urlencoded)として送り、Content-Typeも付けます。文字列ならそのまま送るので、JSONなどではContent-Typeを自分で付けてください。

array('status' => int, 'body' => string)を返します。通信に失敗したときはFegg\Http\ClientExceptionを投げます(エラーを参照)。

引数​

名前データ型デフォルト備考
$urlstringhttp://またはhttps://のURL
$bodyarray|stringフォームの項目、またはそのまま送る本文
$headersarrayarray()リクエストヘッダー。1つずつ'Name: value'の形で書きます

例​

Controller
use Fegg\Http\Client;

$client = new Client();

// A form
$response = $client->post('https://example.com/api/entries', array('name' => $name, 'mail' => $mail));

// JSON
$response = $client->post(
'https://hooks.example.com/notify',
json_encode(array('text' => 'New entry from ' . $name)),
array('Content-Type: application/json')
);