gpt4 book ai didi

php - json_decode 返回 NULL , UTF-8 BOM

转载 作者:可可西里 更新时间:2023-10-31 23:22:37 25 4
gpt4 key购买 nike

我想解码 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() 7b22636f6d6d616e64223a2245313031222c226d6f7669655f6964223a226d6f7669655f6964222c226c696e6b5f6964223a226c696e6b5f6964227d000

我通过客户端加密 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;
}

最佳答案

您的解密显然在字符串末尾留下了一堆填充 NUL 字节。

修复您的解密机制或修剪它们:trim($json, "\x0")

关于php - json_decode 返回 NULL , UTF-8 BOM,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13582930/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com