我正在为此绞尽脑汁 - 请求在 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。
任何想法将不胜感激,谢谢!
最佳答案
你应该使用 $res->getBody()->getContents()
。
并且 json_decode()
在出现任何错误时返回 null
。您必须(不幸的是)使用 json_last_error()
单独检查它们。
关于PHP Guzzle POST 请求返回 Null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46222259/