fetch_rss( string $url )

Build Magpie object based on RSS from URL.


Description Description


Parameters Parameters

$url

(string) (Required) URL to retrieve feed


Top ↑

Return Return

(bool|MagpieRSS) false on failure or MagpieRSS object on success.


Top ↑

Source Source

File: wp-includes/rss.php

412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
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 habbit 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()

Top ↑

Changelog Changelog

Changelog
Version Description
1.5.0 Introduced.


Top ↑

User Contributed Notes User Contributed Notes

You must log in before being able to contribute a note or feedback.