【问题标题】:authorize.net json return extra charactersauthorize.net json 返回额外的字符
【发布时间】:2016-05-13 19:42:06
【问题描述】:

我有这个代码

$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 内容类型:应用程序/json;字符集=utf-8 服务器:Microsoft-IIS/7.5 X-AspNet-版本:2.0.50727 X-Powered-By: ASP.NET 访问控制允许来源:* 访问控制允许方法: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 字符串。可以做什么?

【问题讨论】:

  • 快到 2019 年了,使用 Authorize.net API 端点时这种行为仍然存在(已针对 https://apitest.authorize.net/xml/v1/request.api 进行验证)。

标签: php json authorize.net


【解决方案1】:

我在开发 library for accessing their JSON API 时遇到了同样的问题。在the code that handles the response 中,我必须去掉这些字符才能将字符串正确解码为 JSON。

第 113 行:

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

【讨论】:

  • 我经常看到这个正则表达式。它真的应该通过整个 JSON 字符串以任何顺序去除任意数量的这些字符吗?不应该只看前三个字符吗? '/^[\x00-\x1F\x80-\xFF]{3,3}/'
【解决方案2】:

我在 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);
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-03-23
  • 2010-12-05
  • 2017-06-28
  • 2010-10-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多