wp_get_http()
云策文档标注
概述
wp_get_http() 是一个已弃用的 WordPress 函数,用于执行 HTTP HEAD 或 GET 请求。如果提供可写文件路径,它会执行 GET 请求并将响应内容写入文件;否则执行 HEAD 请求。该函数自 WordPress 4.4.0 起被弃用,建议使用 WP_Http 类替代。
关键要点
- 函数 wp_get_http() 用于执行 HTTP HEAD 或 GET 请求,根据 $file_path 参数决定请求类型。
- 如果 $file_path 为可写文件路径,执行 GET 请求并将响应内容写入该文件;否则执行 HEAD 请求。
- 函数自 WordPress 4.4.0 起被弃用,推荐使用 WP_Http 类进行 HTTP 请求操作。
- 支持重定向处理,最多允许 5 次重定向,超过则返回 false。
- 返回值为 WpOrgRequestsUtilityCaseInsensitiveDictionary 类型(成功时包含头部信息)或 false(失败时)。
代码示例
function wp_get_http( $url, $file_path = false, $red = 1 ) {
_deprecated_function( __FUNCTION__, '4.4.0', 'WP_Http' );
// Add 60 seconds to the script timeout to ensure the remote request has enough time.
if ( function_exists( 'set_time_limit' ) ) {
@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;
}注意事项
- 该函数已弃用,开发者应迁移到 WP_Http 类以避免未来兼容性问题。
- 文档中提到的返回类型描述可能有误:实际返回数组(包含头部信息),而非字符串。
- 函数内部使用 wp_safe_remote_request() 进行安全请求,并处理重定向逻辑。
原文内容
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.
See also
Parameters
$urlstringrequired-
URL to fetch.
$file_pathstring|booloptional-
File path to write request to.
Default:
false $redintoptional-
The number of Redirects followed, Upon 5 being hit, returns false.
Default:
1
Source
function wp_get_http( $url, $file_path = false, $red = 1 ) {
_deprecated_function( __FUNCTION__, '4.4.0', 'WP_Http' );
// Add 60 seconds to the script timeout to ensure the remote request has enough time.
if ( function_exists( 'set_time_limit' ) ) {
@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;
}
Skip to note 2 content
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