fetch_rss

Build Magpie object based on RSS from URL.

Description

(bool|MagpieRSS) fetch_rss( (string) $url ); 

Returns (bool|MagpieRSS)

false on failure or MagpieRSS object on success.

Parameters (1)

0. $url (string)
URL to retrieve feed

Usage

  1. if ( !function_exists( 'fetch_rss' ) ) { 
  2. require_once ABSPATH . WPINC . '/rss.php'; 
  3.  
  4. // URL to retrieve feed 
  5. $url = ''; 
  6.  
  7. // NOTICE! Understand what this does before running. 
  8. $result = fetch_rss($url); 
  9.  

Defined (1)

The function is defined in the following location(s).

/wp-includes/rss.php  
  1. function fetch_rss ($url) { 
  2. // initialize constants 
  3. init(); 
  4.  
  5. if ( !isset($url) ) { 
  6. //error"fetch_rss called without a url"); 
  7. return false; 
  8.  
  9. // if cache is disabled 
  10. if ( !MAGPIE_CACHE_ON ) { 
  11. // fetch file, and parse it 
  12. $resp = _fetch_remote_file( $url ); 
  13. if ( is_success( $resp->status ) ) { 
  14. return _response_to_rss( $resp ); 
  15. else { 
  16. //error"Failed to fetch $url and cache is off"); 
  17. return false; 
  18. // else cache is ON 
  19. else { 
  20. // Flow 
  21. // 1. check cache 
  22. // 2. if there is a hit, make sure it's fresh 
  23. // 3. if cached obj fails freshness check, fetch remote 
  24. // 4. if remote fails, return stale object, or error 
  25.  
  26.  
  27. if (MAGPIE_DEBUG and $cache->ERROR) { 
  28. debug($cache->ERROR, E_USER_WARNING); 
  29.  
  30. $cache_status = 0; // response of check_cache 
  31. $request_headers = array(); // HTTP headers to send with fetch 
  32. $rss = 0; // parsed RSS object 
  33. $errormsg = 0; // errors, if any 
  34.  
  35. if (!$cache->ERROR) { 
  36. // return cache HIT, MISS, or STALE 
  37. $cache_status = $cache->check_cache( $url ); 
  38.  
  39. // if object cached, and cache is fresh, return cached obj 
  40. if ( $cache_status == 'HIT' ) { 
  41. $rss = $cache->get( $url ); 
  42. if ( isset($rss) and $rss ) { 
  43. $rss->from_cache = 1; 
  44. if ( MAGPIE_DEBUG > 1) { 
  45. debug("MagpieRSS: Cache HIT", E_USER_NOTICE); 
  46. return $rss; 
  47.  
  48. // else attempt a conditional get 
  49.  
  50. // set up headers 
  51. if ( $cache_status == 'STALE' ) { 
  52. $rss = $cache->get( $url ); 
  53. if ( isset($rss->etag) and $rss->last_modified ) { 
  54. $request_headers['If-None-Match'] = $rss->etag; 
  55. $request_headers['If-Last-Modified'] = $rss->last_modified; 
  56.  
  57. $resp = _fetch_remote_file( $url, $request_headers ); 
  58.  
  59. if (isset($resp) and $resp) { 
  60. if ($resp->status == '304' ) { 
  61. // we have the most current copy 
  62. if ( MAGPIE_DEBUG > 1) { 
  63. debug("Got 304 for $url"); 
  64. // reset cache on 304 (at minutillo insistent prodding) 
  65. $cache->set($url, $rss); 
  66. return $rss; 
  67. elseif ( is_success( $resp->status ) ) { 
  68. $rss = _response_to_rss( $resp ); 
  69. if ( $rss ) { 
  70. if (MAGPIE_DEBUG > 1) { 
  71. debug("Fetch successful"); 
  72. // add object to cache 
  73. $cache->set( $url, $rss ); 
  74. return $rss; 
  75. else { 
  76. $errormsg = "Failed to fetch $url. "; 
  77. if ( $resp->error ) { 
  78. # compensate for Snoopy's annoying habbit to tacking 
  79. # on '\n' 
  80. $http_error = substr($resp->error, 0, -2); 
  81. $errormsg .= "(HTTP Error: $http_error)"; 
  82. else { 
  83. $errormsg .= "(HTTP Response: " . $resp->response_code .')'; 
  84. else { 
  85. $errormsg = "Unable to retrieve RSS file for unknown reasons."; 
  86.  
  87. // else fetch failed 
  88.  
  89. // attempt to return cached object 
  90. if ($rss) { 
  91. if ( MAGPIE_DEBUG ) { 
  92. debug("Returning STALE object for $url"); 
  93. return $rss; 
  94.  
  95. // else we totally failed 
  96. //error $errormsg ); 
  97.  
  98. return false; 
  99.  
  100. } // end if ( !MAGPIE_CACHE_ON ) { 
  101. } // end fetch_rss()