编程语言
首页 > 编程语言> > php – authorize.net json返回额外的字符

php – authorize.net json返回额外的字符

作者:互联网

我有这个代码

$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 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时遇到了同样的问题.在the code that handles the response中,我不得不将这些字符去掉,以便将字符串正确解码为JSON.

第113行:

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

标签:json,php,authorize-net
来源: https://codeday.me/bug/20190628/1312926.html