duanhong8839 2017-12-19 19:54
浏览 298
已采纳

php-base64编码的数据在json_encode之后丢失

I use the following code to generate a encrypted string for a given string.

class MY_class{
    public static function cryption($string){
        $output = base64_encode(openssl_encrypt($string, $cipher_method, $key, 0, $iv));
        $output = $iv . $output;
        return $output;
    }
}

Using this code I generated the encrypted id of the id of a model object and replaced the original id.

$Model_object = Model_class::find(1);
$Model_object->id = MY_class::cryption($Model_object->id);
echo json_encode($Model_object);

For a given instance print_r() of $Model_object after replacing the original id with encrypted id gives me the following result.

[index] => Array
    (
        [id] => 4df73f34cUYxVmVLWlFUU2M9
    )

But json_encode() only outputs only the first decimal digits of the of the encrypted id.

{"id":4}

I'd really appreciate if someone can explain why this is happening and how to overcome this problem.

P.S. I have already googled this problem and searched in Stack Overflow too. So, don't mention it in comments.

  • 写回答

1条回答 默认 最新

  • dpdfh60088 2017-12-19 21:37
    关注

    As @LSerni said, I was implicitly assigning the encrypted id which is a string to the integer property $Model_object->id.

    So I created a new dynamic property $Model_object->encrypted_id and assigned the encrypted id to it and called that property on the other side. It worked like a charm.

    Thank you very much @LSerni for your insight on the problem.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵