包含正文和标题的Guzzlehttp请求的正确语法

时间:2017-12-02 01:42:39

标签: php laravel http http-post guzzle

我的控制器上有以下Guzzle发送请求: -

$client = new GuzzleHttp\Client(['base_uri' => 'https://domainname/api/v1/']);

$response = $client->request('POST', 'user/register', [
   'headers' => [
       'Authorization' => Session::get('token'),
       'Content-Type'  => 'application/x-www-form-urlencoded'
    ]],
    [ 'form_params' => [
        'user_id' => $user->id,
        'start_date' => $start_date,
        'expiry_date' => $expiry_date
    ],
 ]);

 $response= $response->getBody();

上面的语法只发送标题但是,我应该使用什么样的语法来发送标题和正文。

2 个答案:

答案 0 :(得分:2)

Guzzle docs有很多例子,请尝试阅读它们。

$response = $client->request('POST', $url, [ 
    'headers' => [
         'User-Agent' => 'testing/1.0',
         'Accept' => 'application/json', 
         'X-Foo' => ['Bar', 'Baz']
    ],
    'form_params' => [
         'field_name' => 'abc', 
         'other_field' => '123', 
         'nested_field' => [
             'nested' => 'hello' 
         ] 
    ] 
]);

答案 1 :(得分:1)

您在第7行]],

中过早关闭阵列
相关问题