wp_remote_request( string $url, array $args = array() )
Performs an HTTP request and returns its response.
Contents
Description 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()
See also See also
- WP_Http::request(): For information on default arguments.
Parameters Parameters
- $url
-
(string) (Required) URL to retrieve.
- $args
-
(array) (Optional) Request arguments.
Default value: array()
Return Return
(WP_Error|array) The response array or a WP_Error on failure.
- 'headers'
(string[]) Array of response headers keyed by their name. - 'body'
(string) Response body. - 'response'
(array) Data about the HTTP response.- 'code'
(int|false) HTTP response code. - 'message'
(string|false) HTTP response message.
- 'code'
- 'cookies'
(WP_HTTP_Cookie[]) Array of response cookies. - 'http_response'
(WP_HTTP_Requests_Response|null) Raw HTTP response object.
Source Source
File: wp-includes/http.php
function wp_remote_request( $url, $args = array() ) {
$http = _wp_http_get_object();
return $http->request( $url, $args );
}
Expand full source code Collapse full source code View on Trac
Changelog Changelog
| Version | Description |
|---|---|
| 2.7.0 | Introduced. |
User Contributed Notes User Contributed Notes
You must log in before being able to contribute a note or feedback.
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 */You can insert the DELETE method inside
wp_remote_request()like this: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 = "https://api.convertkit.com/v3/{$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; } }Expand full source codeCollapse full source code
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; }Expand full source codeCollapse full source code