函数文档

_response_to_rss()

💡 云策文档标注

概述

_response_to_rss() 函数用于将 HTTP 响应转换为 MagpieRSS 对象,处理 RSS 解析和错误检查。它从响应头中提取 Etag 和 Last-Modified 信息,并在解析失败时返回 false。

关键要点

  • 函数接受一个参数 $resp(必需数组),返回 MagpieRSS 对象或布尔值 false。
  • 通过 MagpieRSS 构造函数解析响应结果,检查是否成功且无错误。
  • 遍历响应头以查找并设置 Etag 和 Last-Modified 字段。
  • 解析失败时返回 false 并生成错误消息。

代码示例

function _response_to_rss ($resp) {
    $rss = new MagpieRSS( $resp->results );

    // if RSS parsed successfully
    if ( $rss && (!isset($rss->ERROR) || !$rss->ERROR) ) {

        // find Etag, and Last-Modified
        foreach ( (array) $resp->headers as $h) {
            // 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
            if (strpos($h, ": ")) {
                list($field, $val) = explode(": ", $h, 2);
            }
            else {
                $field = $h;
                $val = "";
            }

            if ( $field == 'etag' ) {
                $rss->etag = $val;
            }

            if ( $field == 'last-modified' ) {
                $rss->last_modified = $val;
            }
        }

        return $rss;
    } // else construct error message
    else {
        $errormsg = "Failed to parse RSS file.";

        if ($rss) {
            $errormsg .= " (" . $rss->ERROR . ")";
        }
        // error($errormsg);

        return false;
    } // end if ($rss and !$rss->error)
}

注意事项

  • 函数在 WordPress 1.5.0 版本中引入,用于支持 RSS 解析。
  • 相关函数包括 MagpieRSS::__construct() 和 fetch_rss(),后者基于 URL 构建 Magpie 对象。
  • 代码中包含对响应头解析的 bug 修复,确保正确处理字段分隔符。

📄 原文内容

Retrieve

Parameters

$resparrayrequired

Return

MagpieRSS|bool

Source

function _response_to_rss ($resp) {
	$rss = new MagpieRSS( $resp->results );

	// if RSS parsed successfully
	if ( $rss && (!isset($rss->ERROR) || !$rss->ERROR) ) {

		// find Etag, and Last-Modified
		foreach ( (array) $resp->headers as $h) {
			// 2003-03-02 - Nicola Asuni (www.tecnick.com) - fixed bug "Undefined offset: 1"
			if (strpos($h, ": ")) {
				list($field, $val) = explode(": ", $h, 2);
			}
			else {
				$field = $h;
				$val = "";
			}

			if ( $field == 'etag' ) {
				$rss->etag = $val;
			}

			if ( $field == 'last-modified' ) {
				$rss->last_modified = $val;
			}
		}

		return $rss;
	} // else construct error message
	else {
		$errormsg = "Failed to parse RSS file.";

		if ($rss) {
			$errormsg .= " (" . $rss->ERROR . ")";
		}
		// error($errormsg);

		return false;
	} // end if ($rss and !$rss->error)
}

Changelog

Version Description
1.5.0 Introduced.