wp_remote_request( string $url, array $args = array() )

Retrieve the raw response from the HTTP request.


Description Description

The array structure is a little complex:

$res = array(
    'headers'  => array(),
    'response' => array(
        'code'    => int,
        'message' => string
    )
);

All of the headers in $res[‘headers’] are with the name as the key and the
value as the value. So to get the User-Agent, you would do the following.

$user_agent = $res['headers']['user-agent'];

The body is the raw response content and can be retrieved from $res[‘body’].

This function is called first to make the request and there are other API functions to abstract out the above convoluted setup.

Request method defaults for helper functions:


Parameters Parameters

$url

(string) (Required) Site URL to retrieve.

$args

(array) (Optional) Request arguments.

Default value: array()


Top ↑

Return Return

(WP_Error|array) The response or WP_Error on failure.


Top ↑

Source Source

File: wp-includes/http.php

function wp_remote_request($url, $args = array()) {
	$http = _wp_http_get_object();
	return $http->request( $url, $args );
}

Top ↑

Changelog Changelog

Changelog
Version Description
2.7.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

  1. Skip to note content
    Contributed by 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                = "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;
        }
      }

    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;
      }

You must log in before being able to contribute a note or feedback.