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:

Top ↑

See also


Top ↑

Parameters

$url string Required
URL to retrieve.
$args array Optional
Request arguments.
See WP_Http::request() for information on accepted arguments.
More Arguments from WP_Http::request( ... $args ) Array or string of HTTP request arguments.
  • method string
    Request method. Accepts 'GET', 'POST', 'HEAD', 'PUT', 'DELETE', 'TRACE', 'OPTIONS', or 'PATCH'.
    Some transports technically allow others, but should not be assumed. Default 'GET'.
  • timeout float
    How long the connection should stay open in seconds. Default 5.
  • redirection int
    Number of allowed redirects. Not supported by all transports.
    Default 5.
  • httpversion string
    Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
    Default '1.0'.
  • user-agent string
    User-agent value sent.
    Default 'WordPress/' . get_bloginfo( 'version' ) . '; ' . get_bloginfo( 'url' ).
  • reject_unsafe_urls bool
    Whether to pass URLs through wp_http_validate_url() .
    Default false.
  • blocking bool
    Whether the calling code requires the result of the request.
    If set to false, the request will be sent to the remote server, and processing returned to the calling code immediately, the caller will know if the request succeeded or failed, but will not receive any response from the remote server. Default true.
  • headers string|array
    Array or string of headers to send with the request.
  • cookies array
    List of cookies to send with the request.
  • body string|array
    Body to send with the request. Default null.
  • compress bool
    Whether to compress the $body when sending the request.
    Default false.
  • decompress bool
    Whether to decompress a compressed response. If set to false and compressed content is returned in the response anyway, it will need to be separately decompressed. Default true.
  • sslverify bool
    Whether to verify SSL for the request. Default true.
  • sslcertificates string
    Absolute path to an SSL certificate .crt file.
    Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
  • stream bool
    Whether to stream to a file. If set to true and no filename was given, it will be dropped it in the WP temp dir and its name will be set using the basename of the URL. Default false.
  • filename string
    Filename of the file to write to when streaming. $stream must be set to true. Default null.
  • limit_response_size int
    Size in bytes to limit the response to. Default null.

Default: array()


Top ↑

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.


Top ↑

Source

File: wp-includes/http.php. View all references

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


Top ↑

Changelog

Changelog
Version Description
2.7.0 Introduced.

Top ↑

User Contributed Notes

  1. Skip to note 1 content
    Contributed by 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  */
  2. Skip to note 3 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.