HTTP API

HTTP API

出自WordPress中文文档

目录

HTTP API

Within PHP, there are five different ways to send a HTTP request. For simplicity, the five different ways are called 'transports' and will be used from now on. The purpose for the HTTP API is to support all of them with an API that is simple and standard for each of the transports.

The problem is that web hosting servers support different transports and some might support none. The solution then is to support as many as possible to allow for those who have hosts that disable one or two to still access the web through one of the other transports that might still be enabled or supported.

The other problem was that there wasn't any API for plugins and WordPress core to standardize on. Therefore, there used to be several different implementations in WordPress and many still in Plugins. The problem is even worse for plugins, because the author had to write the implementation themselves and support it afterwards. Given how many workarounds were required for the HTTP API in WordPress, that would be extremely difficult to support. It would also had to be reimplemented in each plugin that wanted to support as many as possible, which many just choose to support one or two if the plugin author was feeling generous.

The HTTP API was also an attempt to standardize on a single API that handled everything as simply as possible. The actual implementation is object-oriented, but there are utility functions, which can be used to abstract the API usage.

The HTTP API was added in WordPress 2.7 and extend further in WordPress 2.8. You may want to maintain backwards compatibility with previous versions, so you should wrap the function calls in function_exists() and provide an alternative, if possible.

In WordPress 2.7, the basics of header, body and response support was started. In the next version, WordPress 2.8, compression, cookie support and proxy support were added. Some of the features are passive, meaning that you don't have to set any option or do anything in order to use the feature. If you use an option that is in a later version, it won't give you an error, but may not work correctly.

While most of the features can be set using the options or using filters, the proxy implementation relies on constants and thus will need to be set manually in the wp-config.php. Of course, there could be and might already be a plugin that will allow you to configure it in the WordPress administration panels.

Helper Functions

The helper functions utilize the HTTP API classes to simplify the process as much as possible. You could use the classes and could use some of the methods to help process the code. The classes assume that you know what you are doing and can use the classes.

You must use the helper functions if you are modifying the core code of WordPress. It is one of the reasons for the helper API, to reduce the amount of error checking and prevent having to edit multiple places when bugs are found.

The functions below are the ones you will use to retrieve an URL. Please be aware that these functions will return a WordPress WP_Error class on failure. You will have to check for that after using these functions.

* wp_remote_get() - Retrieves a URL using the GET HTTP method.
* wp_remote_post() - Retrieves a URL using the POST HTTP method.
* wp_remote_head() - Retrieves a URL using the HEAD HTTP method.
* wp_remote_request() - Retrieves a URL using either the default GET or a custom HTTP method (should be caps) that you specify.

The other helper functions deal with retrieving different parts of the response and does the testing for WP_Error for you. It should be noted that using these functions adds a small amount of overhead (1-2 milliseconds), because of the function calls and checks made in the functions. If speed is important, then learn the array structure and access the array instead.

* wp_remote_retrieve_body() - Retrieves just the body from the response.
* wp_remote_retrieve_header() - Gives you a single HTTP header based on name from the response.
* wp_remote_retrieve_headers() - Returns all of the HTTP headers in an array for processing.
* wp_remote_retrieve_response_code() - Gives you the number for the HTTP response. This should be 200, but could be 4xx or even 3xx on failure.
* wp_remote_retrieve_response_message() - Returns the response message based on the response code.

You should first check that the response is not a !WordPress error.

pre language=php $response = wp_remote_get('http://wordpress.org');

if( is_wp_error( $response ) )

   // Handle error here.

/pre

As you can see in the example below, the API also handles redirection.

pre language=php $theBody = wp_remote_retrieve_body( wp_remote_get('http://www.wordpress.org') ); /pre

Scraping Google

pre language=php $response = wp_remote_get('http://www.google.com'); /pre

If you do this enough, you won't be able to after a while.

Sending Headers

Sending headers is simple enough. Meh, I'm not going to tell you now.

Sending and Receiving Cookies

This is a Version 2.8 feature and it currently isn't out yet. It burns us to write this section so soon. It would be cool though.

Other Arguments

The argument 'method' is the HTTP method, such as POST, GET, HEAD, PUT, etc that you want to use. The safest methods to use for server support are POST, GET, and HEAD, all others, you may meet with various degrees of success. The default for this value is 'GET'.

The argument 'timeout' allows for setting the time in seconds, before the connection is dropped and an error is returned. The default for this value is 5 seconds and it also has a filter named, 'http_request_timeout', in case you want to write a plugin that sets it for every request.

The 'redirection' argument is the amount of times to follow a redirect before giving up. The default is 5 and there is a filter for the value. The filter name is 'http_request_redirection_count' and only passes the redirection default value.

The 'user-agent' argument allows you to set the user-agent. The default of which is, WordPress/2.7; http://www.example.com where 2.7 is the WordPress version number and www.example.com is the blog URL. There is a filter for changing this value as well, which is 'http_headers_useragent'.

I forget what the 'blocking' argument does. Something something, won't pass any response back other than an empty one. This isn't true in all cases. Other cases, it won't block PHP and will allow PHP to continue execution while the transport is working (this is also not true in all cases either, because PHP is not multi-threaded, it is confusing, I know). The key is that when you set blocking to false, then it will just send the request and won't bother you with the details. This is useful for sending a POST request, where you aren't concerned with whether it succeeded or not.

The argument 'compress', available with Version 2.8, allows you to send the request body as compressed. Well, actually, the argument was added, it currently isn't being used as the functionality has been completed. Look for it in 2.9 to do something.

The 'decompress' argument does do something and by default, will decompress any compressed content that comes in. By default, it should not do this, unless you explicitly tell the server that you can accept compressed data, this hasn't always been the case with some servers, as they don't follow the spec (see diatribe about chunk-encoding and HTTP 1.0). If you do leave this as true, then the headers will be set to tell the server that compressed data is accepted and it will be decompressed in the response. If you sent it to false, then the header will be sent that tells servers that compressed content is not accepted. If the content is still compressed, because of incorrect implementation or you sent the headers for it, then you will need to decompressed the content. This is also a 2.8 argument.

The 'sslverify' argument was added in Version 2.8 is something you'll probably not be concerned with. Move along. Still interested? It has to do with whether it will check to see if the SSL certificate is valid (not self-signed, actually for the site in the request) and will deny the response, if it isn't. You will only be concerned with this, if you are requesting HTTPS and and know that the site is self-signed or is invalided and are reasonably sure that it can be trusted, if so, then set to false.

External References

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Delphi HTTP API是Delphi编程语言中的一个库,用于访问和操作HTTP协议的应用程序编程接口(API)。 使用Delphi HTTP API,我们可以方便地通过HTTP协议与远程服务器进行通信。它提供了一组功能强大的类和方法,使我们能够发送HTTP请求,并获取服务器的响应。 通过Delphi HTTP API,我们可以执行各种HTTP操作,如发送GET请求以获取远程服务器上的数据,发送POST请求以向服务器提交数据,发送PUT和DELETE请求来更新和删除资源等。我们还可以设置请求头、添加查询字符串参数、设置身份验证等。 使用Delphi HTTP API,我们可以轻松处理各种HTTP响应,包括读取响应的HTTP状态码、获取响应的内容、处理响应的头信息等。它还提供了处理重定向、处理Cookie等功能,以便更好地进行HTTP交互。 Delphi HTTP API还支持使用SSL和HTTPS进行安全的HTTP通信。我们可以使用SSL/TLS协议对传输的数据进行加密,确保通信的安全性。 总之,Delphi HTTP API为Delphi开发人员提供了一个方便、强大和灵活的工具,用于与远程服务器进行HTTP通信。无论是在开发Web应用程序还是与网络服务进行交互,Delphi HTTP API都可以帮助我们轻松地实现所需的功能。 ### 回答2: Delphi是一种编程语言和集成开发环境(IDE),其HTTP API是一种用于与网络服务器进行通信和数据交换的应用程序接口(API)。 Delphi提供了丰富的库和组件,使开发者能够使用HTTP协议进行网络通信。Delphi的HTTP API是基于HTTP协议的实现,开发者可以使用它来创建客户端应用程序,通过发送HTTP请求与服务器进行交互。 Delphi的HTTP API提供了多种功能,包括发送GET请求来获取远程服务器上的数据,发送POST请求来向服务器提交数据,以及其他HTTP方法(如PUT和DELETE)来执行其他操作。通过使用HTTP API,开发者可以轻松地与Web服务器进行通信,包括获取和提交数据、访问Web服务和执行其他与HTTP相关的任务。 Delphi的HTTP API还提供了处理HTTP响应的功能,开发者可以检查响应状态码,解析响应头和内容,并根据需要进行适当的处理。此外,Delphi的HTTP API还支持处理HTTP重定向、处理Cookie和认证等常见的Web开发需求。 总之,Delphi的HTTP API为开发者提供了一种简单而强大的方式来与网络服务器进行通信和数据交换。无论是开发Web应用程序还是利用Web服务开发客户端应用程序,Delphi的HTTP API都是一个有用的工具。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值