authorize.net json返回额外的字符

时间:2016-02-04 09:15:07

标签: php json authorize.net

我有这段代码

$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."}]}}

响应标题

  

HTTP / 1.1 200好的   缓存控制:私有   内容长度:232   Content-Type:application / json;字符集= utf-8的   服务器:Microsoft-IIS / 7.5   X-AspNet-版本:2.0.50727   X-Powered-By:ASP.NET   Access-Control-Allow-Origin:*   访问控制允许方法:PUT,OPTIONS,POST,GET   Access-Control-Allow-Headers:x-requested-with,cache-control,content-type,origin,method,SOAPAction   日期:星期四,2016年2月4日09:08:15 GMT   连接:保持活力

由于额外的字符我不能json_decode字符串。可以做些什么?

2 个答案:

答案 0 :(得分:3)

我在开发the code that handles the response时遇到了同样的问题。在http://ideone.com/WUI7xD中,我必须删除这些字符,以便将字符串正确解码为JSON。

第113行:

$this->responseJson = preg_replace('/[\x00-\x1F\x80-\xFF]/', '', $responseJson);

答案 1 :(得分:0)

我在使用JSON.parse()的Node.js中遇到了同样的问题。

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);