获取JSON数据会导致警告:htmlSpecialchars()要求参数1为字符串,在第4529行的wp-includes/formatting.php中给出的数组 [英] Getting JSON data results in warning: htmlspecialchars() expects parameter 1 to be string, array given in wp-includes/formatting.php on line 4529

查看:49
本文介绍了获取JSON数据会导致警告:htmlSpecialchars()要求参数1为字符串,在第4529行的wp-includes/formatting.php中给出的数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

5年前,我付钱给别人为我的网站做了一个插件,现在我正试图让它再次正常工作。我不知道JSON是如何工作的,所以这是我第一次尝试它。基本上,我的插件将使用Iframely API获取一篇文章的摘要,并将数据从编辑器中的URL保存到自定义字段。

我的function.php中有问题的代码是:

function post_extra_save( $post_id, $post){
global $pagenow;
if ($pagenow == 'post.php') {
    if ( has_post_format('link', $post_id)) {
        $url = get_post_field('post_content', $post_id);
        $request_url = 'https://iframe.ly/api/iframely?url='. urlencode($url) .'&api_key='.get_field_object('api_key', 'option')['value'];
        $response = wp_remote_get( esc_url_raw( $request_url ) );
        $api_response = json_decode( wp_remote_retrieve_body( $response ), true );
        update_field('field_61942d74195e7', $api_response);
    }
}
}
add_action( 'save_post', 'post_extra_save', 10, 2 );

它所做的是将URL加上API密钥发送到Iframely API,返回JSON并将原始输出保存到自定义字段。

当我保存帖子时,收到以下错误信息:

Warning: htmlspecialchars() expects parameter 1 to be string, array given in /wp-includes/formatting.php on line 4529

知道这是什么意思吗?

推荐答案

找到了我的问题。我让它工作的是,它从POST内容中获取URL,从原始JSON中对其进行编码,然后对其进行解码。

$request = wp_remote_get( 'https://iframe.ly/api/iframely?url='. urlencode($url) .'&api_key='.get_field_object('api_key', 'option')['value'] );
$data_raw = json_encode( wp_remote_retrieve_body( $request ));
$data = json_decode($data_raw);

这篇关于获取JSON数据会导致警告:htmlSpecialchars()要求参数1为字符串,在第4529行的wp-includes/formatting.php中给出的数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆