wordpress rest api创建评论接口实现留言+apifox接口测试

有么有那一刻感觉明明自己很闲,却感觉很累呢?

wordpress rest api创建评论接口实现留言+apifox接口测试

唠叨一会

当你看到这篇文章的时候就意味着某个小程序已经再做评论的路上了!愿天堂的代码没有bug,愿rest api接口可以做个人;wordpress rest api创建评论接口实现留言+apifox接口测试

api文档

Wodpress Rest Api  Create a Comment:https://developer.wordpress.org/rest-api/reference/comments/

说明

通过以上的文档说明可以看到我们小程序获取的评论是

GET /wp/v2/comments

而新建一个评论提交依然还是这个接口,只是get换成了post ==是不是感觉很神奇

POST /wp/v2/comments

参数

author The ID of the user object, if author was a user.
author_email Email address for the object author.
author_ip IP address for the object author.
author_name Display name for the object author.
author_url URL for the object author.
author_user_agent User agent for the object author.
content The content for the object.
date The date the object was published, in the site’s timezone.
date_gmt The date the object was published, as GMT.
parent The ID for the parent of the object.
post The ID of the associated post object.
status State of the object.
meta Meta fields.

apifox请求

难得有一个测试接口的软件是中文的 Apidox下载:https://www.apifox.cn/

下载安装后打开安下图的方式新建一个接口,请求方式改成post和输入我们我们的接口地址,因为我是本地测试随意用取了域名,我们采用最简单的url传参数方式来做测试;在请求参数中我们选择Params,数据中我们有几个是必要,例如:post(评论文章的id),author_name(评论用户名称),author_email(评论用户电子邮件),添加好了之后我们点击保存随后运行

wordpress rest api创建评论接口实现留言+apifox接口测试

点击运行后,来到了发起请求的界面,要注意右上方要选择环境我们可以选择测试环境或者正式环境,随后点击发送

wordpress rest api创建评论接口实现留言+apifox接口测试

报错了,提示我们“抱歉,您必须登录后再评论”,因为post请求必须要登录,可是我们在小程序中就不好操作了;虽然百度很多满分作文但还是有写靠谱的,我们先在主题的functions.php文件添加代码

function filter_rest_allow_anonymous_comments() {
    return true;
}
add_filter('rest_allow_anonymous_comments','filter_rest_allow_anonymous_comments');

在点击一次发送,返回201,后台收到留言内容。如果有提示没有对应的文章内容可以在运行界面下检测Params请求数据(切记)

wordpress rest api创建评论接口实现留言+apifox接口测试

wordpress rest api创建评论接口实现留言+apifox接口测试

(3)
枫瑞博客枫瑞博客
上一篇 2021-02-17 20:02
下一篇 2021-03-05 20:02

相关推荐

回复 小天

您的电子邮箱地址不会被公开。 必填项已用*标注

评论列表(10条)