authorize.net json return 额外字符
authorize.net json return extra characters
我有这个代码
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("'Content-Type: application/json; charset=utf-8'"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_str));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$xmlresult = curl_exec($ch);
$xmlError = $xmlresult;
$json = json_decode($xmlresult, true);
我得到的答案是 json 但我无法转换,因为一开始的答案是多余的字符 请参见示例
п»ї{"customerPaymentProfileIdList":[],"customerShippingAddressIdList":[],"validationDirectResponseList":[],"messages":{"resultCode":"Error","message":[{"code":"E00039","text":"A duplicate record with ID 39223758 already exists."}]}}
回应header
HTTP/1.1 200 OK
Cache-Control: private
Content-Length: 232
Content-Type: application/json; charset=utf-8
Server: Microsoft-IIS/7.5
X-AspNet-Version: 2.0.50727
X-Powered-By: ASP.NET
Access-Control-Allow-Origin: *
Access-Control-Allow-Methods: PUT,OPTIONS,POST,GET
Access-Control-Allow-Headers: x-requested-with,cache-control,content-type,origin,method,SOAPAction
Date: Thu, 04 Feb 2016 09:08:15 GMT
Connection: keep-alive
因为多余的字符我不能json_decode字符串。可以做什么?
我在开发 library for accessing their JSON API. In the code that handles the response 时遇到了同样的问题,我不得不去掉这些字符才能将字符串正确解码为 JSON。
第 113 行:
$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson);
我在 Node.js 和 JSON.parse()
中遇到了同样的问题。
var https = require('https');
var requestData = {
"getCustomerProfileIdsRequest": {
"merchantAuthentication": {
"name": "your-auth-name-here",
"transactionKey": "your-trans-key-name-here"
}
}
};
var requestString = JSON.stringify(requestData);
var req = https.request({
host: "apitest.authorize.net",
port: "443",
path: "/xml/v1/request.api",
method: "POST",
headers: {
"Content-Length": requestString.length,
"Content-Type": "application/json"
}
});
req.on('response', function (resp) {
var response = '';
resp.setEncoding('utf8');
resp.on('data', function(chunk) {
response += chunk;
});
resp.on('end', function() {
var buf = new Buffer(response);
console.log('buf[0]:', buf[0]); // 239 Binary 11101111
console.log('buf[0] char:', String.fromCharCode(buf[0])); // "ï"
console.log('buf[1]:', buf[1]); // 187 Binary 10111011
console.log('buf[1] char:', String.fromCharCode(buf[1])); // "»"
console.log('buf[2]:', buf[2]); // 191 Binary 10111111
console.log('buf[2] char:', String.fromCharCode(buf[2])); // "¿"
console.log('buf[3]:', buf[3]); // 123
console.log('buf[3] char:', String.fromCharCode(buf[3])); // "{"
// Note: The first three chars are a "Byte Order Marker" i.e. `BOM`, `ZERO WIDTH NO-BREAK SPACE`, `11101111 10111011 10111111`
response = JSON.parse(response); // Throws error: 'Unrecoverable exception. Unexpected token SyntaxError: Unexpected token'
console.log(response);
});
});
req.on('error', function (error) {
console.log(JSON.stringify(error));
});
req.on('socket', function(socket) {
socket.on('secureConnect', function() {
req.write(requestString);
req.end();
});
});
如果我在响应中调用 trim()
,它会起作用:
response = JSON.parse(response.trim());
或者替换BOM
:
response = response.replace(/^\uFEFF/, '');
response = JSON.parse(response);
我有这个代码
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, Array("'Content-Type: application/json; charset=utf-8'"));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data_str));
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
$xmlresult = curl_exec($ch);
$xmlError = $xmlresult;
$json = json_decode($xmlresult, true);
我得到的答案是 json 但我无法转换,因为一开始的答案是多余的字符 请参见示例
п»ї{"customerPaymentProfileIdList":[],"customerShippingAddressIdList":[],"validationDirectResponseList":[],"messages":{"resultCode":"Error","message":[{"code":"E00039","text":"A duplicate record with ID 39223758 already exists."}]}}
回应header
HTTP/1.1 200 OK Cache-Control: private Content-Length: 232 Content-Type: application/json; charset=utf-8 Server: Microsoft-IIS/7.5 X-AspNet-Version: 2.0.50727 X-Powered-By: ASP.NET Access-Control-Allow-Origin: * Access-Control-Allow-Methods: PUT,OPTIONS,POST,GET Access-Control-Allow-Headers: x-requested-with,cache-control,content-type,origin,method,SOAPAction Date: Thu, 04 Feb 2016 09:08:15 GMT Connection: keep-alive
因为多余的字符我不能json_decode字符串。可以做什么?
我在开发 library for accessing their JSON API. In the code that handles the response 时遇到了同样的问题,我不得不去掉这些字符才能将字符串正确解码为 JSON。
第 113 行:
$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson);
我在 Node.js 和 JSON.parse()
中遇到了同样的问题。
var https = require('https');
var requestData = {
"getCustomerProfileIdsRequest": {
"merchantAuthentication": {
"name": "your-auth-name-here",
"transactionKey": "your-trans-key-name-here"
}
}
};
var requestString = JSON.stringify(requestData);
var req = https.request({
host: "apitest.authorize.net",
port: "443",
path: "/xml/v1/request.api",
method: "POST",
headers: {
"Content-Length": requestString.length,
"Content-Type": "application/json"
}
});
req.on('response', function (resp) {
var response = '';
resp.setEncoding('utf8');
resp.on('data', function(chunk) {
response += chunk;
});
resp.on('end', function() {
var buf = new Buffer(response);
console.log('buf[0]:', buf[0]); // 239 Binary 11101111
console.log('buf[0] char:', String.fromCharCode(buf[0])); // "ï"
console.log('buf[1]:', buf[1]); // 187 Binary 10111011
console.log('buf[1] char:', String.fromCharCode(buf[1])); // "»"
console.log('buf[2]:', buf[2]); // 191 Binary 10111111
console.log('buf[2] char:', String.fromCharCode(buf[2])); // "¿"
console.log('buf[3]:', buf[3]); // 123
console.log('buf[3] char:', String.fromCharCode(buf[3])); // "{"
// Note: The first three chars are a "Byte Order Marker" i.e. `BOM`, `ZERO WIDTH NO-BREAK SPACE`, `11101111 10111011 10111111`
response = JSON.parse(response); // Throws error: 'Unrecoverable exception. Unexpected token SyntaxError: Unexpected token'
console.log(response);
});
});
req.on('error', function (error) {
console.log(JSON.stringify(error));
});
req.on('socket', function(socket) {
socket.on('secureConnect', function() {
req.write(requestString);
req.end();
});
});
如果我在响应中调用 trim()
,它会起作用:
response = JSON.parse(response.trim());
或者替换BOM
:
response = response.replace(/^\uFEFF/, '');
response = JSON.parse(response);