wp_remote_request( string $url, array $args = array() ): array|WP_Error

Performs an HTTP request and returns its response.

Description

There are other API functions available which abstract away the HTTP method:

See also

Parameters

$urlstringrequired
URL to retrieve.
$argsarrayoptional
Request arguments.
See WP_Http::request() for information on accepted arguments.

Default:array()

Return

array|WP_Error 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.
  • cookies WP_HTTP_Cookie[]
    Array of response cookies.
  • http_response WP_HTTP_Requests_Response|null
    Raw HTTP response object.

Source

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

Changelog

VersionDescription
2.7.0Introduced.

User Contributed Notes

  1. Skip to note 4 content

    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  */
  2. Skip to note 6 content

    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.