fetch_rss()
云策文档标注
概述
fetch_rss() 函数用于从指定 URL 获取 RSS 源并构建 MagpieRSS 对象。它支持缓存机制,根据 MAGPIE_CACHE_ON 常量决定是否启用缓存,以提高性能并处理网络请求失败的情况。
关键要点
- 参数:$url(必需),要检索 RSS 源的 URL。
- 返回值:成功时返回 MagpieRSS 对象,失败时返回 false。
- 缓存逻辑:如果 MAGPIE_CACHE_ON 为 false,直接获取远程文件;否则,使用 RSSCache 检查缓存状态(HIT、MISS、STALE),并可能进行条件获取(如 If-None-Match 和 If-Last-Modified 头)。
- 错误处理:包括网络请求失败、缓存错误等,并尝试返回过期的缓存对象作为后备。
- 依赖函数:如 _fetch_remote_file()、is_success()、_response_to_rss() 和 init()。
代码示例
function fetch_rss ($url) {
// initialize constants
init();
if ( !isset($url) ) {
return false;
}
// if cache is disabled
if ( !MAGPIE_CACHE_ON ) {
$resp = _fetch_remote_file( $url );
if ( is_success( $resp->status ) ) {
return _response_to_rss( $resp );
}
else {
return false;
}
}
// else cache is ON
else {
$cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
// ... 缓存检查和处理逻辑
}
}注意事项
- 确保在调用前已定义相关常量(如 MAGPIE_CACHE_ON、MAGPIE_CACHE_DIR、MAGPIE_CACHE_AGE),否则可能影响缓存行为。
- 函数内部使用 WP HTTP Request API 进行远程文件获取,需确保服务器环境支持。
- 在调试模式下(MAGPIE_DEBUG),可以通过 debug() 函数输出日志信息。
原文内容
Build Magpie object based on RSS from URL.
Parameters
$urlstringrequired-
URL to retrieve feed.
Source
function fetch_rss ($url) {
// initialize constants
init();
if ( !isset($url) ) {
// error("fetch_rss called without a url");
return false;
}
// if cache is disabled
if ( !MAGPIE_CACHE_ON ) {
// fetch file, and parse it
$resp = _fetch_remote_file( $url );
if ( is_success( $resp->status ) ) {
return _response_to_rss( $resp );
}
else {
// error("Failed to fetch $url and cache is off");
return false;
}
}
// else cache is ON
else {
// Flow
// 1. check cache
// 2. if there is a hit, make sure it's fresh
// 3. if cached obj fails freshness check, fetch remote
// 4. if remote fails, return stale object, or error
$cache = new RSSCache( MAGPIE_CACHE_DIR, MAGPIE_CACHE_AGE );
if (MAGPIE_DEBUG and $cache->ERROR) {
debug($cache->ERROR, E_USER_WARNING);
}
$cache_status = 0; // response of check_cache
$request_headers = array(); // HTTP headers to send with fetch
$rss = 0; // parsed RSS object
$errormsg = 0; // errors, if any
if (!$cache->ERROR) {
// return cache HIT, MISS, or STALE
$cache_status = $cache->check_cache( $url );
}
// if object cached, and cache is fresh, return cached obj
if ( $cache_status == 'HIT' ) {
$rss = $cache->get( $url );
if ( isset($rss) and $rss ) {
$rss->from_cache = 1;
if ( MAGPIE_DEBUG > 1) {
debug("MagpieRSS: Cache HIT", E_USER_NOTICE);
}
return $rss;
}
}
// else attempt a conditional get
// set up headers
if ( $cache_status == 'STALE' ) {
$rss = $cache->get( $url );
if ( isset($rss->etag) and $rss->last_modified ) {
$request_headers['If-None-Match'] = $rss->etag;
$request_headers['If-Last-Modified'] = $rss->last_modified;
}
}
$resp = _fetch_remote_file( $url, $request_headers );
if (isset($resp) and $resp) {
if ($resp->status == '304' ) {
// we have the most current copy
if ( MAGPIE_DEBUG > 1) {
debug("Got 304 for $url");
}
// reset cache on 304 (at minutillo insistent prodding)
$cache->set($url, $rss);
return $rss;
}
elseif ( is_success( $resp->status ) ) {
$rss = _response_to_rss( $resp );
if ( $rss ) {
if (MAGPIE_DEBUG > 1) {
debug("Fetch successful");
}
// add object to cache
$cache->set( $url, $rss );
return $rss;
}
}
else {
$errormsg = "Failed to fetch $url. ";
if ( $resp->error ) {
# compensate for Snoopy's annoying habit to tacking
# on 'n'
$http_error = substr($resp->error, 0, -2);
$errormsg .= "(HTTP Error: $http_error)";
}
else {
$errormsg .= "(HTTP Response: " . $resp->response_code .')';
}
}
}
else {
$errormsg = "Unable to retrieve RSS file for unknown reasons.";
}
// else fetch failed
// attempt to return cached object
if ($rss) {
if ( MAGPIE_DEBUG ) {
debug("Returning STALE object for $url");
}
return $rss;
}
// else we totally failed
// error( $errormsg );
return false;
} // end if ( !MAGPIE_CACHE_ON ) {
} // end fetch_rss()
Changelog
| Version | Description |
|---|---|
| 1.5.0 | Introduced. |