php - 反序列化数据不起作用

标签 php wordpress serialization

在过去的 5 个小时里一直在做这件事,我感到很困惑。尝试了最荒谬的功能来尝试修复它但无济于事。

我正在从 WP 数据库中检索数据。在插入之前,数据已使用 PHP 中的 serialize() 函数序列化了 1 个数组。然后使用 WP 函数 update_user_meta 将其插入到 WP 数据库中。 This function 的引用资料说:

$meta_value
(mixed) (required) The new desired value of the meta_key, which must be different from the
existing value. Arrays and objects will be automatically serialized. 
Note that using objects may cause this bug to popup.
    Default: None

这让我觉得数据可能被序列化了两次。尽管经历了很多功能,如 unserialize()array_mapjson_decode 以及这些功能的组合,但我现在得到了以下内容:

$i = 0;
while($i < count($fbData)){

    $someValue = $fbData[$i]['meta_value'];
    $usermeta = array_map( function( $a ){ return $a[0]; }, get_user_meta( $fbData[$i]['user_id'] ));
    if( $usermeta['facebookmeta'] ){
        $unserialized = unserialize( $usermeta['facebookmeta'] );
        //other methods tried: unserialize( unserialize
        // unserialize( json_decode(
        // json_decode( unserialize( json_decode(
        // json_decode( unserialize(
        // unserialize( array_map( 
        // unserialize( array_map( json_decode
        // whole lot others
        var_dump( $unserialized );
    }
$i++;
}

但是这不起作用。这与 $fbData 一起使用:

'facebookmeta' => string 's:668:"a:16:{s:2:"id";s:9:"123456";s:4:"name";s:12:"Henkie";s:10:"first_name";s:4 //and so on

这是结果:

string 'a:16:{s:2:"id";s:9:"123456";s:4:"name";s:12:"Henkie";s:10:"first_name";s:4: //and so on

从结果中可以看出,它只是从开头删除了“s:668:””,这表明它是一个 668 字符的字符串,其余部分保持不变。

为什么反序列化不能正常工作?

最佳答案

哎呀找到答案了! :) WP功能隐藏在functions.php中来自 WP 本身。

function maybe_unserialize( $original ) {
    if ( is_serialized( $original ) ) // don't attempt to unserialize data that wasn't serialized going in
        return @unserialize( $original );
    return $original;
}

通过执行以下操作修复了反序列化:

$unserialized = maybe_unserialize( unserialize( $usermeta['facebookmeta'] ));

以整齐的数组形式返回所有内容! :)

快乐快乐! :)

关于php - 反序列化数据不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16281156/

相关文章:

php - 如何检测嵌入式视频编码比特率

php - YAML国际化

c# - 如何将远程 URL 中的图像序列化为 IsolateStorage 或 XML?

json - 冷融合9 : Using serializeJson on Hibernate entity encodes numeric properties as strings

c++ - boost 使用 struct 中包含的 typedef 定义的 native 类型的序列化

php - 如何在 phalcon 中创建图像链接

php - 降序在查询中不起作用,但它应该起作用?

php - 删除 WordPress 子主题中的父主题排队文件

mysql - 启动 mariadb 时出错 - 没有这样的过程

php - Bogo 插件 : Post in alternative language appears on blog despite draft, 预定或 bin 状态