【问题标题】:Proper syntax for Guzzlehttp request containing body and header包含正文和标头的 Guzzlehttp 请求的正确语法
【发布时间】:2017-12-02 01:42:39
【问题描述】:

我的控制器上有以下 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();

上述语法只发送标头,但是,我应该使用什么正确的语法来发送标头和正文。

【问题讨论】:

  • 您是否查看过该 request 方法的 guzzle 文档?

标签: php laravel http http-post guzzle


【解决方案1】:

Guzzle 文档有大量示例,请尝试阅读。

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

【讨论】:

    【解决方案2】:

    您在第 7 行 ]], 中过早关闭数组

    【讨论】:

      猜你喜欢
      • 2019-03-24
      • 1970-01-01
      • 1970-01-01
      • 2021-07-18
      • 1970-01-01
      • 1970-01-01
      • 2010-11-15
      • 2020-12-23
      • 1970-01-01
      相关资源
      最近更新 更多