我想解码 JSON 数据并使用 json_decode 函数将其存储到数组中,但它返回 NULL 值。我认为这是因为 UTF-8 BOM。任何解决方案?我使用带有 xampp 的 Windows7 操作系统。我将我的编码设置为
header('Content-type:application/json; charset=utf-8');
JSON数据
{"command":"E101","user_id":"someuser","movie_id":"1","link_id":"2"}
JSON 错误:控制字符错误,可能编码不正确
$json_errors = array(
JSON_ERROR_NONE => 'No error has occurred',
JSON_ERROR_DEPTH => 'The maximum stack depth has been exceeded',
JSON_ERROR_CTRL_CHAR => 'Control character error, possibly incorrectly encoded',
JSON_ERROR_SYNTAX => 'Syntax error',
);
echo 'Last error : ', $json_errors[json_last_error()], PHP_EOL, PHP_EOL;
如果我解析这个 JSON,没有发生错误
{"command":"E101","user_id":"someuser","movie_id":"movie_id","link_id":"link_id"}
唯一的区别是我将字符串数据存储到 movie_id 和 link_id 中。为什么会这样?
JSON 数据 Bin2Hex() 7b22636f6d6d616e64223a2245313031222c226d6f7669655f6964223a226d6f7669655f6964222c226c696e6b5f6964223a226c696e005f69604227
我加密 JSON 数据并通过客户端,我在服务器端解密。
这是我的加密功能
public function ajax_enc($data){
$vector = "myvector";
$filter = new Zend_Filter_Encrypt(array('adapter' => 'mcrypt', 'key' => $this->_AJAXKEY));
$filter->setVector($vector);
$encrypted = $filter->filter($data);
// bin2hex for user use case
return bin2hex($encrypted); // rawurlencode(..) works
}
解密
public function ajax_dec($data)
{
$vector = "myvector";
$filter = new Zend_Filter_Decrypt(array('adapter' => 'mcrypt', 'key' => $this->_AJAXKEY ));
$filter->setVector($vector);
$decoded = pack('H*', $data);
$decrypted = $filter->filter($decoded);
return $decrypted;
}