返回介绍

wp_get_http()

发布于 2017-09-11 12:04:38 字数 3917 浏览 948 评论 0 收藏 0

Warning: This function has been deprecated. Use WP_Http() instead.

wp_get_http( string $url,  string|bool $file_path = false,  int $red = 1 )

Perform a HTTP HEAD or GET request.


description

If $file_path is a writable filename, this will do a GET request and write the file to that path.


参数

$url

(string) (Required) URL to fetch.

$file_path

(string|bool) (Optional) File path to write request to.

Default value: false

$red

(int) (Optional) The number of Redirects followed, Upon 5 being hit, returns false.

Default value: 1


返回值

(bool|string) False on failure and string of headers if HEAD request.


源代码

File: wp-includes/deprecated.php

function wp_get_http( $url, $file_path = false, $red = 1 ) {
	_deprecated_function( __FUNCTION__, '4.4.0', 'WP_Http' );

	@set_time_limit( 60 );

	if ( $red > 5 )
		return false;

	$options = array();
	$options['redirection'] = 5;

	if ( false == $file_path )
		$options['method'] = 'HEAD';
	else
		$options['method'] = 'GET';

	$response = wp_safe_remote_request( $url, $options );

	if ( is_wp_error( $response ) )
		return false;

	$headers = wp_remote_retrieve_headers( $response );
	$headers['response'] = wp_remote_retrieve_response_code( $response );

	// WP_HTTP no longer follows redirects for HEAD requests.
	if ( 'HEAD' == $options['method'] && in_array($headers['response'], array(301, 302)) && isset( $headers['location'] ) ) {
		return wp_get_http( $headers['location'], $file_path, ++$red );
	}

	if ( false == $file_path )
		return $headers;

	// GET request - write it to the supplied filename
	$out_fp = fopen($file_path, 'w');
	if ( !$out_fp )
		return $headers;

	fwrite( $out_fp,  wp_remote_retrieve_body( $response ) );
	fclose($out_fp);
	clearstatcache();

	return $headers;
}

更新日志

Versiondescription
4.4.0Use WP_Http
2.5.0Introduced.

相关函数

Uses

  • wp-includes/functions.php: _deprecated_function()
  • wp-includes/deprecated.php: wp_get_http()
  • wp-includes/http.php: wp_safe_remote_request()
  • wp-includes/http.php: wp_remote_retrieve_headers()
  • wp-includes/http.php: wp_remote_retrieve_response_code()
  • wp-includes/http.php: wp_remote_retrieve_body()
  • wp-includes/load.php: is_wp_error()
  • Show 2 more uses Hide more uses

Used By

  • wp-includes/deprecated.php: wp_get_http()

User Contributed Notes

  1. Skip to note content You must log in to vote on the helpfulness of this noteVote results for this note: 0You must log in to vote on the helpfulness of this note Contributed by smartoarif

    “Return: (bool|string) False on failure and string of headers if HEAD request.” – this is said in above documentation.

    But, it never returns string; instead returns array containing header info. Noting it here, as i think this is not a wordpress code bug (according to code it should be array return in case of header and ofcourse array is more useful in the scrnario of get full header info), instead a documentation typo, may be.

    // Arif

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

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

发布评论

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