wp_remote_request()
云策文档标注
概述
wp_remote_request() 是 WordPress 中用于执行 HTTP 请求并返回响应的核心函数。它支持自定义 HTTP 方法,如 GET、POST、HEAD 等,并可通过参数数组配置请求细节。开发者应优先使用更安全的 wp_safe_remote_request() 处理用户可控的 URL。
关键要点
- 函数执行 HTTP 请求,返回响应数组或 WP_Error 对象。
- 参数包括必需的 URL 字符串和可选的请求参数数组,后者参考 WP_Http::request()。
- 存在抽象 HTTP 方法的便捷函数:wp_remote_get()(默认 GET)、wp_remote_post()(默认 POST)、wp_remote_head()(默认 HEAD)。
- 安全提示:对于用户控制的 URL,使用 wp_safe_remote_request() 替代。
- 源代码显示函数内部调用 _wp_http_get_object() 获取 WP_Http 对象并执行请求。
代码示例
// 发送 DELETE 请求示例
$response = wp_remote_request( 'http://www.example.com/index.php',
array(
'method' => 'DELETE'
)
);
$body = wp_remote_retrieve_body($response);
echo $body;注意事项
- 处理响应时需检查是否为 WP_Error,并验证 HTTP 状态码(如 200 或 201)以确保成功。
- 在调用第三方 API 时,可结合 apply_filters 允许参数修改,并设置超时等选项。
原文内容
Performs an HTTP request and returns its response.
Description
There are other API functions available which abstract away the HTTP method:
- Default ‘GET’ for wp_remote_get()
- Default ‘POST’ for wp_remote_post()
- Default ‘HEAD’ for wp_remote_head()
Important: If the URL is user-controlled, use wp_safe_remote_request() instead.
See also
- WP_Http::request(): For information on default arguments.
Parameters
$urlstringrequired-
URL to retrieve.
$argsarrayoptional-
Request arguments.
See WP_Http::request() for information on accepted arguments.Default:
array()
Source
function wp_remote_request( $url, $args = array() ) {
$http = _wp_http_get_object();
return $http->request( $url, $args );
}
Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
Skip to note 4 content
Abiral Neupane
Send a Delete request with wp_remote_post
$response = wp_remote_request( 'http://www.example.com/index.php', array( 'method' => 'DELETE' ) ); $body = wp_remote_retrieve_body($response); echo $body; /* Prints response provided from the service provider. Most of the cases, it will be JSON */Skip to note 5 content
David Towoju
You can insert the DELETE method inside
wp_remote_request()like this:$url = 'http://wordpress.org'; $args = array( 'method' => 'DELETE' ); $response = wp_remote_request( $url, $args );Skip to note 6 content
cartpauj
Sample ConvertKit API call function using wp_remote_request()
private function call($args, $endpoint, $api_secret = null, $method = 'GET') { if(is_null($api_secret)) { $api_secret = $this->api_secret(); } //Populate the correct endpoint for the API request $url = "<a href="https://api.convertkit.com/v3/" rel="nofollow ugc">https://api.convertkit.com/v3/</a>{$endpoint}?api_secret={$api_secret}"; //Allow 3rd parties to alter the $args $args = apply_filters('convertkit-call-args', $args, $endpoint, $method); //Populate the args for use in the wp_remote_request call $wp_args = array('body' => $args); $wp_args['method'] = $method; $wp_args['timeout'] = 30; //Make the call and store the response in $res $res = wp_remote_request($url, $wp_args); //Check for success if(!is_wp_error($res) && ($res['response']['code'] == 200 || $res['response']['code'] == 201)) { return $res['body']; } else { return false; } }What about calling this function? Well here’s another function which updates a contact’s email address/name
public function update_subscriber($contact, $new_email = '') { $id = $this->get_subscriber_id_by_email($contact); if(!$id) { return; } //Nada found? $args = array( 'email_address' => (!empty($new_email) && is_email($new_email))?$new_email:$contact->user_email, 'first_name' => (string)$contact->first_name ); $resp_body = (array)json_decode($this->call($args, "subscribers/{$id}", null, 'PUT')); //don't really care what the response was at this point, maybe we'll update this later return true; }