當前位置:首頁>WordPress建站>WordPress開發(fā)>WordPress HTTP API 指南:回顧

WordPress HTTP API 指南:回顧

撰寫一個有關(guān)API,或API的一部分的系列教程,所面臨的其中一個挑戰(zhàn),是不花費太多時間很難覆蓋所述API的各個方面,同時盡量不只是簡單帶過API的皮毛,而沒有給予足夠的實用信息。

案例分析:縱觀過去的系列中,我們已經(jīng)講解了 WordPress HTTP API。具體而言,我們已經(jīng)介紹了wp_remote_get和wp_remote_post,并已經(jīng)使用這兩個函數(shù)做了比較廣泛的工作,包括創(chuàng)建了實例。

注:由于時間精力有限,本教程沒辦法翻譯分享,希望朋友們可以加入我們,幫助我們進行翻譯,有小酬謝,有意者請聯(lián)系倡萌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 GETrequests.

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 POSTrequests.

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 $_POSTdata 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:

With that said, let me know if you prefer summary posts or not. As I mentioned, this is something that I don’t typically do, but if it helps provide a point of reference for you guys, then I’m happy to continue doing them for future series’.

聲明:本站所有文章,如無特殊說明或標注,均為本站原創(chuàng)發(fā)布。任何個人或組織,在未征得本站同意時,禁止復制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書籍等各類媒體平臺。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進行處理。

給TA打賞
共{{data.count}}人
人已打賞
歡迎關(guān)注WordPress大學公眾號 WPDAXUE
WordPress開發(fā)

WordPress HTTP API 指南:從 wp_remote_post 保存數(shù)據(jù)

2016-6-19 11:05:29

WordPress開發(fā)

如何為 WordPress 主題和插件添加管理通知(Admin Notices)

2016-6-26 16:37:18

0 條回復 A文章作者 M管理員
    暫無討論,說說你的看法吧
?
個人中心
購物車
優(yōu)惠劵
今日簽到
有新私信 私信列表
搜索

孟村| 西贡区| 中牟县| 五华县| 合山市| 高淳县| 时尚| 察哈| 中山市| 唐山市| 永清县| 扎鲁特旗| 桐庐县| 嘉峪关市| 沿河| 惠州市| 宁远县| 房产| 泸溪县| 同仁县| 南阳市| 农安县| 潞城市| 建宁县| 赤水市| 凤台县| 崇信县| 陈巴尔虎旗| 托克逊县| 宁城县| 化隆| 博白县| 和静县| 辛集市| 阳山县| 登封市| 姚安县| 盐亭县| 安新县| 宁乡县| 琼中|