【问题标题】:how to recive the response of a post method send with guzzle如何接收使用 guzzle 发送的 post 方法的响应
【发布时间】:2019-10-12 23:45:27
【问题描述】:

我正在调用一个带有 guzzle 的 api 并发送一些数据,该 api 向我返回了一些我想要接收并显示它们的数据,所以这是我的代码:

$requestapi = $client->post('http:url/api/v1/transaction/Verify', [
        'headers' => ['Content-Type' => 'application/json'],
        'body' => '{
        "tn":"1905463527",
        }'
    ]);

现在,当我 dd $requestapi 时,它现在向我显示验证 api 返回的结果,并仅显示 200 响应。

【问题讨论】:

    标签: php laravel api guzzle


    【解决方案1】:

    GuzzleHttp\Client::post 方法返回GuzzleHttp\Psr7\Response 对象。 您可以简单地使用getBody 方法访问响应正文。

    $response = $client->post(...);
    $contents = $response->getBody()->getContents();
    

    【讨论】:

    • 是的,谢谢你的正确答案,但我怎样才能访问 getbody 的项目??
    • 例如我可以使用 contents->name 吗?如果我们考虑服务器为我发送名称??
    • @Farshad 它取决于响应格式。对于 application/json 内容类型,您可以从响应字符串 $array = json_decode($contents, true) 创建一个数组,并通过数组访问 $array['name'] 访问 name 属性
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2016-04-16
    • 2014-07-13
    • 2011-03-03
    • 1970-01-01
    • 2018-07-31
    • 2022-02-21
    • 1970-01-01
    相关资源
    最近更新 更多