如何在api请求中声明json的base64?

时间:2019-04-18 06:11:54

标签: php json rest

我正在尝试在api请求中使用json数组的base64编码。 这是json数组:

{"action":"START_STREAMING","metricsList": [{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"STATUS"},":[{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"TIME"},{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"INFO"}],"duration":60}

这是我当前的解决方案:

  $command = base64_encode({"action":"START_STREAMING","metricsList": [{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"STATUS"},":[{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"TIME"},{"category":"HARDWARE","subcategory":"MACHINE","component":"DASHBOARD","metric":"INFO"}],"duration":60});

  $request = [
        'command' => $command
    ];

我很确定这是不对的。有帮助吗?

1 个答案:

答案 0 :(得分:0)

  

即使Base64编码不是加密,但有时也可以   在单个字符串中传递数据或参数很有用。

Base64 URL编码:

function b64link_encode($string){
    $string = base64_encode($string);
    $string = urlencode($string);
    return $string ;
}

对该数据进行编码并通过

Base64 URL解码:

function b64link_decode($string){
    $string = urldecode($string);
    $string = base64_decode($string);
    return $string ;
}
  

或者你可以做   see this

URL中的某些字符很特殊,例如+表示空格。要通过URL发送任意数据,您需要对URL进行转义以避免将数据中包含的字符识别为“特殊字符”。由于要将数据放入HTML中,因此还需要对HTML进行转义,以避免将URL编码数据中的字符识别为特殊的HTML字符。因此:

$data = /* some data */;
$base64Data = base64_encode($data);
$urlData = urlencode($base64Data);
$htmlData = htmlspecialchars($urlData);
printf('<input type="hidden" value="%s" name="pass-it-on">', $htmlData);