简体   繁体   中英

PHP Guzzle POST Request Returning Null

I'm racking my brains over this - the request goes fine in Postman, but I can't access the body using 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()));

The above returns null - no errors, just null. The below is the same request in Postman, successful with a body.

在此处输入图片说明

Any ideas would be really appreciated, thanks!

You should use $res->getBody()->getContents() .

And json_decode() returns null in case of any error. You have to check them separately (unfortunately) with json_last_error() .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM