PHP Guzzle POST 请求返回 Null

标签 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()));

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

enter image description here

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

最佳答案

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

并且 json_decode() 在出现任何错误时返回 null。您必须(不幸的是)使用 json_last_error() 单独检查它们。

关于PHP Guzzle POST 请求返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222259/

相关文章:

php - guzzlehttp 在 Laravel 项目上抛出错误 - strtolower() : Argument #1 ($string) must be of type string, int 给出

javascript - PHP 变量到 JavaScript 变量到 HTML 跨度

javascript - Cloudinary如何在php中生成签名

php - 返回 1 返回 0 VS 返回 true 返回 false

php - 使用 Guzzle HTTP 客户端时如何禁用代理?

php - Guzzle 如何发送异步 Web 请求?

php - 如何使用MYSQL获取某个单词后所有具有大写字符的数据库

rest - 在 HTTP POST 响应中包含创建的资源?

ruby-on-rails - 我是否需要对作为 POST 表单参数传递的字符串(例如 URL)进行编码

javascript - Jquery:删除 $ ('#id' ).on(...) 元素的 id 更改时的监听器