WordPress.org

Codex

Interested in functions, hooks, classes, or methods? Check out the new WordPress Code Reference!

Function Reference/wp remote get

Description

Retrieve the raw response from the HTTP request using the GET method. Results include HTTP headers and content.

See wp_remote_post() for using the HTTP POST method

Usage

<?php $response wp_remote_get$url$args ); ?>

Default Usage

<?php 
global $wp_version;
$args = array(
    'timeout'     => 5,
    'redirection' => 5,
    'httpversion' => '1.0',
    'user-agent'  => 'WordPress/' . $wp_version . '; ' . home_url(),
    'blocking'    => true,
    'headers'     => array(),
    'cookies'     => array(),
    'body'        => null,
    'compress'    => false,
    'decompress'  => true,
    'sslverify'   => true,
    'stream'      => false,
    'filename'    => null
); 
?>

Parameters

$url
(string) (required) Site URL to retrieve.
Default: None
$args
(array) (optional) Override the defaults.
Default: array()

See HTTP API for more information on the arguments array format.

Return Values

(WP_Error|array) 
The response or WP_Error on failure. See wp_remote_post() for a full example of response array format.

Examples

Get a remote URL:

$response = wp_remote_get( 'http://www.example.com/index.html' );
if ( is_array( $response ) ) {
  $header = $response['headers']; // array of http header lines
  $body = $response['body']; // use the content
}

Get a remote URL with special arguments:

wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );

Notes

Use wp_remote_retrieve_body( $response ) to get the response body.

Use wp_remote_retrieve_response_code( $response ) to get the HTTP status code for the response.

Use related functions in wp-includes/http.php to get other parameters such as headers.

See WP_Http_Streams::request() method located in wp-includes/class-wp-http-streams.php for the format of the array returned by wp_remote_get().

Change Log

Since: 2.7.0

Source File

wp_remote_get() is located in wp-includes/http.php

Related

HTTP API: wp_remote_request(), wp_remote_get(), wp_remote_post(), wp_remote_head() wp_remote_retrieve_body(), wp_remote_retrieve_header(), wp_remote_retrieve_headers(), wp_remote_retrieve_response_code(), wp_remote_retrieve_response_message()