返回介绍

wp_remote_get()

发布于 2017-09-11 12:44:23 字数 7082 浏览 1230 评论 0 收藏 0

wp_remote_get( string $url,  array $args = array() )

Retrieve the raw response from the HTTP request using the GET method.


description


参数

$url

(string) (Required) Site URL to retrieve.

$args

(array) (Optional) Request arguments.

Default value: array()


返回值

(WP_Error|array) The response or WP_Error on failure.


源代码

File: wp-includes/http.php

function wp_remote_get($url, $args = array()) {
	$http = _wp_http_get_object();
	return $http->get( $url, $args );
}

更新日志

Versiondescription
2.7.0Introduced.

相关函数

Uses

  • wp-includes/http.php: _wp_http_get_object()

Used By

  • wp-admin/includes/class-wp-community-events.php: WP_Community_Events::get_events()
  • wp-admin/includes/upgrade.php: wp_install_maybe_enable_pretty_permalinks()
  • wp-admin/includes/network.php: network_step2()
  • wp-admin/includes/schema.php: populate_network()
  • wp-admin/includes/update.php: get_core_checksums()
  • wp-admin/includes/import.php: wp_get_popular_importers()
  • wp-admin/includes/credits.php: wp_credits()
  • wp-includes/deprecated.php: url_is_accessable_via_ssl()
  • Show 3 more used by Hide more used by

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 2You must log in to vote on the helpfulness of this note Contributed by Store Locator Plus

    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.

  2. Get a remote URL with special arguments

    
    /** @var array|WP_Error $response */
    $response = wp_remote_get( 'http://www.example.com/index.php?action=foo',
    	array(
    		'timeout'     => 120,
    		'httpversion' => '1.1',
    	)
    );
    

    Get a remote URL

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
    我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
    原文