wp rest api_WP API-使用WordPress REST API

wp rest api

WP API WordPress plugin

Before we discuss the WP REST API, it’s important to understand some terminology and background information. The acronym API stands for Application Programming Interface. An API is a programmatic way to interact with an application’s data. For example, Facebook’s API gives developers the ability to get all of the friends associated with a certain user. An API typically includes a specific set of instructions called documentation; to make it easy for any developer to work with it.

在讨论WP REST API之前,了解一些术语和背景信息很重要。 缩写API代表应用程序编程接口。 API是与应用程序数据进行交互的编程方式。 例如,Facebook的API使开发人员能够获取与特定用户关联的所有朋友。 API通常包括一组特定的指令,称为文档。 使任何开发人员都可以轻松使用它。

REST means Representational State Transfer. An API can be considered RESTful if its design/architecture subscribes to a specific set of constraints. You can look up what these constraints are here.

REST表示代表性状态转移。 如果API的设计/体系结构订阅了一组特定的约束,则可以认为该API是RESTful的。 您可以在此处查找这些约束。

HTTP requests are often the way that you interact with a RESTful API (HTTP requests are also the primary way that data is transmitted across the Internet). HTTP means Hyper Text Transfer Protocol. This protocol allows information to be shared between a client (cell phone, tablet, laptop, desktop, etc.) and a web accessible server in a request-response protocol. As an example, in order to publish a status to a user’s Facebook timeline, a HTTP request targeting that action on behalf of that user would be sent from our JavaScript code to Facebook’s API (ie. a Facebook server). The client (JavaScript code) would receive a response from the Facebook server indicating that the user’s status was successfully published.

HTTP请求通常是您与RESTful API交互的方式(HTTP请求也是通过Internet传输数据的主要方式)。 HTTP表示超文本传输​​协议。 该协议允许以请求-响应协议在客户端(手机,平板电脑,笔记本电脑,台式机等)和Web可访问服务器之间共享信息。 例如,为了将状态发布到用户的Facebook时间轴,将代表该用户的针对该操作的HTTP请求从我们JavaScript代码发送到Facebook的API(即Facebook服务器)。 客户端(JavaScript代码)将从Facebook服务器收到响应,指示用户状态已成功发布。

HTTP Request Flow

1. A client makes a HTTP request to a server and 2. The server responds with an HTTP response.

1.客户端向服务器发出HTTP请求,并且2.服务器以HTTP响应进行响应。

In a HTTP request, you need to define the type of action that you want to perform against a resource. There are four primary actions associated with any HTTP request (commonly referred to as CRUD):

在HTTP请求中,您需要定义要对资源执行的操作类型。 与任何HTTP请求(通常称为CRUD)相关联的主要动作有四个:

  1. POST (Create)

    POST(创建)
  2. GET (Retrieve)

    GET(检索)
  3. PUT (Update)

    PUT(更新)
  4. DELETE (Delete)

    删除(删除)

A resource is a data object that can be accessed via a HTTP request. The WP REST API allows you to “access your WordPress site’s data (resources) through an easy-to-use HTTP REST API”. In the case of the most recent version of the WP API (version 2), the resources include the following 9 WordPress objects:

资源是可以通过HTTP请求访问的数据对象。 WP REST API允许您“通过易于使用的HTTP REST API访问WordPress站点的数据(资源)”。 对于最新版本的WP API(版本2),资源包括以下9个WordPress对象:

  1. Posts

    帖子
  2. Pages

    页数
  3. Media

    媒体
  4. Post meta

    发布元
  5. Post revisions

    发布修订
  6. Comments

    注释
  7. Taxonomies

    分类法
  8. Terms

    条款
  9. Users

    用户数

With the WP API, you can perform any of the four CRUD actions against any of your WordPress site’s resources listed above. For example, you can use the WP API to create a post, retrieve a post, update a post or delete a post associated with your WordPress website.

使用WP API,您可以对上面列出的WordPress网站的任何资源执行四个CRUD操作中的任何一个。 例如,您可以使用WP API创建帖子,检索帖子,更新帖子或删除与WordPress网站关联的帖子。

It’s important to know that some HTTP requests don’t require authentication (anyone can make requests and the corresponding responses). In WP API, for example, certain GET requests like getting posts and getting media don’t require authentication. Other GET requests do require authentication though. For example, getting post revisions, getting users and getting post meta data require authentication. In addition, all POST, PUT and DELETE WP API actions require authentication.

重要的是要知道某些HTTP请求不需要身份验证(任何人都可以发出请求和相应的响应)。 例如,在WP API中,某些GET请求(例如获取帖子和获取媒体)不需要身份验证。 其他GET请求确实需要身份验证。 例如,获取帖子修订,获取用户和获取元数据都需要身份验证。 此外,所有POST,PUT和DELETE WP API操作都需要身份验证。

In WP API’s case, authentication for on site actions (plugins or themes installed to the site) are handled by cookies. However, if you’re off site, authentication is handled by OAuth (Y

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值