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 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog