如何使用Guzzle PHP获取SENT数据的主体?

ruh*_*net 15 php http guzzle

我在PHP中使用Guzzle(v6.1.1)向服务器发出POST请求.它工作正常.我正在添加一些日志记录功能来记录发送和接收的内容,我无法弄清楚如何获取Guzzle发送到服务器的数据.我可以很好地得到响应,但是如何获取发送的数据?(这将是JSON字符串.)

这是我的代码的相关部分:

$client = new GuzzleHttp\Client(['base_uri' => $serviceUrlPayments ]);
    try {
       $response = $client->request('POST', 'Charge', [
            'auth' => [$securenetId, $secureKey],
            'json' => [     "amount" => $amount,
                            "paymentVaultToken" => array(
                                    "customerId" => $customerId,
                                    "paymentMethodId" => $token,
                                    "publicKey" => $publicKey
                                    ),
                            "extendedInformation" => array(
                                    "typeOfGoods" => $typeOfGoods,
                                    "userDefinedFields" => $udfs,
                                    "notes" => $Notes
                                    ),
                            'developerApplication'=> $developerApplication 
            ]
    ]);

    } catch (ServerErrorResponseException $e) {
        echo (string) $e->getResponse()->getBody();
    }


    echo $response->getBody(); // THIS CORRECTLY SHOWS THE SERVER RESPONSE
    echo $client->getBody();           // This doesn't work
    echo $client->request->getBody();  // nor does this
Run Code Online (Sandbox Code Playgroud)

任何帮助,将不胜感激.我确实试图在Guzzle源代码中查找类似于getBody()的函数,该函数可以处理请求,但我不是PHP专家,所以我没有提出任何有用的信息.我也经常搜索Google,但发现只有人在讨论从服务器上获取响应,我没有遇到任何问题.

Fed*_*kun 16

您可以通过创建中间件来完成这项工作.

use GuzzleHttp\Client;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Middleware;
use Psr\Http\Message\RequestInterface;

$stack = HandlerStack::create();
// my middleware
$stack->push(Middleware::mapRequest(function (RequestInterface $request) {
    $contentsRequest = (string) $request->getBody();
    //var_dump($contentsRequest);

    return $request;
}));

$client = new Client([
    'base_uri' => 'http://www.example.com/api/',
    'handler' => $stack
]);

$response = $client->request('POST', 'itemupdate', [
    'auth' => [$username, $password],
    'json' => [
        "key" => "value",
        "key2" => "value",
    ]
]);
Run Code Online (Sandbox Code Playgroud)

然而,这在接收响应之前被触发.你可能想做这样的事情:

$stack->push(function (callable $handler) {
    return function (RequestInterface $request, array $options) use ($handler) {
        return $handler($request, $options)->then(
            function ($response) use ($request) {
                // work here
                $contentsRequest = (string) $request->getBody();
                //var_dump($contentsRequest);
                return $response;
            }
        );
    };
});
Run Code Online (Sandbox Code Playgroud)

  • 谢谢.为了这个方式过于复杂,应该是一个简单的任务 (3认同)

DJ *_*ipe 9

使用Guzzle 6.2.

在过去的几天里,我一直在努力解决这个问题,同时尝试构建一种审计与不同API的HTTP交互的方法.在我的情况下,解决方案是简单地回滚请求正文.

请求的主体实际上是作为实现的.因此,当发送请求时,Guzzle从流中读取.读取完整的流会将流的内部指针移动到结尾.因此,getContents()在请求完成后调用时,内部指针已经在流的末尾并且不返回任何内容.

解决方案?将指针倒回到开头并再次读取流.

<?php
// ...
$body = $request->getBody();
echo $body->getContents(); // -->nothing

// Rewind the stream
$body->rewind();
echo $body->getContents(); // -->The request body :)
Run Code Online (Sandbox Code Playgroud)