WordPress has a big market share of the web. REST APIs are a growing technique and a big opportunity for developers. Knowing how to create APIs and how to consume them gives you a great advantage. A REST API can be consumed everywhere. On mobile applications, on front-end (web apps) or any other devices that have access on the net.
WordPress在网络上占有很大的市场份额。 REST API是一种不断发展的技术,对开发人员来说是一个巨大的机会。 知道如何创建API以及如何使用它们会为您带来巨大的优势。 REST API可以在任何地方使用。 在移动应用程序,前端(Web应用程序)或可通过网络访问的任何其他设备上。
Maybe your customer already has a site that is running on WordPress and also wants a mobile application. You can create the API using custom code and relying on the database but that can cause a lot of frustration. Maybe they’re in a hurry and want the API as soon as possible. In WordPress, it is as simple as installing a plugin.
也许您的客户已经有一个在WordPress上运行的网站,并且还想要一个移动应用程序。 您可以使用自定义代码并依赖数据库来创建API,但这可能会导致很多挫败感。 也许他们很着急,希望尽快获得API。 在WordPress中,就像安装插件一样简单。
In this article, we will give a general overview of the JSON REST API plugin. We will cover how this plugin works and the basic philosophy of the REST architecture.
在本文中,我们将概述JSON REST API插件。 我们将介绍该插件的工作原理以及REST体系结构的基本原理。
Grab the latest version of JSON REST API on WordPress plugin directory. Install and activate it. Now you have a REST API ready to use. You need a REST Client to test and explore it. There are many tools for that job. Don’t limit yourself to just these four tools. There are many other REST Clients.
在WordPress插件目录上获取最新版本的JSON REST API 。 安装并激活它。 现在,您已经可以使用REST API。 您需要一个REST Client进行测试和探索。 有很多工具可以完成这项工作。 不要只局限于这四个工具。 还有许多其他的REST客户端。
The first one is cURL. It is a command line tool that can send different requests to the given endpoint. It is not only REST API related but it is a general HTTP Request tool. Many test examples on other articles are made using this tool. It’s the universal HTTP tool that can be translated to any programming language.
第一个是cURL 。 它是一个命令行工具,可以将不同的请求发送到给定的端点。 它不仅与REST API相关,而且是通用的HTTP请求工具。 使用此工具可以制作许多其他文章的测试示例。 它是通用的HTTP工具,可以转换为任何编程语言。
One of the tools I usually use is the PHPStorm REST Client. As I mainly work with this IDE when developing, it is easier for me using a tool that I can access faster. Many other IDE-s come with REST Client integrated to make REST API developing easier and faster.
我通常使用的工具之一是PHPStorm REST Client。 由于我在开发时主要使用此IDE,因此使用可以更快访问的工具对我来说更容易。 REST Client集成了许多其他IDE,从而使REST API的开发变得更加轻松和快捷。
The third one and the one that I highly recommend is Postman. It is the easiest and more intuitive REST client I have ever worked with. Unfortunately, is only available as a Chrome extension. If you are using Firefox there is a nice extension called RESTClient.
我强烈推荐的第三个是Postman 。 这是我曾经使用过的最简单,更直观的REST客户端。 不幸的是,只能作为Chrome扩展程序使用。 如果您使用的是Firefox,则有一个不错的扩展名为RESTClient 。
The first thing you have to do is to check if this plugin works, where is its endpoint, the base URL that holds all the information about the schema, all the Resources (Collections) and the routes. The plugin adds a new field called “`Link`” on the header so check for that in the header of the Response. If you use Postman or any other GUI clients just inspect the header. If you use cURL execute this command to your homepage:
您要做的第一件事是检查此插件是否正常工作,其端点在哪里,包含有关架构,所有资源(集合)和路由的所有信息的基本URL。 该插件在标题上添加了一个名为“ Link”的新字段,因此请在Response的标题中进行检查。 如果您使用Postman或任何其他GUI客户端,则只需检查标题即可。 如果使用cURL,请在首页上执行以下命令:
curl -I http://yoursite.com/The header option grabs only the header. In my case the endpoint is local.wordpress.dev/wp-json. If you left the permalink to it’s default value on the settings, then you may have the link value something like this: http://local.wordpress.dev/?json_route=/.
标头选项仅获取标头。 在我的情况下,端点是local.wordpress.dev/wp-json 。 如果将永久链接保留为设置的默认值,则链接值可能类似于以下内容: http://local.wordpress.dev/?json_route=/ : http://local.wordpress.dev/?json_route=/ 。
It changes only the rewrite rules but not any other essential changes. Everything is the same in the two cases.
它仅更改重写规则,而不更改其他任何必要的更改。 在这两种情况下,一切都是相同的。
The index endpoint (or API endpoint) is the starting point. You can explore the whole API from there. It gives you all the information about the API. It gives information about the Collections, Entities and how they are mapped to routes. Make a GET request to your endpoint and see what it returns. On REST Clients it’s easy because you are dealing with GUI interface. Let’s see how is done using cURL.
index endpoint (或API端点)是起点。 您可以从那里探索整个API。 它为您提供有关API的所有信息。 它提供有关集合,实体以及它们如何映射到路线的信息。 向端点发出GET请求,然后查看返回的内容。 在REST客户端上,这很容易,因为您正在处理GUI界面。 让我们看看如何使用cURL。
curl http://local.wordpress.dev/wp-json/In my case it returns a big JSON file that looks like this.
在我的情况下,它返回一个看起来像这样的大JSON文件。
{ "name": "Local WordPress Dev", "description": "Just another WordPress site", "URL": "http://local.wordpress.dev", "routes": { "/": { "supports": [ "HEAD", "GET" ], "meta": { "self": "http://local.wordpress.dev/wp-json/" } }, "/posts": { "supports": [ "HEAD", "GET", "POST" ], "meta": { "self": "http://local.wordpress.dev/wp-json/posts" }, "accepts_json": true }, "/posts/<id>": { "supports": [ "HEAD", "GET", "POST", "PUT", "PATCH", "DELETE" ], "accepts_json": true }, "/posts/<id>/revisions": { "supports": [ "HEAD", "GET" ] } }, "authentication": [], "meta": { "links": { "help": "https://github.com/WP-API/WP-API", "profile": "https://raw.github.com/WP-API/WP-API/master/docs/schema.json" } } }Actually the response is too long so I removed most of it. The index route (endpoint) gives gives information about the title of the site, the description and it’s URL. All the Routes that you can find on the API are also mapped in the ‘routes’ field. posts, users, media, pages are Collections. Collections are a group of Entities. An Entity is a single post or a single page with its ID.
实际上,响应时间太长,因此我删除了大部分响应。 索引路由(端点)提供有关站点标题,描述及其URL的信息。 您可以在API上找到的所有路由也都映射在“路由”字段中。 posts , users , media , pages是Collections。 集合是一组实体。 实体是具有其ID的单个帖子或单个页面。
Let’s say we want to see the latest posts. The endpoint for the Collection is api_endpoint/posts. This returns the latest posts. The Entity is api_endpoint/posts/ID with the given ID as an argument. Using cURL, the command would be:
假设我们要查看最新帖子。 集合的端点是api_endpoint/posts 。 这将返回最新帖子。 实体是api_endpoint/posts/ID ,其中给定的ID作为参数。 使用cURL,命令将是:
curl http://local.wordpress.dev/wp-json/postsAnd:
和:
curl http://local.wordpress.dev/wp-json/1Retrieving data is a good thing but there are times when we want to post data.
检索数据是一件好事,但是有时候我们想要发布数据。
I want to share some problems that I got when tried to post or delete data. First, you need to authenticate yourself. There are three ways you can authenticate.
我想分享一些尝试发布或删除数据时遇到的问题。 首先,您需要验证自己的身份。 您可以通过三种方式进行身份验证。
The first one is using the cookies. This is one method not widely used when working with APIs. When a user navigates on the web, pages tend to store data on users side. The cookie method here is a bit different by using the nonces method.
第一个是使用cookie。 这是使用API时未广泛使用的一种方法。 当用户在Web上浏览时,页面倾向于在用户端存储数据。 这里的cookie方法与使用nonces方法有所不同。
The second one is the OAuth method. This plugin implements the version 1.0a of OAuth. This method is widely used. Google, Facebook, Twitter and Flickr use OAuth for third party authentication. OAuth is in version two but they have chosen to use version 1.0a. There is a big debate for the version two. Some big companies didn’t shift in version two for different reasons but mainly concerned about security.
第二个是OAuth方法。 该插件实现OAuth的1.0a版本。 该方法被广泛使用。 Google,Facebook,Twitter和Flickr使用OAuth进行第三方身份验证。 OAuth是第二版,但他们选择使用1.0a版。 对于第二版,存在很大的争议。 一些大公司出于不同原因没有在第二版中转移,而是主要关注安全性。
The third one is the Basic Authentication. Using this method you have to send your username and password each time you make a request. Client tools for REST testing usually have this method implemented themselves. This method is mainly used when developing. Is rarely used on production. On production consider using OAuth.
第三个是基本身份验证。 使用这种方法,您每次发出请求时都必须发送用户名和密码。 用于REST测试的客户端工具通常自行实现此方法。 该方法主要在开发时使用。 很少用于生产。 在生产中,请考虑使用OAuth。
Unfortunately, the Basic Authentication plugin is not updated to often. They concentrated all their efforts on the OAuth plugin. This plugin can’t be found on wordpress.org but only on it’s GitHub repository. You have to manually install it. On the plugin directory execute this command:
不幸的是,基本身份验证插件不会经常更新。 他们将所有精力都集中在OAuth插件上。 无法在wordpress.org上找到此插件,而只能在GitHub存储库上找到。 您必须手动安装它。 在插件目录上,执行以下命令:
git clone https://github.com/WP-API/Basic-Auth basicAuthAlternatively visit the GitHub page and download the plugin and install it manually via FTP.
或者访问GitHub页面并下载插件,然后通过FTP手动安装。
Then go to the dashboard and activate it. This plugin has some problems with non-Apache servers. As I mainly use WordPress VVV which is a Vagrant installation, it has problems because VVV comes with nginx. Change the permalink configuration if yours is default. That worked for me.
然后转到仪表板并激活它。 该插件在非Apache服务器上存在一些问题。 由于我主要使用的是Vagrant安装的WordPress VVV ,因此它有问题,因为VVV附带了nginx。 如果您的默认链接是永久链接,请更改其配置。 那对我有用。
Now that we have the authentication set up is time to create some posts. Postman has the basic authentication implemented. Insert the username and password if you use Postman and it will remember base 64 encoding each time it makes a request. To see if it works make a request in ‘index_endpoint/users/me’. It returns the information about you.
现在我们已经建立了身份验证,是时候创建一些帖子了。 邮递员已实现基本身份验证。 如果使用Postman,请输入用户名和密码,每次发出请求时,它都会记住base 64编码。 要查看其是否有效,请在“ index_endpoint / users / me”中进行请求。 它返回有关您的信息。
If you try to access this URL without using basic auth or any other authentication methods, the API will return:
如果您尝试在不使用基本身份验证或任何其他身份验证方法的情况下访问此URL,则API将返回:
[ { "code": "json_not_logged_in", "message": "You are not currently logged in." } ]Let’s try to make a request via cURL and also include the Basic Auth method:
让我们尝试通过cURL进行请求,并包括Basic Auth方法:
curl --user admin:password http://local.wordpress.dev/wp-json/users/meThe --user option makes the admin:password argument which is basically just 64 encoding of that plain text. cURL builds itself the Request headers for the Basic Auth by adding an additional field. This is what it adds in my case:
--user选项产生admin:password参数,该参数基本上只是该纯文本的64位编码。 cURL通过添加其他字段来为“基本身份验证”构建自己的“请求”标头。 这就是我的情况:
Authorization: Basic YWRtaW46cGFzc3dvcmQ=This is what is returned when I try to access my profile using the given credentials.
这是当我尝试使用给定的凭据访问我的个人资料时返回的内容。
{ "ID": 1, "username": "admin", "name": "admin", "first_name": "", "last_name": "", "nickname": "admin", "slug": "admin", "URL": "", "avatar": "https://gravatar.com/avatar/06e92fdf4a9a63441dff65945114b47f?s=96", "description": "", "registered": "2014-07-17T22:59:59+00:00", "roles": [ "administrator" ], "capabilities": { "switch_themes": true, "edit_themes": true, "activate_plugins": true, "edit_plugins": true, "edit_users": true, "edit_files": true, "manage_options": true, "moderate_comments": true, "manage_categories": true, "manage_links": true, "upload_files": true, "import": true, "administrator": true }, "email": false, "meta": { "links": { "self": "http://local.wordpress.dev/wp-json/users/1", "archives": "http://local.wordpress.dev/wp-json/users/1/posts" } } }Let’s try to delete one post. To delete one post we must know it’s URL. It’s URL is api_endpoint/posts/ID. Replace the ID with 1 or any other post ID that you want. Don’t forget that you should use authentication to delete something.
让我们尝试删除一个帖子。 要删除一篇文章,我们必须知道它的URL。 它的网址是api_endpoint/posts/ID 。 将ID替换为1或所需的任何其他帖子ID。 不要忘记,您应该使用身份验证来删除某些内容。
curl --user admin:password -X DELETE http://local.wordpress.dev/wp-json/posts/4If using Postman or similar send the URL above (http://local.wordpress.dev/wp-json/posts/4) and select the ‘Delete’ request method from the dropdown list next to the URL field.
如果使用Postman或类似工具,请发送上方的URL(http://local.wordpress.dev/wp-json/posts/4),然后从URL字段旁边的下拉列表中选择“删除”请求方法。
And this is what I get back:
这就是我得到的:
{ "message":"Deleted post" }Posting a new record (post for example) is easy too. Create a new JSON file with two fields. One for the title and one for the content.
发布新记录(例如发布)也很容易。 使用两个字段创建一个新的JSON文件。 一是标题,一是内容。
{ "title": "This is a post", "content_raw": "This is some content" }Send that new data to the Collection of that Entity that we want to create. Remember that when posting new Entity, you should post it in the Collection endpoint. Here we used Basic Auth again. -X POST makes this request a POST Request. --data captures a file that is in that folder and sends it as raw data.
将新数据发送到我们要创建的实体的集合。 请记住,发布新实体时,应将其发布在Collection端点中。 在这里,我们再次使用了基本身份验证。 -X POST使此请求成为POST请求。 --data捕获该文件夹中的文件,并将其作为原始数据发送。
curl --user admin:password -X POST http://local.wordpress.dev/wp-json/posts --data @data.jsonDon’t limit yourself. WordPress REST API team has also some other tools that are tightly integrated with this plugin. They have a Client Cli, Client JS and a Client PHP to work with your API right out of the box. Also head over their documentation for deeper technical information.
不要限制自己。 WordPress REST API团队还有其他一些与此插件紧密集成的工具。 他们有一个Client Cli , Client JS和Client PHP可以直接使用您的API。 还可以查阅其文档,以获取更深入的技术信息。
Knowing how to use this plugin is one thing but the best advice I can give in this case is: ‘don’t limit yourself’. There is so much information out there about REST. Here on SitePoint we have a nice series about building a REST API from scratch by Vito Tardia.
知道如何使用该插件是一回事,但是在这种情况下,我可以提供的最佳建议是:“不要限制自己”。 关于REST的信息太多了。 在SitePoint上,我们有一个很好的系列,内容涉及Vito Tardia从头开始构建REST API 。
Creating a REST API on WordPress is easy. You don’t have to create one from scratch if you’re using WordPress. As a matter of fact, the index end-point tells you everything about this plugin and how to use it. We covered Collections, Entities and how to manipulate some data using different verbs (GET, POST, PUT, DELETE).
在WordPress上创建REST API很容易。 如果您使用WordPress,则无需从头开始创建一个。 实际上,索引端点会告诉您有关此插件的所有信息以及如何使用它。 我们介绍了集合,实体以及如何使用不同的动词(GET,POST,PUT,DELETE)操纵某些数据。
An important aspect when developing is also authentication so we covered this as well. Consider OAuth 1.0a when developing for production. You can also use Basic Auth but OAuth is already tested and backed by a number of big companies.
开发时的一个重要方面也是身份验证,因此我们也介绍了这一点。 在进行生产开发时,请考虑使用OAuth 1.0a。 您也可以使用基本身份验证,但是OAuth已通过许多大公司的测试和支持。
What do you think about REST APIs on WordPress? Does it compete with the hand crafted APIs? Do you know any other WordPress plugins about REST? Please let us know in the comments below.
您如何看待WordPress上的REST API? 它可以与手工制作的API竞争吗? 您是否了解有关REST的其他WordPress插件? 请在下面的评论中告诉我们。
翻译自: https://www.sitepoint.com/wordpress-json-rest-api/
相关资源:jdk-8u281-windows-x64.exe