Home > Backend Development > PHP Tutorial > Wordpress 插件开发 如何实现接收json数据

Wordpress 插件开发 如何实现接收json数据

WBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWBOYWB
Release: 2016-06-06 20:15:37
Original
1243 people have browsed it

先需要插件实现功能,提交评论json数据到特定的路径,然后处理数据.

尝试添加钩子到解析请求的动作.

<code>function sync_comment($obj){
    test($_POST);
}
add_action( 'parse_request', 'sync_comment' );</code>
Copy after login
Copy after login

实际上不可行,只能获取GET的数据,POST为空.

不知道是不是跨域安全的问题,对Wordpress不熟悉,比较棘手.

寻求解决方案建议.

回复内容:

先需要插件实现功能,提交评论json数据到特定的路径,然后处理数据.

尝试添加钩子到解析请求的动作.

<code>function sync_comment($obj){
    test($_POST);
}
add_action( 'parse_request', 'sync_comment' );</code>
Copy after login
Copy after login

实际上不可行,只能获取GET的数据,POST为空.

不知道是不是跨域安全的问题,对Wordpress不熟悉,比较棘手.

寻求解决方案建议.

检查发现,请求头没有提交Content-Type: application/x-www-form-urlencoded就不能解析$_POST
解决方案改成这样就好

<code>$data = json_decode(file_get_contents("php://input"));</code>
Copy after login
Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Issues
php data acquisition?
From 1970-01-01 08:00:00
0
0
0
PHP extension intl
From 1970-01-01 08:00:00
0
0
0
How to learn php well
From 1970-01-01 08:00:00
0
0
0
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template