我尝试使用CURL发布到API。 当我对curl_exec进行var_dump时,我收到一条消息"格式错误的JSON"。我认为我的JSON看起来非常好,它应该工作。知道为什么不是吗?这是
我的JSON结果如下:
{" AmountDebit":10,"货币":" EUR""发票":" testinvoice 123""服务" {" ServiceList":[{"动作":"支付""名称&# 34;:"理想""参数":[{"名称":"发行人""值&#34 ;: " ABNANL2A"}]}]}}
我的代码:
<?php
$postArray = array(
"Currency" => "EUR",
"AmountDebit" => 10.00,
"Invoice" => "testinvoice 123",
"Services" => array(
"ServiceList" => array(
array(
"Action" => "Pay",
"Name" => "ideal",
"Parameters" => array(
array(
"Name" => "issuer",
"Value" => "ABNANL2A"
)
)
)
)
)
);
ksort($postArray);
$post = json_encode($postArray);
echo $post . '<br><br>';
$md5 = md5($post, true);
$post = base64_encode($md5);
echo '<b>MD5 from json</b> ' . $md5 . '<br><br>';
echo '<b>base64 from MD5</b> ' . $post . '<br><br>';
$websiteKey = '-';
$uri = strtolower(urlencode('https://testcheckout.buckaroo.nl/json/transaction'));
$nonce = 'nonce_' . rand(0000000, 9999999);
$time = time();
$hmac = $websiteKey . 'POST' . $uri . $time . $nonce . $post;
$s = hash_hmac('sha256', $hmac, '-', true);
$hmac = base64_encode($s);
$url= 'https://testcheckout.buckaroo.nl/json/transaction';
echo 'Authorization: hmac ' .$websiteKey.':'.$hmac .':'.$nonce . ':'.$time;
$headers = array();
$headers[] = 'Authorization: hmac ' .$websiteKey.':'.$hmac .':'.$nonce . ':'.$time;
$headers[] = 'Content-Type: application/json';
$headers[] = 'Content-Length: ' . strlen(json_encode($postArray));
print_r($headers);
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl,CURLOPT_POSTFIELDS, json_encode($postArray));
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_HTTPHEADER, $headers);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($curl);
var_dump($result);
curl_close($curl);
?>
答案 0 :(得分:0)
正如之前的评论所说:你的json是有效的,似乎尊重Buckaroo规范(https://testcheckout.buckaroo.nl/json/Docs/Api/POST-json-Transaction)。尝试将JSON_PRESERVE_ZERO_FRACTION标志用于json_encode,以十进制强制舍入数字(1 => 1.0)