由于我的博客用了wordpress插件:“search-everything”,最近发表文章成功后,后台总是出现如下错误:
Fatal error: Uncaught Error: Cannot use object of type WP_Error as array in /fujieace/nginx/html/wp-content/plugins/search-everything/search-everything.php:927 Stack trace: #0 /fujieace/nginx/html/wp-includes/class-wp-hook.php(289): se_post_publish_ping(5096) #1 /fujieace/nginx/html/wp-includes/class-wp-hook.php(311): WP_Hook->apply_filters(NULL, Array) #2 /fujieace/nginx/html/wp-includes/plugin.php(478): WP_Hook->do_action(Array) #3 /fujieace/nginx/html/wp-includes/post.php(4846): do_action('publish_post', 5096, Object(WP_Post)) #4 /fujieace/nginx/html/wp-includes/post.php(4160): wp_transition_post_status('publish', 'draft', Object(WP_Post)) #5 /fujieace/nginx/html/wp-includes/post.php(4354): wp_insert_post(Array, false) #6 /fujieace/nginx/html/wp-admin/includes/post.php(419): wp_update_post(Array) #7 /fujieace/nginx/html/wp-admin/post.php(227): edit_post() #8 {main} thrown in /fujieace/nginx/html/wp-content/plugins/search-everything/search-everything.php on line 927
中文翻译
/fujieace/nginx/html/wp-content/plugins/search-everything/search-everything.php,第927行,出现致命错误:未捕获错误:无法将类型的对象用作数组。
原因:
上面报错其实已经说明了原因了,通俗点讲就是,我要的是一个数组类型数据,结果你给我的是一个对象类型数据,因此才报错了,这就是典型的类型不匹配。再加上PHP又是弱语言,对类型这块本身也没怎么严格要求。
解决方法
虽然上面报了“Fatal error”,依然不影响我文章发表成功。解不解决?都不影响网站的运行。但是,对于研究技术的人来说,肯定还是要解决的。具体操作如下:
1、打开 /fujieace/nginx/html/wp-content/plugins/search-everything/search-everything.php 文件。
2、查看 927行 代码:
$response = json_decode($zemanta_response['body']);
我看了这行代码后,立马就明白了,这行代码写得有问题,少了一个 true 。
3、只需要将:
$response = json_decode($zemanta_response['body']);
修改为
$response = json_decode($zemanta_response['body'],true);
4、保存
大家只要去PHP官方手册查一下这个函数用法,大家就能明白为什么了?
json_decode
(PHP 5 >= 5.2.0, PHP 7, PHP 8, PECL json >= 1.2.0)
json_decode — 对 JSON 格式的字符串进行解码
说明
json_decode ( string $json , bool $assoc = false , int $depth = 512 , int $options = 0 ) : mixed
接受一个 JSON 编码的字符串并且把它转换为 PHP 变量
参数
json
待解码的 json string 格式的字符串。
这个函数仅能处理 UTF-8 编码的数据。
assoc
当该参数为 true 时,将返回 array 而非 object 。
depth
指定递归深度。
options
由 JSON_BIGINT_AS_STRING, JSON_INVALID_UTF8_IGNORE, JSON_INVALID_UTF8_SUBSTITUTE, JSON_OBJECT_AS_ARRAY, JSON_THROW_ON_ERROR 组成的掩码。
经过测试,此解决方法并没有任何的效果。我改了代码依然报这个错误,由于最近时间比较忙,暂时先忽略这个问题吧!毕竟也无大碍。