今天无意间发现一个有意思的插件,可以用Flutter来获取WordPress的接口。WordPress是什么?
WordPress是一个以PHP和MySQL为平台的自由开源的博客软件和内容管理系统。WordPress具有插件架构和模板系统。截至2018年4月,排名前1000万的网站中超过30.6%使用WordPress。WordPress是最受欢迎的网站内容管理系统。WordPress是当前因特网上最流行的博客系统。
一般WordPress的后台都是用php java 等这些语言来获取数据,这次Flutter也插入进来了,所以Flutter不止Android,iOS 实现语言,它要做的事情还有很多很多,前方的道路还有很远,战斗从来没有停止过,只要你感想,它就敢做,加油。
本头条核心宗旨
欢迎来到「技术刚刚好」作者,「技术刚刚好」是个人维护,每天至少更新一篇Flutter技术文章,实时为大家播报Flutter最新消息。如果你刚好也在关注Flutter这门技术,那就跟我一起学习进步吧,你的赞,收藏,转发是对我个人最大的支持,维护不易,欢迎关注。
技术刚刚好经历
近几年,移动端跨平台开发技术层出不穷,从Facebook家的ReactNative,到阿里家WEEX,前端技术在移动端跨平台开发中大展身手,技术刚刚好作为一名Android开发,经历了从Reactjs到Vuejs的不断学习。而在2018年,我们的主角变成了Flutter,这是Goolge开源的一个移动端跨平台解决方案,可以快速开发精美的移动App。希望跟大家一起学习,一起进步!
本文核心要点
今天就把这个插件分享给大家,就当是一个练手学习的项目,如果大家感兴趣可以进去看看。
插件地址:https://pub.dev/packages/flutter_wordpress
接入方式:
Flutter Wordpress #
该库使用WordPress REST API V2为您的应用程序提供了一种与WordPress网站进行交互的方式。
要求为了进行身份验证和使用管理员级别的REST API,您需要使用WordPress网站中两个流行的身份验证插件之一:
- 应用密码
- WP REST API的JWT身份验证 (推荐)
1.导入库号
https://pub.dartlang.org/packages/flutter_wordpress
import 'package:flutter_wordpress/flutter_wordpress.dart' as wp;
2.实例化WordPress类
wp.WordPress wordPress;
// adminName and adminKey is needed only for admin level APIs
wordPress = wp.WordPress(
baseUrl: 'http://localhost',
authenticator: wp.WordPressAuthenticator.JWT,
adminName: '',
adminKey: '',
);
3.验证用户编号
Future<wp.user> response = wordPress.authenticateUser(
username: 'ChiefEditor',
password: 'chiefeditor@123',
);
response.then((user) {
createPost(user);
}).catchError((err) {
print('Failed to fetch user: $err');
});
/<wp.user>
4.获取帖子
Future<list>> posts = wordPress.fetchPosts(
params: wp.ParamsPostList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 20,
order: wp.Order.desc,
orderBy: wp.PostsOrderBy.date,
),
fetchAuthor: true,
fetchFeaturedMedia: true,
fetchComments: true,
);
/<list>
5.获取用户
Future<list>> users = wordPress.fetchUsers(
params: wp.ParamsUserList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
order: wp.Order.asc,
orderBy: wp.UsersOrderBy.name,
role: wp.UserRole.subscriber,
),
);
/<list>
6.获取评论
Future<list>> comments = wordPress.fetchComments(
params: wp.ParamsCommentList(
context: wp.WordPressContext.view,
pageNum: 1,
perPage: 30,
includePostIDs: [1],
),
);
/<list>
7.创建帖子编号
void createPost(wp.User user) {
final post = wordPress.createPost(
post: new wp.Post(
title: 'First post as a Chief Editor',
content: 'Blah! blah! blah!',
excerpt: 'Discussion about blah!',
author: user.id,
commentStatus: wp.PostCommentStatus.open,
pingStatus: wp.PostPingStatus.closed,
status: wp.PostPageStatus.publish,
format: wp.PostFormat.standard,
sticky: true,
),
);
post.then((p) {
print('Post created successfully with ID: ${p.id}');
postComment(user, p);
}).catchError((err) {
print('Failed to create post: $err');
});
}
8.发表评论
void postComment(wp.User user, wp.Post post) {
final comment = wordPress.createComment(
comment: new wp.Comment(
author: user.id,
post: post.id,
content: "First!",
parent: 0,
),
);
comment.then((c) {
print('Comment successfully posted with ID: ${c.id}');
}).catchError((err) {
print('Failed to comment: $err');
});
}
总结
还是那句话,程序员不只是敲代码,还要天天学习,不管你是20岁,30岁,40岁,只要是程序员你就得不停学习,不停前进。
谢谢观看技术刚刚好的文章,技术刚刚好是个人维护,每天至少更新一篇Flutter技术文章,实时为大家播报Flutter最新消息。如果你刚好也在关注Flutter这门技术,那就跟我一起学习进步吧,你的赞,收藏,转发是对我个人最大的支持,维护不易,欢迎关注。
閱讀更多 技術剛剛好 的文章