由於Marco的回答已棄用,您必須使用以下語法(根據jasonlfunk的評論):
$client = new \GuzzleHttp\Client();
$response = $client->request('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => '[email protected]',
'name' => 'Test user',
'password' => 'testpassword',
]
]);
請求與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'
]
]
]);
REST動詞使用參數
// PUT
$client->put('http://www.example.com/user/4', [
'body' => [
'email' => '[email protected]',
'name' => 'Test user',
'password' => 'testpassword',
],
'timeout' => 5
]);
// DELETE
$client->delete('http://www.example.com/user');
異步POST數據
有用的長期服務器操作使用。
$client = new \GuzzleHttp\Client();
$promise = $client->requestAsync('POST', 'http://www.example.com/user/create', [
'form_params' => [
'email' => '[email protected]',
'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();
}
);
Documentation更多地闡述了新的可能性。在狂飲V6.0 +
這種方法現在已經過時的6.0。而不是'身體'使用'form_params'。 – jasonlfunk
作爲數組傳遞「body」請求選項以發送POST請求已被棄用。請使用「form_params」請求選項發送應用程序/ x-www-form-urlencoded請求或「multipart」請求選項以發送multipart/form-data請求。 –
@JeremyQuinton,所以你有什麼選擇intead ...請回復 – Madhur