我正在尝试使用CURL从我的代码发布到API。我在结果变量上做了一个var_dump。我得到了“格式错误的JSON”我很确定我的JSON很好。
代码如下所示:
<?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 = 'secret';
$uri = strtolower(urlencode('testcheckout.buckaroo.nl/json/Transaction'));
$nonce = 'nonce_' . rand(0000000, 9999999);
$time = time();
$hmac = $websiteKey . 'POST' . $uri . $time . $nonce . $post;
$s = hash_hmac('sha256', $hmac, 'secret', 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($post);
print_r($headers);
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl,CURLOPT_POSTFIELDS, $post);
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);
?>
$ post变量的响应是:
{"AmountDebit":10,"Currency":"EUR","Invoice":"testinvoice 123","Services":{"ServiceList":[{"Action":"Pay","Name":"ideal","Parameters":[{"Name":"issuer","Value":"ABNANL2A"}]}]}}
为什么var_dump $ result返回格式错误的JSON?我认为它与我的标题有关,但它没有。我用Google搜索,但我找不到明确的答案。所以我希望我能在这里找到一些帮助
显然我覆盖了我试图发送的变量。我想我现在正在做这件事,我仍然得到格式错误的JSON事情:
<?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);
$post1 = 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('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($post1);
print_r($headers);
$curl = curl_init();
curl_setopt($curl, CURLOPT_CUSTOMREQUEST, "POST");
curl_setopt($curl,CURLOPT_POSTFIELDS, $post1);
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);
?>