wp_safe_remote_request()
Gets data for the given HTTP request (URL). Data is returned as an array: body, headers, response status...
This function is well suited for HTTP requests to unknown URLs. The given URL is checked to avoid redirects and attacks related to "spoofing" the URL.
Возвращает
Array|WP_Error.
Array- The server response as an array. See WP_Http::request().- WP_Error - in case of error.
Использование
wp_safe_remote_request( $url, $args );
- $url(string) (required)
- URL of the site to retrieve data from.
- $args(array)
- Request parameters.
Default: array()
Примеры
#1 Get server response data by URL
Suppose we want to get a page http://wordpress.org, but first we have to check if the server returned the status code 200 (OK):
$res = wp_safe_remote_request('http://wordpress.org');
print_r( $res );
We Get:
Array ( [headers] => Array ( [server] => nginx [date] => Sun, 02 Nov 2014 21:01:46 GMT [content-type] => text/html; charset=utf-8 [connection] => close [vary] => Accept-Encoding [x-frame-options] => SAMEORIGIN [x-nc] => HIT lax 250 ) [body] => <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml" dir="ltr" lang="en"> ... [response] => Array ( [code] => 200 [message] => OK ) [cookies] => Array ( ) [filename] =>
Notes
- See: wp_remote_request() For more information on the response array format.
- See: WP_Http::request() For default arguments information.
- See: wp_http_validate_url() For more information about how the URL is validated.
Changelog
| Since 3.6.0 | Introduced. |
wp_safe_remote_request() wp safe remote request code WP 7.0
function wp_safe_remote_request( $url, $args = array() ) {
$args['reject_unsafe_urls'] = true;
$http = _wp_http_get_object();
return $http->request( $url, $args );
}