wp_remote_get()采集/获取远程文件的内容以及文件信息

目录

描述

Retrieve the raw response from the HTTP request using the GET method. Results include HTTP headers and content.

See wp_remote_post() for using the HTTP POST method

wp_remote_get() 描述

用法

<?php $response wp_remote_get$url$args ); ?>

Default Usage

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
<?php $args = 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); ?>

wp_remote_get() 用法

参数

$url

(string) (必填) Site URL to retrieve.

默认值: None

$args

(array) (可选) Override the defaults.

默认值: array()

See HTTP API for more information on the arguments array format.

wp_remote_get() 参数

返回值

(WP_Error|array

The response or WP_Error on failure. See wp_remote_post() for a full example of response array format.

wp_remote_get() 返回值

示例

Get a remote URL:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

$response = wp_remote_get( 'http://www.example.com/index.html' );
if( is_array($response) ) {
  $header = $response['headers']; // array of http header lines
  $body = $response['body']; // use the content
}

Get a remote URL with special arguments:

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */

wp_remote_get( 'http://www.example.com/index.php?action=foo', array( 'timeout' => 120, 'httpversion' => '1.1' ) );

wp_remote_get() 示例

注意

for return array see WP_Http_Streams::request() method located in wp-includes/class-http.php

wp_remote_get() 注意

历史

添加于 版本: 2.7.0

wp_remote_get() 历史

源文件

wp_remote_get() 函数的代码位于 wp-includes/http.php

/* ----------------------------------
 * wordpress之魂 © http://wphun.com
 * ---------------------------------- */
/**
 * Retrieve the raw response from the HTTP request using the GET method.
 *
 * @since 2.7.0
 *
 * @see wp_remote_request() For more information on the response array format.
 * @see WP_Http::request() For default arguments information.
 *
 * @param string $url  Site URL to retrieve.
 * @param array  $args Optional. Request arguments. Default empty array.
 * @return WP_Error|array The response or WP_Error on failure.
 */
function wp_remote_get($url, $args = array()) {
	$http = _wp_http_get_object();
	return $http->get( $url, $args );
}

wp_remote_get() 源文件