Base64 图像在 JSON 中作为数组出现

Base64 image coming across as array in JSON

我目前正在与生成 UPS 货件的第三方 API 进行交互。我正在使用 returns base64 编码图像和跟踪号码的端点之一。我遇到的问题是 base64 编码图像显示为 27,000+ 行数组而不是字符串。

我一直在和他们的程序员交谈,但他是一个 .net 人,只能告诉我这个问题与我收到带有 UTF-8 编码的 json 有关。他说 "there should be some kind of way to choose ASCIIEncoding when receiving the value back from the JSON package"。

这是我用于通过 cURL 与第三方 API 交互的代码:

/**
 * @param $url
 * @param $method
 * @param array $data
 * @return mixed
 */
protected function callApi($url, $method, $data = array())
{
    $curlObj = curl_init();
    curl_setopt($curlObj, CURLOPT_HTTPHEADER, array(
        'Content-Type: application/json',
        'Accept: application/json'
    ));
    switch ($method)
    {
        case "POST":
            curl_setopt($curlObj, CURLOPT_POST, 1);
            if (!empty($data)) {
                $data = json_encode($data);
                curl_setopt($curlObj, CURLOPT_POSTFIELDS, $data);
            }
            break;
        case "PUT":
            curl_setopt($curlObj, CURLOPT_PUT, 1);
            break;
        default:
            if (!empty($data))
                $url = sprintf("%s?%s", $url, http_build_query($data));
    }

    curl_setopt($curlObj, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
    curl_setopt($curlObj, CURLOPT_USERPWD, $this->_apiUser .":" . $this->_apiPassword);
    curl_setopt($curlObj, CURLOPT_URL, $url);
    curl_setopt($curlObj, CURLOPT_RETURNTRANSFER, 1);
    $result = curl_exec($curlObj);
    curl_close($curlObj);

    return $result;
}

下面是专门调用此方法以设置发货的代码:

 public function createShipment($quoteId) {
    $data = array('QuoteId' => $quoteId);

    return json_decode($this->callApi($this->_apiUrl . 'shipment/' . $quoteId . '?sessionID=' . $this->_sessionId, 'POST', $data));
}

最后,我收到了 json 的一小部分。除了图片数组中的几行之外,我已经砍掉了所有行,以免向页面发送垃圾邮件,但可以清楚地说明我正在使用的内容。

{ "CarrierCode": 1, "CodAmount": 0, "IsSmartPickUp":错误, "IsThermal":错误, "LabelImage": [ 71, 73、 70, 56, 55, 97, 120, 5、 32, 3、 231、 0, 0, 0, 0, 0, 1、 1、 1、 2、 2、 2、 3、 3、 3、 4、 4、 4、 5、 5、 5、 6、 6、 6、 7、 7、 7、 8, 8, 8, 9、 9、 9、 10, 10, 10, 11、 11、 11、 12, 12, 12, 13、 13、 13、 14, 14, 14 ], "QuoteId": "b014d267-0850-49ce-b71d-28b82966b1c9", "ShipmentId": "b014d267-0850-49ce-b71d-28b82966b1c9", "TotalShipments": "1", "TrackingNumber":“1Z1A39W90393227795” }

LabelImage 应该是 base64 图像。我如何以正确的格式接收它,以便我可以为我们的客户服务人员打印图像?

试试这个代码:

$result = createShipment($quoteId);
$base64_str = '';
foreach($result['LabelImage'] as $charCode) {
    $base64_str .= chr($charCode);
}
$image = base64_decode($base64_str);

让我们假设 .net 开发人员是正确的,您必须通知服务器您 accept/expect 使用 ASCII 编码(或类似的编码)的响应数据。
那么仍然有几种可能的方法来触发该行为 "in" 服务器应用程序。

按likelyhood/correctness降序排列:

  1. Accept-Charset: iso-8859-1 添加到请求数组 header 中。
  2. 将 Content-Type header 更改为 Content-Type: application/json; charset=iso-8859-1 ,因为没有指示,否则服务器应该使用请求的编码来回答* - 但你只是 "allowed" 也在请求中发送 iso-8859-1 数据。
  3. 将接受 header 更改为 Accept: application/json;charset=iso-8859-1 - 滥用接受 header 的 parameter 字段;不好,但我见过这样的实现,但是...先尝试其他选项。

*) 哎呀,或者不是.... rfc2616 中的“14.2 Accept-Charset”另有说明:If no Accept-Charset header is present, the default is that any character set is acceptable.