函数文档

_fetch_remote_file()

💡 云策文档标注

概述

_fetch_remote_file() 是一个 WordPress 函数,用于通过 WP HTTP Request API 获取远程 URL 的头部信息和内容,并返回 Snoopy 风格的响应对象。它主要用于 RSS 解析等场景。

关键要点

  • 函数接受两个参数:必需的 URL 字符串和可选的头部数组(默认为空字符串)。
  • 内部使用 wp_safe_remote_request() 发起安全 HTTP 请求,并处理可能的 WP_Error。
  • 返回一个 stdClass 对象,包含状态码、响应码、头部数组和响应体,以兼容 Snoopy 格式。
  • 头部处理时,WP_HTTP 会将键名小写,而 Snoopy 不会,函数对此进行了适配。
  • 相关函数包括 wp_safe_remote_request()、wp_remote_retrieve_headers()、wp_remote_retrieve_response_code()、wp_remote_retrieve_body() 和 is_wp_error()。
  • 该函数自 WordPress 1.5.0 版本引入,主要用于 fetch_rss() 函数中。

代码示例

function _fetch_remote_file($url, $headers = "" ) {
    $resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) );
    if ( is_wp_error($resp) ) {
        $error = array_shift($resp->errors);

        $resp = new stdClass;
        $resp->status = 500;
        $resp->response_code = 500;
        $resp->error = $error[0] . "n"; //n = Snoopy compatibility
        return $resp;
    }

    // Snoopy returns headers unprocessed.
    // Also note, WP_HTTP lowercases all keys, Snoopy did not.
    $return_headers = array();
    foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
        if ( !is_array($value) ) {
            $return_headers[] = "$key: $value";
        } else {
            foreach ( $value as $v )
                $return_headers[] = "$key: $v";
        }
    }

    $response = new stdClass;
    $response->status = wp_remote_retrieve_response_code( $resp );
    $response->response_code = wp_remote_retrieve_response_code( $resp );
    $response->headers = $return_headers;
    $response->results = wp_remote_retrieve_body( $resp );

    return $response;
}

📄 原文内容

Retrieve URL headers and content using WP HTTP Request API.

Parameters

$urlstringrequired
URL to retrieve
$headersarrayoptional
Headers to send to the URL. Default empty string.

Return

Snoopy style response

Source

function _fetch_remote_file($url, $headers = "" ) {
	$resp = wp_safe_remote_request( $url, array( 'headers' => $headers, 'timeout' => MAGPIE_FETCH_TIME_OUT ) );
	if ( is_wp_error($resp) ) {
		$error = array_shift($resp->errors);

		$resp = new stdClass;
		$resp->status = 500;
		$resp->response_code = 500;
		$resp->error = $error[0] . "n"; //n = Snoopy compatibility
		return $resp;
	}

	// Snoopy returns headers unprocessed.
	// Also note, WP_HTTP lowercases all keys, Snoopy did not.
	$return_headers = array();
	foreach ( wp_remote_retrieve_headers( $resp ) as $key => $value ) {
		if ( !is_array($value) ) {
			$return_headers[] = "$key: $value";
		} else {
			foreach ( $value as $v )
				$return_headers[] = "$key: $v";
		}
	}

	$response = new stdClass;
	$response->status = wp_remote_retrieve_response_code( $resp );
	$response->response_code = wp_remote_retrieve_response_code( $resp );
	$response->headers = $return_headers;
	$response->results = wp_remote_retrieve_body( $resp );

	return $response;
}

Changelog

Version Description
1.5.0 Introduced.