wp_remote_get()
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
Array|WP_Error
. The response or WP_Error on failure. See WP_Http::request() for information on return value.
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.
Usage Template
$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 Get an external page
$response = wp_remote_get( 'http://www.example.com/index.html' );
$response 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 the parameters when retrieving a remote page
$response = wp_remote_get( 'http://www.example.com/index.php?action=foo', [ 'timeout' => 120, 'httpversion' => '1.1' ] ); // check if you got the right answer if ( is_wp_error( $response ) ){ echo $response->get_error_message(); } elseif( wp_remote_retrieve_response_code( $response ) === 200 ){ // All OK, do something with the data $request['body'] $body = wp_remote_retrieve_body( $response ); }
Or it could be simpler:
if ( wp_remote_retrieve_response_code( $response ) === 200 ){ $body = wp_remote_retrieve_body( $response ); }
Or even simpler:
$body = wp_remote_retrieve_body( $response ); // return '' if error
#3 Send a non-blocking request
This example shows how to send a non-blocking request. It is used to simply make a request and continue to execute the current code without paying attention to what is returned in the response:
$remote_url = 'https://site.com/wp-cron.php'; $args = [ 'timeout' => 0.01, 'blocking' => false, 'sslverify' => false, ]; $result = wp_remote_post( $remote_url, $args ); // continue executing the code
Notes
- See: wp_remote_request() For more information on the response array format.
- See: WP_Http::request() For default arguments information.
Changelog
Since 2.7.0 | Introduced. |
wp_remote_get() wp remote get code WP 6.7.2
function wp_remote_get( $url, $args = array() ) { $http = _wp_http_get_object(); return $http->get( $url, $args ); }