WordPress at a glance
Home All Function Functions APIs HTTP API

wp_remote_get() WP 2.7.0

Retrieve the raw response from the HTTP request using the GET method. It's a wrapper for curl. The result includes HTTP headers and content of the webpage, and returned in the form of an array.

Use wp_remote_post() for HTTP POST method.

Read more about HTTP API.

wp_safe_remote_get() is a safe alternative of this function.

No Hooks.

Return

WP_Error/Array. The response or WP_Error on failure.

If a server returned 500, 404 etc. status codes, there won't be a WP_Error – the data of the response will be returned as usual.

Template of usage

$remote_get = wp_remote_get( $url, array(
	'timeout'     => 5,
	'redirection' => 5,
	'httpversion' => '1.0',
	'user-agent'  => 'WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ),
	'blocking'    => true,
	'headers'     => array(),
	'cookies'     => array(),
	'body'        => null,
	'compress'    => false,
	'decompress'  => true,
	'sslverify'   => true,
	'stream'      => false,
	'filename'    => null
) );

Usage

wp_remote_get( $url, $args );
$url(string) (required)
Site URL to retrieve.
$args(array)

Request arguments in an array.

  • timeout — time in seconds, before the connection is dropped and an error is returned. The time can be changed in the plugin through the filter http_request_timeout.
    Default: 5

  • redirection — the amount of times to follow a redirect before giving up. There is a filter for the value: http_request_redirection_count.
    Default: 5

  • user-agent — it allows you to set the user-agent. There is a filter for changing this value: http_headers_useragent.
    Default: "WordPress/4.0; http://example.com"

  • blocking — allows you to trigger a non-blocking request. The default is true; setting it to false will generally allow PHP to continue execution while the transport is working. The key is that when you set blocking to false, then it will just send the request and won't bother you with the details. This is useful for sending a POST request, where you aren't concerned with whether it succeeded or not, or if you don't want to slow down the processing time of the page. (Note that not all the transports support non-blocking requests, and so you may still be blocked anyway. The alternative of setting an ultra-low timeout is not recommended, since low timeouts may cause the request to not be sent at all with some transports.)
    Default: true

  • compress — allows you to send the request body as compressed.

  • decompress — Responsible for accept-encoding request header. If you do leave this as true, then the headers will be set to tell the server that compressed data is accepted and it will be decompressed in the response. If you sent it to false, then the header will be sent that tells servers that compressed content is not accepted. If the content is still compressed, because of incorrect implementation or you sent the headers for it, then you will need to decompress the content.
    Default: true

  • sslverify — true - will check to see if the SSL certificate is valid (not self-signed, actually for the site in the request) and will deny the response if it isn't. If you are requesting HTTPS and know that the site is self-signed or is invalid and are reasonably sure that it can be trusted, then set to false.

  • method — HTTP method to use in a request (POST, GET, HEAD, PUT DELETE). It's better to use a special function for this: wp_remote_request().
    Default: array()

Examples

#1 Retrieve an external webpage

$result = wp_remote_get( 'http://example.com/index.html' );

/* $result will contain:
Array
(
	[headers] => Array
		(
			[accept-ranges] => bytes
			[cache-control] => max-age=604800
			[content-type] => text/html
			[date] => Fri, 24 Oct 2014 13:07:13 GMT
			[etag] => "359670651"
			[expires] => Fri, 31 Oct 2014 13:07:13 GMT
			[last-modified] => Fri, 09 Aug 2013 23:54:35 GMT
			[server] => ECS (ewr/144C)
			[x-cache] => HIT
			[x-ec-custom-error] => 1
			[content-length] => 1270
			[connection] => close
		)

	[body] => <!doctype html> ...
	[response] => Array
		(
			[code] => 200
			[message] => OK
		)

	[cookies] => Array
		(
		)

	[filename] => 
)
*/

#2 Set an arguments

$response = wp_remote_get( 
	'http://example.com/index.php?action=foo', 
	[ 'timeout' => 120, 'httpversion' => '1.1' ]
);

// errors check
if ( is_wp_error( $response ) ){
	echo $response->get_error_message();
}
elseif( wp_remote_retrieve_response_code( $response ) === 200 ){
	// Everything is OK, do something with the data $request['body']
	$body = wp_remote_retrieve_body( $response );
}
// it can be done easier
if ( wp_remote_retrieve_response_code( $response ) === 200 ){
	$body = wp_remote_retrieve_body( $response );
}

// or even much easier
$body = wp_remote_retrieve_body( $response ); // returns '' in the case of error

Code of wp remote get: wp-includes/http.php WP 5.2.1

<?php
function wp_remote_get( $url, $args = array() ) {
	$http = _wp_http_get_object();
	return $http->get( $url, $args );
}

Related Functions

From tag: HTTP API (curl remote)

No comments
    Hello, !     Log In . Register