dongqiao8417 2016-04-15 07:29
浏览 44

在使用wp_remote_post之前清理HTML内容

I have a WordPress plugin, which will gather post's information such as post title, post content and send to 3rd party application to do some analysis.

Basically will store post title and post content into array, encode the array into JSON format and send to 3rd party application via wp_remote_post. I'm facing challenge in the post content which may containing some html tag and ruin my JSON data.

What should I do to prevent the post content breaking my JSON data? Should I sanitize it or in other way?

Example code as below:

$args = array(
    'title' => get_the_title(),
    'content' => get_the_content()
);

$json = json_encode($args);

wp_remote_post( 'http://dummydomain.com/', array(
    'body' => array(
        'data' => $args
    )
) );
  • 写回答

1条回答 默认 最新

  • douye9822 2016-04-16 08:21
    关注

    All you need to do is to pass additional flags to json_encode, to notify it that your code can contains HTML tags. Passing this two flags should be enough:

    JSON_HEX_TAG - All < and > are converted to \u003C and \u003E. Available since PHP 5.3.0. JSON_HEX_QUOT - All " are converted to \u0022. Available since PHP 5.3.0.

    http://php.net/manual/en/json.constants.php

    Your code could look like this:

    $args = array(
        'title' => get_the_title(),
        'content' => get_the_content()
    );
    
    $json = json_encode($args, JSON_HEX_TAG | JSON_HEX_QUOT);
    
    wp_remote_post( 'http://dummydomain.com/', array(
        'body' => array(
            'data' => $json
        )
    ) );
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题