PHP Guzzle POST 请求返回空值

fro*_*fat 3 php post guzzle

我正在为此绞尽脑汁 - 请求在 Postman 中运行良好,但我无法使用 Guzzle 访问正文。

$client = new \GuzzleHttp\Client();

        $res = $client->request('POST',  'https://apitest.authorize.net/xml/v1/request.api', [
            'headers' => [
                'Content-Type' => 'application/json',
            ],
            'body' => json_encode([
                'createTransactionRequest' => [
                    'merchantAuthentication' => [
                        'name' => '88VuW****',
                        'transactionKey' => '2xyW8q65VZ*****'
                    ],
                    'transactionRequest' => [
                        'transactionType' => 'authCaptureTransaction',
                        'amount' => "5.00",
                        'payment' => [
                            'opaqueData' => [
                                'dataDescriptor' => 'COMMON.ACCEPT.INAPP.PAYMENT',
                                'dataValue' => $_POST['dataValue']
                            ]
                        ]
                    ]
                ]
            ])
        ]);

        dd(json_decode($res->getBody()));
Run Code Online (Sandbox Code Playgroud)

以上返回null - 没有错误,只是null。下面是 Postman 中的相同请求,使用 body 成功。

在此处输入图片说明

任何想法将不胜感激,谢谢!

Ale*_*kov 5

你应该使用$res->getBody()->getContents().

并在出现任何错误时json_decode()返回null。您必须使用json_last_error().