Karena jawaban Marco tidak digunakan lagi , Anda harus menggunakan sintaks berikut (sesuai komentar jasonlfunk):
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
]
]);
Minta dengan file POST
$response = $client->request('POST', 'http://www.example.com/files/post', [
'multipart' => [
[
'name' => 'file_name',
'contents' => fopen('/path/to/file', 'r')
],
[
'name' => 'csv_header',
'contents' => 'First Name, Last Name, Username',
'filename' => 'csv_header.csv'
]
]
]);
Penggunaan kata kerja REST dengan params
// PUT
$client->put('http://www.example.com/user/4', [
'body' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
'timeout' => 5
]);
// DELETE
$client->delete('http://www.example.com/user');
Data POST asinkron
Berguna untuk operasi server yang lama.
$client = new \GuzzleHttp\Client();
$promise = $client->requestAsync('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
]
]);
$promise->then(
function (ResponseInterface $res) {
echo $res->getStatusCode() . "\n";
},
function (RequestException $e) {
echo $e->getMessage() . "\n";
echo $e->getRequest()->getMethod();
}
);
Setel header
Menurut dokumentasi , Anda dapat mengatur header:
// Set various headers on a request
$client->request('GET', '/get', [
'headers' => [
'User-Agent' => 'testing/1.0',
'Accept' => 'application/json',
'X-Foo' => ['Bar', 'Baz']
]
]);
Informasi selengkapnya untuk debugging
Jika Anda menginginkan informasi lebih detail, Anda dapat menggunakan debug
opsi seperti ini:
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => 'test@gmail.com',
'name' => 'Test user',
'password' => 'testpassword',
],
// If you want more informations during request
'debug' => true
]);
Dokumentasi lebih menjelaskan tentang kemungkinan-kemungkinan baru.