WordPress.org

Ready to get started?Download WordPress

Forums

How do I extend fetch_feed timeout? (2 posts)

  1. Strictly Software
    Member
    Posted 1 year ago #

    I am trying to load an RSS feed from betfair

    http://rss.betfair.com/RSS.aspx?format=rss&sportID=7

    But I keep getting an error when I use the WP Error object that it is timing out.

    I have a backup source to load similar data from but it is not as complete. Therefore I want to be able to extend the time the feed is allowed to load e.g even up to 30 seconds if possible as I can then cache it for future use.

    The exact code is

    $rss = fetch_feed('http://rss.betfair.com/RSS.aspx?format=rss&sportID=7');
    if (!is_wp_error( $rss ) ) : // Checks that the object is created correctly
       RSSDebug("no error");
       // Figure out how many total items there are, but limit it to 20.
       $maxitems = $rss->get_item_quantity(20); 
    
       RSSDebug("get 20 items");
    
       // Build an array of all the items, starting with element 0 (first element).
       $rss_items = $rss->get_items(0, $maxitems);
       $count = 0;
       $max = 10;
    else:
       RSSDebug("couldnt load feed!");
       RSSDebug("Error is " . $rss->get_error_message());
    endif;

    Where RSSDebug just outputs a string to the HTML

    The messages I get are:

    couldnt load feed!
    Error is WP HTTP Error: Operation timed out after 10000 milliseconds with 0 bytes received.

    10 seconds seems to be too slow at times so I need longer and I am having to rely on another source which is not so good.

    I thought there would have been some overloaded constructor method to use which I could pass in a timeout but I cannot find anything online.

    Any help would be appreciated as I am debating just using a file_get_contents/my own xml parsing code - Thanks

  2. Strictly Software
    Member
    Posted 1 year ago #

    As a quick fix I just went straight for the SimplePie code that the WordPress code is wrapping around.

    $feed = new SimplePie();
    $feed->set_feed_url('http://rss.betfair.com/RSS.aspx?format=rss&sportID=7');
    $feed->set_timeout(30); // set to 30 seconds
    $feed->set_item_limit(40);
    $feed->set_stupidly_fast(true);
    $feed->enable_cache(true);
    $feed->set_cache_duration(200);
    $feed->init();
    $feed->handle_content_type();
    
    if (!$feed->error()):
    	RSSDebug("no error");
    	$rss_items = $feed->get_items(0, 20);
    	$count = 0;
    	$max = 10;
    	RSSDebug("rss items");
    else:
    	RSSDebug("couldnt load feed!");
    	RSSDebug("Error is " . $feed->error());
    endif;

    So I get any error message and can extend the timeout to 30 seconds.

    I still use my backup feed but at least I can get my main results if possible.

Topic Closed

This topic has been closed to new replies.

About this Topic