Skip to main content
Version: Fegg 5.x

Client::post

Send a POST request

Description​

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

An array $body is sent as a form (application/x-www-form-urlencoded, the Content-Type is added). A string is sent as it is: add its Content-Type yourself, for JSON for example.

Returns array('status' => int, 'body' => string). A transport failure throws Fegg\Http\ClientException (see Errors).

Arguments​

NameData typeDefaultRemarks
$urlstringhttp:// or https:// URL
$bodyarray|stringForm fields, or the body as it is
$headersarrayarray()Request headers, each as 'Name: value'

Example​

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')
);