【问题标题】:Getting JSON data results in warning: htmlspecialchars() expects parameter 1 to be string, array given in wp-includes/formatting.php on line 4529获取 JSON 数据会导致警告:htmlspecialchars() 期望参数 1 是字符串,数组在 wp-includes/formatting.php 的第 4529 行
【发布时间】:2021-12-28 22:55:21
【问题描述】:

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

知道这意味着什么吗?

【问题讨论】:

  • 都没有用,是的,我确实问了另一个问题,但我收到一条新的错误消息并将其删除并将其重写为该主题。

标签: json wordpress wordpress-theming advanced-custom-fields


【解决方案1】:

发现我的问题。我让它工作的是它从帖子内容中获取 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);

【讨论】:

    猜你喜欢
    • 2019-06-21
    • 2015-04-29
    • 1970-01-01
    • 1970-01-01
    • 2017-03-22
    • 1970-01-01
    • 2013-12-11
    • 2018-02-22
    • 2018-11-10
    相关资源
    最近更新 更多