frostfat
frostfat

Reputation: 185

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.

enter image description here

Any ideas would be really appreciated, thanks!

Upvotes: 5

Views: 4182

Answers (1)

Alexey Shokov
Alexey Shokov

Reputation: 5010

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().

Upvotes: 6

Related Questions