2015-01-07 19 views
30

如何使用GuzzleHttp(版本5.0)發佈請求。 我嘗試做以下,並收到錯誤PHP GuzzleHttp。如何使用params發送請求?

$client = new \GuzzleHttp\Client(); 
$client->post(
    'http://www.example.com/user/create', 
    array(
     'email' => '[email protected]', 
     'name' => 'Test user', 
     'password' => 'testpassword' 
    ) 
); 

PHP致命錯誤:未捕獲的異常「InvalidArgumentException」有消息「沒有方法可以處理電子郵件設定鍵」

回答

30

試試這個

$client = new \GuzzleHttp\Client(); 
$client->post(
    'http://www.example.com/user/create', 
    array(
     'body' => array(
      'email' => '[email protected]', 
      'name' => 'Test user', 
      'password' => 'testpassword' 
     ) 
    ) 
); 
+46

這種方法現在已經過時的6.0。而不是'身體'使用'form_params'。 – jasonlfunk

+2

作爲數組傳遞「body」請求選項以發送POST請求已被棄用。請使用「form_params」請求選項發送應用程序/ x-www-form-urlencoded請求或「multipart」請求選項以發送multipart/form-data請求。 –

+0

@JeremyQuinton,所以你有什麼選擇intead ...請回復 – Madhur

69

由於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 +

+0

我怎麼能發送查詢字符串在發佈請求? –

+0

你在找什麼?如果查詢字符串是URL的一部分,則必須直接將其添加到URL中,例如** http://www.example.com/user/create?mode = dev「**。 – jedema

+0

我試圖發送帖子我想它的['body']鍵 –

14

筆記,得到以下錯誤的另一個來源可能是不正確的使用JSON的作爲數組:

Passing in the "body" request option as an array to send a POST request has been deprecated. Please use the "form_params" request option to send a application/x-www-form-urlencoded request, or a the "multipart" request option to send a multipart/form-data request.

不正確

$response = $client->post('http://example.com/api', [ 
    'body' => [ 
     'name' => 'Example name', 
    ] 
]) 

正確

$response = $client->post('http://example.com/api', [ 
    'json' => [ 
     'name' => 'Example name', 
    ] 
]) 

正確

$response = $client->post('http://example.com/api', [ 
    'headers' => ['Content-Type' => 'application/json'], 
    'body' => json_encode([ 
     'name' => 'Example name', 
    ]) 
])