【问题标题】:Guzzle 6 Post upgradeGuzzle 6 后期升级
【发布时间】:2020-05-08 19:02:40
【问题描述】:

我最近将我的 guzzle 版本从 3 更新到 6。以下调用在 3 上运行,但现在我需要将其升级到 6(因为它不工作)。阅读文档后,我有点困惑这个新的帖子请求在 Guzzle 6 中是如何工作的。这是我在 Guzzle 3 中的旧帖子请求

 try
        {
            $request = $this->guzzleClient->post(
                '/login?token='.$this->container->getParameter("token"),
                array(),
                json_encode($data)
            );
            $request->setHeader('Content-Type', 'application/json');
            $response = $request->send();

            return $response->json();
        }

我如何翻译它以发布请求?

【问题讨论】:

  • 看起来这就是你要找的东西:stackoverflow.com/questions/30860235/…
  • @OluwatobiSamuelOmisakin - 如果该问题的答案也适用于此处,请将其标记为重复项。
  • 这能回答你的问题吗? guzzle ver 6 post method is not woking
  • @MagnusEriksson 不是真的。
  • 请提供比“不是真的”更多的反馈。如果您将代码重写为接受答案中的代码,会发生什么?错误信息?请求无效?

标签: php laravel symfony guzzle guzzle6


【解决方案1】:

你需要这个:

$response = $this->guzzle6->post(
    '/login?token='.$this->container->getParameter("token"),
    [
        'json' => $data
    ]
);

return json_decode($response->getBody()->getContents());

Guzzle 6 没有->json() 回复,所以你必须自己解码。

【讨论】:

    猜你喜欢
    • 2021-08-26
    • 2018-11-21
    • 1970-01-01
    • 1970-01-01
    • 2022-01-21
    • 2018-11-19
    • 1970-01-01
    • 2013-02-25
    • 1970-01-01
    相关资源
    最近更新 更多