wp_remote_get( string $url, array $args = array() ): array|WP_Error
Performs an HTTP request using the GET method and returns its response.
Contents
Description
See also
- wp_remote_request() : For more information on the response array format.
- WP_Http::request(): For default arguments information.
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
stringRequest method. Accepts'GET'
,'POST'
,'HEAD'
,'PUT'
,'DELETE'
,'TRACE'
,'OPTIONS'
, or'PATCH'
.
Some transports technically allow others, but should not be assumed. Default'GET'
.timeout
floatHow long the connection should stay open in seconds. Default 5.redirection
intNumber of allowed redirects. Not supported by all transports.
Default 5.httpversion
stringVersion of the HTTP protocol to use. Accepts'1.0'
and'1.1'
.
Default'1.0'
.user-agent
stringUser-agent value sent.
Default'WordPress/'
. get_bloginfo('version'
) . '; ' . get_bloginfo('url'
).reject_unsafe_urls
boolblocking
boolWhether 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|arrayArray or string of headers to send with the request.
cookies
arrayList of cookies to send with the request.body
string|arrayBody to send with the request. Default null.compress
boolWhether to compress the $body when sending the request.
Default false.decompress
boolWhether 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
boolWhether to verify SSL for the request. Default true.sslcertificates
stringAbsolute path to an SSL certificate .crt file.
Default ABSPATH . WPINC .'/certificates/ca-bundle.crt'
.stream
boolWhether 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
stringFilename of the file to write to when streaming. $stream must be set to true. Default null.limit_response_size
intSize in bytes to limit the response to. Default null.
Default:
array()
Return
More Information
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() .
Source
File: wp-includes/http.php
.
View all references
function wp_remote_get( $url, $args = array() ) {
$http = _wp_http_get_object();
return $http->get( $url, $args );
}
Changelog
Version | Description |
---|---|
2.7.0 | Introduced. |
User Contributed Notes
You must log in before being able to contribute a note or feedback.
Valid arguments for the second parameter can be found in class-http.php in the header. There is not easy way to reference the list on the current version of this guide so I’m pasting the PHPDoc header here. Hopefully the docs site will expand the WP_Http page or find a way to reference the valid parameters through indirection while the robots build these pages.
* @param string|array $args {
* Optional. Array or string of HTTP request arguments.
*
* @type string $method Request method. Accepts 'GET', 'POST', 'HEAD', or 'PUT'.
* Some transports technically allow others, but should not be
* assumed. Default 'GET'.
* @type int $timeout How long the connection should stay open in seconds. Default 5.
* @type int $redirection Number of allowed redirects. Not supported by all transports
* Default 5.
* @type string $httpversion Version of the HTTP protocol to use. Accepts '1.0' and '1.1'.
* Default '1.0'.
* @type string $user-agent User-agent value sent.
* Default WordPress/' . $wp_version . '; ' . get_bloginfo( 'url' ).
* @type bool $reject_unsafe_urls Whether to pass URLs through wp_http_validate_url().
* Default false.
* @type bool $blocking 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.
* @type string|array $headers Array or string of headers to send with the request.
* Default empty array.
* @type array $cookies List of cookies to send with the request. Default empty array.
* @type string|array $body Body to send with the request. Default null.
* @type bool $compress Whether to compress the $body when sending the request.
* Default false.
* @type bool $decompress 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.
* @type bool $sslverify Whether to verify SSL for the request. Default true.
* @type string sslcertificates Absolute path to an SSL certificate .crt file.
* Default ABSPATH . WPINC . '/certificates/ca-bundle.crt'.
* @type bool $stream Whether to stream to a file. If set to true and no filename was
* given, it will be droped it in the WP temp dir and its name will
* be set using the basename of the URL. Default false.
* @type string $filename Filename of the file to write to when streaming. $stream must be
* set to true. Default null.
* @type int $limit_response_size Size in bytes to limit the response to. Default null.
Top ↑
Feedback
Nowadays there is a way to reference the $args: https://developer.wordpress.org/reference/classes/WP_Http/request/ — By Andrija Naglic —
Get a remote URL
The top comment by Store Locator Plus adds good information but it is hard to read.
To find the arguments you can see WP_Http::request documentation or see below for a formatted PHPDoc version:
Get a remote URL with special arguments
When adding headers, each part of the header has to be a separate element like this:
not like cURL where you can use ‘Content-Type: application/json’
If you load data from an API and have set a domain restriction on the API key (such as only allow on “www.example.com/*”), you need to set the “referer” in “headers” in $args, as wp_remote_get() does not set the referer automatically:
Or, as a string, to work on your localhost as well:
It would be cool to have a link to this somewhere on this page: https://developer.wordpress.org/reference/functions/wp_safe_remote_get/
replace
wp_get_http( $url, $filename )
to get a remote file and write to the file system:Thank you @charlestonsw for pasting in the comments with the meaning of all the args, which lead me to this simple replacement.
It’s better to execute JSON response like that to keep it managed.
An example for reading a JSON response from an API end point with error checking.
Here we are checking for the response code of 200 utilizing the wp_remote_retrieve_response_code() function and also verifying there are no JSON errors during the json_decode() function call utilising the json_last_error() function.
Top ↑
Feedback
$result = json_decode($responseBody);
if success as it is will return an object and not a array, my friend, so the if will fail even if there are no errors. Consider changing line 4 to:$result = json_decode($responseBody, true);
— By Marcos Silva —json_decode()
is native to PHP, and so will never return an instance ofWP_Error
object; that check is unnecessary, or, should be adjusted to check$response
. — By crstauf —