本文目录
[隐藏]
- 1But First, Why a Summary?
- 2Remote Requests
- 3wp_remote_get
- 3.1How Does This Work?
- 3.2What’s Being Returned?
- 4wp_remote_post
- 4.1How Does This Work?
- 4.2What’s Being Returned?
- 5Conclusion
本文是《WordPress HTTP API 指南》系列教程的第 8 部分,该系列共包含以下 8 个部分:
- WordPress HTTP API 指南:wp_remote_get 概述
- WordPress HTTP API 指南:wp_remote_get 实例
- WordPress HTTP API 指南:wp_remote_get 响应
- WordPress HTTP API 指南:wp_remote_get 参数
- WordPress HTTP API 指南:wp_remote_post 概述
- WordPress HTTP API 指南:wp_remote_post 实例
- WordPress HTTP API 指南:从 wp_remote_post 保存数据
- WordPress HTTP API 指南:回顾
撰写一个有关API,或API的一部分的系列教程,所面临的其中一个挑战,是不花费太多时间很难覆盖所述API的各个方面,同时尽量不只是简单带过API的皮毛,而没有给予足够的实用信息。
案例分析:纵观过去的系列中,我们已经讲解了 WordPress HTTP API。具体而言,我们已经介绍了wp_remote_get和wp_remote_post,并已经使用这两个函数做了比较广泛的工作,包括创建了实例。
注:由于时间精力有限,本教程没办法翻译分享,希望朋友们可以加入我们,帮助我们进行翻译,有小酬谢,有意者请联系倡萌QQ 745722006(注明:教程翻译)。
以下为原文:http://code.tutsplus.com/tutorials/a-look-at-the-wordpress-http-api-a-review–wp-32531
One of the challenges that comes with writing a series about an API – or even part of an API – is that it’s hard to cover every aspect of said API without spending too much time diving deep into one part and simultaneously trying not to simply skim across the top of each API without giving enough practical information.
Case in point: Throughout the last series, we’ve been taking a look at the WordPress HTTP API. Specifically, we’ve covered wp_remote_get
and wp_remote_post
, and we’ve done some relatively extensive work with both functions including building example projects.
The thing is, there’s still a lot of ground that could be covered in the WordPress HTTP API. In the future, we may do an advanced series on more aspects of the API, but for now, let’s review everything we’ve covered in this series.
But First, Why a Summary?
Writing a relatively lengthy series about a couple of functions can cover a lot of ground. The problem with doing this is that if at any time in the future you need to refer to one part, you may not recall exactly where the information was located.
Or, perhaps worse, you may have to trudge through a significant amount of information in order to find the one aspect that you need to keep making progress on your work.
And sure, you can always refer to the series index, but in order to give a “quick guide” of sorts, I thought it may be useful to summarize the articles, the functions, and high-level notes regarding the segment of the API that we covered just in case you need a reference for your work.
Of course, note that you can always view the series in its entirety on the series listing page.
Remote Requests
Before we review each of the functions, remember that a remote request can be defined as the process by which one server makes a request to another server.
Generally speaking, one server may simply send data to the other server which will then do something with it (be it save the data, process the data, and so on), and it may optionally send a response back.
At a high-level, that’s a remote request. For more information about this particular idea, be sure to check out this post.
wp_remote_get
wp_remote_get
is a function that’s a part of the WordPress HTTP API that’s responsible for making GET
requests.
The function accepts:
- A URL to which the request is being made
- The array of arguments to send along with the request
If you’re primarily responsible for retrieving information from the server, then this is the function that you will want to use.
Secondly, if you need more than a URL or more control over the request that’s being sent, then you can review this article to look at all of the arguments that it accepts.
How Does This Work?
Next in the series, we built an actual plugin that would leverage wp_remote_get
in order for us to retrieve the number of followers for a given Twitter account, as well as the last tweet sent from said Twitter account.
The primary purpose of this article and this demo was to give a practical example of how to usewp_remote_get
in a “real world” setting. For the full source code for the working demo, be sure to review the associated article.
What’s Being Returned?
Because wp_remote_get
is focused on retrieving information, it only makes sense that we’d expect a response, right? In the final article covering wp_remote_get
, we looked at what exactly is returned from the server and how WordPress formats it for our use.
If, during the course of your work, you have a difficult time deciphering exactly what it is that is coming back from the server (or why it’s not working as expected), then this is the article you should review.
wp_remote_post
Just as wp_remote_get
is responsible for making GET
requests, wp_remote_post
is responsible for making POST
requests.
Just as with the wp_remote_get
, wp_remote_post
accepts the same arguments:
- The URL to which the request is being made
- An array of arguments that help tailor the request to the server
But there’s a fundamental difference in the purpose of this function and the prior one that’s discussed. The difference is what happens when the request is completed.
Just as wp_remote_get
is primarily used to retrieve data, wp_remote_post
is used to send data across the wire to be processed – a response may never be sent back.
For the initial survey of this particular function – what it accepts including the advanced list of arguments – review this article.
How Does This Work?
Just as we did with wp_remote_get
, we created a plugin to demonstrate how wp_remote_post
works within the larger context of a WordPress theme.
Though the plugin is on GitHub for reference, we walk through the entire first version of the plugin in the following article. Specifically, we cover how to make the request to a script responsible for receiving $_POST
data and then how it can format and return a response to the caller.
What’s Being Returned?
In the final article in the series, we completed the plugin by using LESS to give the plugin a slightly nicer look and feel, and we rounded out the plugin so that it actually saves some of the response data to the database just to give an idea as to how this can be achieved.
Conclusion
Summary posts are new territory – for me, at least – as I’ve historically let my series’ stand on their own, but I thought that this would be a nice reference to provide considering we covered so much ground in the series.
To reiterate:
- Here is the full listing of articles in the series
- This is a link to the
wp_remote_get
source code - Here is the
wp_remote_post
project on GitHub
阅读该系列的其他文章: 上一篇:WordPress HTTP API 指南:从 wp_remote_post 保存数据