WordPress Rest API 最细接口详解
0、前言
感觉好久没有写点比较有内容的blog了。一直没什么空,最近假期刚好有点时间,回顾之前忙的东西,刚好可以对前段时间对wordpress(下面简称:wp)将其应用到iOS的app移动开发应用当中的调研结果进行总结。我觉得这个挺有意思的,而且有必要做个笔记。
我做iOS开发也有几年了,什么是wordpress都没听说过,认为是一个很偏的存在。心里面充满了疑问,wp是什么?有什么用?我能拿来干嘛?
于是带着这些问题,先科普一下:
WordPress是使用PHP语言开发的博客平台,用户可以在支持PHP和MySQL数据库的服务器上架设属于自己的网站。也可以把 WordPress当作一个内容管理系统(CMS)来使用。
这是百度百科对它的定义,大概有了模糊的了解,如有兴趣进一步了解来龙去脉点击这里查看
通过上面我们基本了解wp的是一个适用广泛、拓展性很强、插件很多的开发博客页面的框架。而且该框架已经比较成熟,已经有对应的后台、安卓、web的开发教程。很遗憾的就是没有iOS开发教程。为此才开启了我的调研过程。作为移动端对wp的解读,本质上就是对WordPress
Rest API插件的解读。即将接口和参数字段摸索出来并得到应用。通过中文网站和诸多博客的借鉴、以及通过多天的亲自测试了解。得到了一套比较有用的最新的API接口:
注:
1、以下接口拼接自己的域名,如:http://chenzimin.example.cn。
2、在wpAPI文档中,有些接口没有提供,建站时需要后台服务器帮助提供,以下我这边是自己后台服务器提供接口用米字号作*标记。
3、以下接口get请求一般请求头可以不带token。
4、wo记得没错的话,wp的token有效期为24小时。
1、登录注册模块
1.1、用户注册 *
请求协议|请求方法:HTTP|POST
接口路径:/fapi/userregster.php
请求参数:
参数名 | 说明 | 类型 |
---|---|---|
username | 用户名,必填 | [string] |
password | 密码,必填 | [string] |
邮箱 | [string] | |
nickname | 昵称 | [string] |
phone | 手机号 | [string] |
成功示例:
24
直接返回新用户id。
1.2、获取Token
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/jwt-auth/v1/token
请求参数
参数类型:表单
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
username | 用户名 | 是 | [string] |
password | 密码 | 是 | [string] |
成功示例:
{ "token":"eyJ0eXAiOiJKV1QiLCJhbGciOiJIUzI1NiJ9.eyJpc3MiOiJodHRwOlwvXC96aG91YnV5aS5tZWlsYXNoaWRhaS5jbiIsImlhdCI6MTU4Njc2NDA3MCwibmJmIjoxNTg2NzY0MDcwLCJleHAiOjE1ODczNjg4NzAsImRhdGEiOnsidXNlciI6eyJpZCI6IjIyIn19fQ.7kgtWfUTlm6tbZX63V7OEpo08PAxakZiK4e9DE6Nvb8", "user_email":"", "user_nicename":"100022", "user_display_name":"1111111" }
1.3、验证Token
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/jwt-auth/v1/token/validate
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
成功示例:
{ "code":"jwt_auth_valid_token", "data":{ "status":200 } }
注意点1: 获取的token要放在请求头,并且token值的格式必须为:Bearer+token值,否侧无法判断。
注意点2: 要实现这个接口,需要后台服务器授权验证,详细参考:使用 JWT Authentication 验证
WordPress REST API 权限,只有授权验证处理通过,这个接口才能请求成功,否则会返回错误,要求授权。
1.4、账户登录 *
请求协议|请求方法:HTTP|POST
接口路径:/fapi/loginwithtoken.php
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数:
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
username | 用户名 | 是 | [string] |
pwd | 密码 | 是 | [string] |
成功示例:
{ "data":{ "ID":"22", "user_login":"1111111", "user_pass":"$P$BSz.lD0LqQxK/v8E9Un1Cc3VwnURpp.", "user_nicename":"100022", "user_email":"wyp001@123.com", "user_url":"", "user_registered":"2020-04-13 07:23:02", "user_activation_key":"", "user_status":"0", "display_name":"1111111", "father_id":"0", "reg_ip":"" }, "ID":22, "caps":{ "author":true }, "cap_key":"wp_capabilities", "roles":[ "author" ], "allcaps":{ "upload_files":true, "edit_posts":true, "edit_published_posts":true, "publish_posts":true, "read":true, "level_2":true, "level_1":true, "level_0":true, "delete_posts":true, "delete_published_posts":true, "author":true }, "filter":null }
1.5、获取用户信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/users/me
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | 用户token,格式:Bearer+token |
成功示例:
{ "id":22, "name":"1111111", "url":"", "description":"描述啊描述", "link":"http://chenzimin.example.cn/author/100022/", "slug":"100022", "avatar_urls":{ "24":"http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=24&d=mm&r=g", "48":"http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=48&d=mm&r=g", "96":"http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=96&d=mm&r=g" }, "meta":[ ], "woocommerce_meta":{ "activity_panel_inbox_last_read":"", "activity_panel_reviews_last_read":"", "categories_report_columns":"", "coupons_report_columns":"", "customers_report_columns":"", "orders_report_columns":"", "products_report_columns":"", "revenue_report_columns":"", "taxes_report_columns":"", "variations_report_columns":"", "dashboard_sections":"", "dashboard_chart_type":"", "dashboard_chart_interval":"", "dashboard_leaderboard_rows":"" }, "_links":{ "self":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/users/22" } ], "collection":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/users" } ] } }
1.6、修改用户信息
wpAPI中没有找到该接口,是根据获取用户信息get方法,推理出来,修改用户信息为post,测试部分字段有效,以下请求参数列表,是个人认为会有用到,才罗列出来的。
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/wp/v2/users/me
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
name | 显示名称 | 是 | [string] |
first_name | 名字 | 是 | [string] |
last_name | 姓氏 | 是 | [string] |
邮箱 | 是 | [string] | |
locale | 语言设置,默认:zh_CN | 是 | [string] |
nickname | 昵称 | 是 | [string] |
roles | 如:[ “author” ] | 是 | [array] |
capabilities | 功能分类设置,查看返回结果 | 是 | [string] |
avatar_urls | 头像,如:{ “24”:“http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=24&d=mm&r=g”, “48”:“http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=48&d=mm&r=g”, “96”:“http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=96&d=mm&r=g” }。 |
是 | [object] |
woocommerce_meta | 店铺的相关配置 | 是 | [string] |
成功示例:
{ "id":22, "username":"1111111", "name":"测试将name1111111修改ceshi", "first_name":"", "last_name":"", "email":"wyp001@123.com", "url":"", "description":"描述啊描述", "link":"http://chenzimin.example.cn/author/100022/", "locale":"zh_CN", "nickname":"1111111", "slug":"100022", "roles":[ "author" ], "registered_date":"2020-04-13T07:23:02+00:00", "capabilities":{ "upload_files":true, "edit_posts":true, "edit_published_posts":true, "publish_posts":true, "read":true, "level_2":true, "level_1":true, "level_0":true, "delete_posts":true, "delete_published_posts":true, "author":true }, "extra_capabilities":{ "author":true }, "avatar_urls":{ "24":"http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=24&d=mm&r=g", "48":"http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=48&d=mm&r=g", "96":"http://cn.gravatar.com/avatar/cf29dfa8761d26cf525bd3275293a527?s=96&d=mm&r=g" }, "meta":[ ], "woocommerce_meta":{ "activity_panel_inbox_last_read":"", "activity_panel_reviews_last_read":"", "categories_report_columns":"", "coupons_report_columns":"", "customers_report_columns":"", "orders_report_columns":"", "products_report_columns":"", "revenue_report_columns":"", "taxes_report_columns":"", "variations_report_columns":"", "dashboard_sections":"", "dashboard_chart_type":"", "dashboard_chart_interval":"", "dashboard_leaderboard_rows":"" }, "_links":{ "self":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/users/22" } ], "collection":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/users" } ] } }
1.7、获取所有的用户信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/users
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
1.8、获取指定用户ID的用户信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/users/用户id,如:1
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
2、页面
2.1、获取所有的页面信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/pages
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
2.2、获取指定页面ID的页面信息
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/wp/v2/pages/289
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
3、文章的分类和标签
3.1、获取文章分类数组
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/categories
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
成功示例:
[ { "id":14, "count":13, "description":"", "link":"http://chenzimin.example.cn/category/%e5%85%ab%e5%8d%a6%e5%8d%a6%e8%b1%a1/", "name":"八卦卦象", "slug":"%e5%85%ab%e5%8d%a6%e5%8d%a6%e8%b1%a1", "taxonomy":"category", "parent":0, "meta":[ ], "_links":{ "self":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/categories/14" } ], "collection":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/categories" } ], "about":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/taxonomies/category" } ], "wp:post_type":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts?categories=14" } ], "curies":[ { "name":"wp", "href":"https://api.w.org/{rel}", "templated":true } ] } }, ]
3.2、获取指定分类ID的分类信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/categories/分类id
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
3.3、获取文章的所有标签
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/tags
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
3.4、获取指定标签ID的标签信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/tags/标签id
接口使用状态:正常启用
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
3.5、获取当前wp所有的内容类型
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/types
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
3.6、获取指定内容类型
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/types/(post|page|attachment 三种类型)
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
4、文章
核心模块
4.1、获取文章列表
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/posts
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数:
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
page | 指定要返回的结果的页面 | 是 | [string] |
per_page | 返回的记录数,指定为1到100之间的整数 | 是 | [string] |
offset | 指定开始检索帖子的任意偏移量.offset = 6将使用每页的默认帖子数,但从集合中的第6个帖子开始 | 是 | [string] |
order | 默认降序。order = asc(升序)、order = desc(降序)。 | 是 | [string] |
orderby | 控制集合被排序的字段,默认为orderby = date。有效值为“date”,“relevance”,“id”,“include”,“title”和“slug” 有关其他集合支持的值,请参阅REST API参考 |
是 | [string] |
成功示例:
[ { "id":165, "date":"2020-03-10T16:06:43", "date_gmt":"2020-03-10T08:06:43", "guid":{ "rendered":"/?p=165" }, "modified":"2020-03-10T16:07:10", "modified_gmt":"2020-03-10T08:07:10", "slug":"%e5%85%ad%e7%88%bb%e7%ad%91%e5%9f%ba%e7%ac%ac%e4%b8%80%e5%85%b3-%e5%85%ab%e5%8d%a6%e5%8d%a6%e8%b1%a1%e4%b9%8b%e5%85%91%e5%ae%ab%e5%85%ab%e5%8d%a6", "status":"publish", "type":"post", "link":"/2020/03/10/%e5%85%ad%e7%88%bb%e7%ad%91%e5%9f%ba%e7%ac%ac%e4%b8%80%e5%85%b3-%e5%85%ab%e5%8d%a6%e5%8d%a6%e8%b1%a1%e4%b9%8b%e5%85%91%e5%ae%ab%e5%85%ab%e5%8d%a6/", "title":{ "rendered":"文章标题" }, "content":{ "rendered":"文章内容", "protected":false }, "excerpt":{ "rendered":"文章摘要", "protected":false }, "author":1, "featured_media":0, "comment_status":"open", "ping_status":"open", "sticky":false, "template":"", "format":"standard", "meta":{ "_links_to":"", "_links_to_target":"" }, "categories":[ 14 ], "tags":[ ], "_links":{ "self":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts/165" } ], "collection":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts" } ], "about":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/types/post" } ], "author":[ { "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/users/1" } ], "replies":[ { "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/comments?post=165" } ], "version-history":[ { "count":0, "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts/165/revisions" } ], "wp:attachment":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/media?parent=165" } ], "wp:term":[ { "taxonomy":"category", "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/categories?post=165" }, { "taxonomy":"post_tag", "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/tags?post=165" } ], "curies":[ { "name":"wp", "href":"https://api.w.org/{rel}", "templated":true } ] } } ]
4.2、获取文章详情
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/posts/文章id
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数名 | 说明 | 必填 | 类型 | 值可能性 |
---|---|---|---|---|
context | 上下文参数控制数据返回的格式 | 否 | [string] | view:默认上下文,输出普通的用户内容。edit:更新文章时用来添加附加字段,包括 title_raw, content_raw, guid_raw 和自定义字段, 以适应文章编辑。parent:上下文在嵌入另一个(如文章作者)的返回时使用。这样做是为了使用用户数据的最小子集来减少响应大小。返回父字段作为一个 ID,而不是一个嵌入的文章,以确保我们不会遍历后面的整个层次结构。 |
成功示例:
{ "id":165, "date":"2020-03-10T16:06:43", "date_gmt":"2020-03-10T08:06:43", "guid":{ "rendered":"http://chenzimin.example.cn/?p=165" }, "modified":"2020-03-10T16:07:10", "modified_gmt":"2020-03-10T08:07:10", "slug":"%e5%85%ad%e7%88%bb%e7%ad%91%e5%9f%ba%e7%ac%ac%e4%b8%80%e5%85%b3-%e5%85%ab%e5%8d%a6%e5%8d%a6%e8%b1%a1%e4%b9%8b%e5%85%91%e5%ae%ab%e5%85%ab%e5%8d%a6", "status":"publish", "type":"post", "link":"http://chenzimin.example.cn/2020/03/10/%e5%85%ad%e7%88%bb%e7%ad%91%e5%9f%ba%e7%ac%ac%e4%b8%80%e5%85%b3-%e5%85%ab%e5%8d%a6%e5%8d%a6%e8%b1%a1%e4%b9%8b%e5%85%91%e5%ae%ab%e5%85%ab%e5%8d%a6/", "title":{ "rendered":"文章标题" }, "content":{ "rendered":" ", "protected":false }, "excerpt":{ "rendered":"文章摘要", "protected":false }, "author":1, "featured_media":0, "comment_status":"open", "ping_status":"open", "sticky":false, "template":"", "format":"standard", "meta":{ "_links_to":"", "_links_to_target":"" }, "categories":[ 14 ], "tags":[ ], "_links":{ "self":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts/165" } ], "collection":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts" } ], "about":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/types/post" } ], "author":[ { "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/users/1" } ], "replies":[ { "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/comments?post=165" } ], "version-history":[ { "count":0, "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts/165/revisions" } ], "wp:attachment":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/media?parent=165" } ], "wp:term":[ { "taxonomy":"category", "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/categories?post=165" }, { "taxonomy":"post_tag", "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/tags?post=165" } ], "curies":[ { "name":"wp", "href":"https://api.w.org/{rel}", "templated":true } ] } }
4.3、创建一篇文章
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/wp/v2/posts
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数名 | 说明 | 必填 | 类型 | 值可能性 |
---|---|---|---|---|
title | 文章标题 | 是 | [string] | |
content | 文章内容 | 是 | [string] | |
excerpt_raw | 文章摘要 | 否 | [string] | |
status | 文章状态,默认为draft. (string) optional | 否 | [string] | draft:草稿,publish:发表,pending:待定, future:未来, private:私有 |
categories | 文章标题分类,传分类id | 否 | [string] | |
comment_status | 文章的评论状态,默认为设置选项 ‘default_comment_status’的值, 或 ‘closed’. (string) optional | 否 | [string] | open:打开,closed:关闭 |
ping_status | 文章的ping状态。用来确定用户是否可以提交pibgbacks和trackbacks,默认为设置选项 ‘default_ping_status’的值. (string) optional | 否 | [string] | open:打开,closed:关闭 |
sticky | 文章的置顶状态。默认为 false. (boolean) optional | 否 | [string] | true:置顶,false:不置顶 |
author | 文章的作者,作者可以为作者ID或作者对象,默认为当前作者。(string) optional | 否 | [string] | |
name | 文章别名 | 否 | [string] | |
password | 用来保护文章的密码,默认为空字符串 (string) optional | 否 | [string] | |
type | 文章类型,可以是: post, page, link, nav_menu_item, 或其他自定义文章类型. 默认为post. (string) optional | 否 | [string] | |
post_parent | 父级文章的ID,默认为0. (integer) optional | 否 | [string] | |
post_format | 文章格式,默认为standard. (string) optional | 否 | [string] | |
menu_order | 页面的顺序序号,默认为0 (integer) optional | 否 | [string] | |
post_meta | 文章的元数据,文章元数据应该是一个或多个元数据对象的数组,查看使用端点键值对创建文章元数据章节。 (array) optional | 否 | [string] | |
date | 发布文章的本地日期和时间. 日期应该是一个 RFC3339 时间戳(http://tools.ietf.org/html/rfc3339). 例如: 2014-01-01T12:20:52Z. 默认为本地日期和时间. (string) optional |
否 | [string] | |
date_gmt | 文章发布的UTC日期和时间,日期应该为一个 RFC3339 时间戳. 例如: 201401-01T12:20:52Z. 默认为当前的 GMT 日期和时间. (string) optional | 否 | [string] |
成功示例:
{ "id": 591, "date": "2020-04-14T10:33:02", "date_gmt": "2020-04-14T02:33:02", "guid": { "rendered": "http://chenzimin.example.cn/?p=591", "raw": "http://chenzimin.example.cn/?p=591" }, "modified": "2020-04-14T10:33:02", "modified_gmt": "2020-04-14T02:33:02", "password": "", "slug": "", "status": "draft", "type": "post", "link": "http://chenzimin.example.cn/?p=591", "title": { "raw": "1212", "rendered": "1212" }, "content": { "raw": "1212", "rendered": " 1212 \n", "protected": false, "block_version": 0 }, "excerpt": { "raw": "", "rendered": " 1212 \n", "protected": false }, "author": 22, "featured_media": 0, "comment_status": "open", "ping_status": "open", "sticky": false, "template": "", "format": "standard", "meta": { "_links_to": "", "_links_to_target": "" }, "categories": [ 14 ], "tags": [], "permalink_template": "http://chenzimin.example.cn/2020/04/14/%postname%/", "generated_slug": "1212-2", "_links": { "self": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts/591" } ], "collection": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts" } ], "about": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/types/post" } ], "author": [ { "embeddable": true, "href": "http://chenzimin.example.cn/wp-json/wp/v2/users/22" } ], "replies": [ { "embeddable": true, "href": "http://chenzimin.example.cn/wp-json/wp/v2/comments?post=591" } ], "version-history": [ { "count": 0, "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts/591/revisions" } ], "wp:attachment": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/media?parent=591" } ], "wp:term": [ { "taxonomy": "category", "embeddable": true, "href": "http://chenzimin.example.cn/wp-json/wp/v2/categories?post=591" }, { "taxonomy": "post_tag", "embeddable": true, "href": "http://chenzimin.example.cn/wp-json/wp/v2/tags?post=591" } ], "wp:action-publish": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts/591" } ], "wp:action-assign-categories": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts/591" } ], "wp:action-create-tags": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts/591" } ], "wp:action-assign-tags": [ { "href": "http://chenzimin.example.cn/wp-json/wp/v2/posts/591" } ], "curies": [ { "name": "wp", "href": "https://api.w.org/{rel}", "templated": true } ] } }
4.4、编辑文章
请求协议|请求方法:HTTP|PUT
接口路径:/wp-json/wp/v2/posts/文章id
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
备注 | 所有参数同创建文章 | 否 | [string] |
4.5、删除文章
请求协议|请求方法:HTTP|DELETE
接口路径:/wp-json/wp/v2/posts/文章id
接口使用状态:正常启用
请求头部:
| 头部标签 | 必填 | 头部内容 |
| :------------ | :------------ |
|Authorization|是|Bearer+token|
请求参数
参数名 | 说明 | 必填 | 类型 | 值可能性 | 限制 | 示例 |
---|---|---|---|---|---|---|
force | 是 | [string] | false:指定文章被移动到回收站,默认,true:文章将被永久删除 |
4.6、文章点赞 *
请求协议|请求方法:HTTP|POST
接口路径:/fapi/setpostlike.php
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
postid | 文章id | 是 | [string] |
4.7、获取文章版本
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/posts/文章id/revisions
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
成功示例:
返回一个 200 OK 状态码和指定文章的版本
4.8、付费文章列表 *
请求协议|请求方法:HTTP|GET
接口路径:/fapi/getproductlist.php
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
numberposts | 1 | 是 | [string] |
post_type | product | 是 | [string] |
pagenum | 是 | [string] |
5、文章评论
5.1、获取某文章所有的评论
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/comments
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数:
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
post | 文章ID | 是 | [string] |
parent | 父级评论id,0表示父级。添加该字段则获取文章的所有一级评论 | 否 | [string] |
成功示例:
[ { "id":8, "post":601, "parent":7, "author":1, "author_name":"admin", "author_url":"", "date":"2020-04-15T09:33:14", "date_gmt":"2020-04-15T01:33:14", "content":{ "rendered":"不错不错 " }, "link":"http://chenzimin.example.cn/2020/04/14/%e6%b7%bb%e5%8a%a0%e5%88%b0%e5%a1%94%e7%bd%97%e7%89%8c%e5%88%86%e7%b1%bb/#comment-8", "status":"approved", "type":"comment", "author_avatar_urls":{ "24":"http://cn.gravatar.com/avatar/d87e1904bcb5bd85ad6882da2b2d9165?s=24&d=mm&r=g", "48":"http://cn.gravatar.com/avatar/d87e1904bcb5bd85ad6882da2b2d9165?s=24&d=mm&r=g", "96":"http://cn.gravatar.com/avatar/d87e1904bcb5bd85ad6882da2b2d9165?s=24&d=mm&r=g" }, "meta":[ ], "_links":{ "self":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/comments/8" } ], "collection":[ { "href":"http://chenzimin.example.cn/wp-json/wp/v2/comments" } ], "author":[ { "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/users/1" } ], "up":[ { "embeddable":true, "post_type":"post", "href":"http://chenzimin.example.cn/wp-json/wp/v2/posts/601" } ], "in-reply-to":[ { "embeddable":true, "href":"http://chenzimin.example.cn/wp-json/wp/v2/comments/7" } ] } }, ]
5.2、获取指定评论详情
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/wp/v2/comments/评论id
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
5.3、创建一条文章评论
注: 我记得测试这个接口的时候,一直提示权限有问题。后来因为其他事情没有继续往下处理了。哪位大神要是看到这里,帮测试一下,移动端是否可以创建评论。
请求协议|请求方法:HTTP|POST
接口路径:/wp-json/wp/v2/comments
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
请求参数
参数类型:表单
参数名 | 说明 | 必填 | 类型 |
---|---|---|---|
post | 文章id | 是 | [string] |
content | 评论内容 | 是 | [string] |
parent | 评论所属父评论id,默认为0,即一级评论 | 是 | [string] |
author_name | 评论者的名称 | 是 | [string] |
author_email | 评论者邮箱 | 是 | [string] |
author_url | 评论者url地址 | 是 | [string] |
6、媒体文件
6.1、获取所有的媒体信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/media
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
6.2、获取指定媒体ID的媒体信息
请求协议|请求方法:HTTP|GET
接口路径:/wp-json/wp/v2/media/媒体ID
请求头部:
头部标签 | 必填 | 头部内容 |
---|---|---|
Authorization | 是 | Bearer+token |
7、总结
通过接口的测试和了解,wp的主要功能包括:用户的登录注册、获取文章分类、获取文章详情、新增|修改|删除文章、评论文章、点赞文章和评论。
那么可以实现移动端资讯类app的基本应用。如果个人|团队想构建一个比较简单的资讯类项目的话,应用wp框架是一个比较不错的选择。
说到iOS端创建文章的话,那就必须要用到富文本编辑器了。于是我就顺便了解了
下iOS端富文本编辑器框架,发现这一类的却不多,个人感觉来说,比较好用的,bug比较少的就一个:WGEditor-mobile,谁有比较好的富文本编辑器框架的童鞋,欢迎留言!
参考链接: