wp_safe_remote_request()WP 3.6.0

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.

Использование

wp_safe_remote_request( $url, $args );
$url(string) (required)
URL of the site to retrieve data from.
$args(array)
Request parameters.
Default: array()

Примеры

0

#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

Changelog

Since 3.6.0 Introduced.

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 );
}