不确定为什么我的响应以字符串形式返回而不是JSON。我正在程序上测试authorize.net intgration而不在服务器上使用composer。有人能帮帮我吗?
$API_LOGIN_ID = 'xxxxxxxxxxxxxxx';
$API_TRANSACTION_KEY = 'xxxxxxxxxxxxxxx';
$secure_headers = 'xxxxxxxxxxxxxxx';
$merchantAuthentication = array(
'name' => $API_LOGIN_ID,
'transactionKey' => $API_TRANSACTION_KEY
);
$billTo = array(
'firstName'=>$firstName,
'lastName'=>$lastName,
'company'=>$company,
'address'=>$address,
'city'=>$city,
'state'=>$state,
'zip'=>$zip,
'country'=>'USA'
);
$amount = '123.99';
$customer = array('email'=>'ferdieserrano@yahoo.com');
$transactionRequest = array(
'transactionType' => 'authCaptureTransaction',
'amount'=> $amount,
'customer'=>$customer,
'billTo'=>$billTo
);
$hostedPaymentBillingAddressValue = '{"show": true, "required":true}';
$hostedPaymentBillingAddressOptions = array(
'settingName'=>'hostedPaymentBillingAddressOptions',
'settingValue'=> $hostedPaymentBillingAddressValue
);
$hostedPaymentButtonValue = '{"text" : "Pay Now"}';
$hostedPaymentButtonOptions = array(
'settingName'=>'hostedPaymentButtonOptions',
'settingValue'=> $hostedPaymentButtonValue
);
$hostedPaymentCustomerValue = '{"showEmail":true, "requiredEmail":true}';
$hostedPaymentCustomerOptions = array(
'settingName'=>'hostedPaymentCustomerOptions',
'settingValue'=> $hostedPaymentCustomerValue
);
$hostedPaymentPaymentValue = '{"cardCodeRequired" : true}';
$hostedPaymentPaymentOptions = array(
'settingName'=>'hostedPaymentPaymentOptions',
'settingValue'=> $hostedPaymentPaymentValue
);
$hostedPaymentReturnOptionsValue = '{"url":"https://www.example.com/continue","urlText":"Continue","cancelUrl":"https://example.com/cancel","cancelUrlText":"Cancel"}';
$hostedPaymentReturnOptions = array(
'settingName'=>'hostedPaymentReturnOptions',
'settingValue'=> $hostedPaymentReturnOptionsValue
);
$hostedPaymentSecurityOptionsValue = '{"captcha" : true}';
$hostedPaymentSecurityOptions = array(
'settingName'=>'hostedPaymentSecurityOptions',
'settingValue'=> $hostedPaymentSecurityOptionsValue
);
$setting = array($hostedPaymentBillingAddressOptions, $hostedPaymentButtonOptions, $hostedPaymentCustomerOptions, $hostedPaymentPaymentOptions, $hostedPaymentReturnOptions, $hostedPaymentSecurityOptions);
$hostedPaymentSettings = array('setting'=>$setting);
$getHostedPaymentPageRequest = array(
'merchantAuthentication' => $merchantAuthentication,
'transactionRequest' =>$transactionRequest,
'hostedPaymentSettings' =>$hostedPaymentSettings
);
$options = array('getHostedPaymentPageRequest'=>$getHostedPaymentPageRequest);
$url = "https://apitest.authorize.net/xml/v1/request.api";
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, false);
curl_setopt($ch, CURLOPT_VERBOSE, true);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($options));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
)
);
$result = curl_exec($ch);
curl_close($ch);
print '<pre>';
print_r(json_decode($result));
print '</pre>';
var_dump($result);
我已经玩了一段时间,但我没有得到JSON。提前谢谢
答案 0 :(得分:1)
感谢您的代码使用了一小部分:
$result = json_decode(removeBOM($result), true);
再次感谢!
答案 1 :(得分:0)
替换这些行:
curl_setopt($ch, CURLOPT_POSTFIELDS,json_encode($options));
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
'Content-Type: application/json',
'Accept: application/json'
)
);
使用:
$options = json_encode($options);
curl_setopt($ch, CURLOPT_POSTFIELDS, $options);
curl_setopt($ch, CURLOPT_HTTPHEADER, array (
'Content-Type: application/json',
'Content-Length: ' . strlen($options)
)
);
好吧,检查完你的代码后,
我得到了自己的回复中的错误,其中返回了BOM,
这意味着你需要删除它,
我有一个非常好的function来从你的字符串中删除BOM。
function removeBOM($data) {
if (0 === strpos(bin2hex($data), 'efbbbf')) {
return substr($data, 3);
}
}
所以,您的最终代码可能如下所示:
function removeBOM($data) {
if (0 === strpos(bin2hex($data), 'efbbbf')) {
return substr($data, 3);
}
}
$result = curl_exec($ch);
curl_close($ch);
$result = json_decode(removeBOM($result));
print '<pre>';
print_r($result);
print '</pre>';