EzDevInfo.com

simplepie

A simple Atom/RSS parsing library for PHP SimplePie: Super-fast, easy-to-use, RSS and Atom feed parsing in PHP. a super-fast, easy-to-use, rss and atom parser written in php.

Checking Valid RSS feed URL

I have decided to use SimplePie to parse RSS and Atom Feeds.

What I want to do is that let people to input RSS and Atom Feeds URL through text fields.

What If they put invalid RSS and Atom Feeds?

I know that invalid Feeds won't be parsed through SimplePie.

But I want to know whether the Feeds are able to be parsed through SimplePie or not.

And through that process, I want to delete those invalid RSS feed URL lists.

Checking the document type, XML or HTML will be a first step to find out the validness.

How can I do that in PHP? or is there other ways to do what I want to do?


Source: (StackOverflow)

Missing Get_favicon function in new Simplepie Release

In the new release the get_favicon method was removed as shown in the Changes in this Release section of documentation.

When I try $item->get_favicon() I only get Call to undefined method SimplePie_Item::get_favicon()

How am I able to get the favicons from the RSS Feeds?


Source: (StackOverflow)

Advertisements

SimplePie html rssfeeds Codeigniter

I wanted to ask, I have a div that I would want to place RSS feeds from the BBC or CNN. Now that works but I wanted to paginate the feeds that come in, display 5 at a time with the others showing when the links are clicked.

I am starting to write the code now but I was hoping I would get either inspiration or have an example I could glean from.


Source: (StackOverflow)

How to filter/block RSS feed items with SimplePie

I've got a google news feed I display in my WordPress site, using the following code:

$feed = fetch_feed($rss_url); // specify the source feed
$limit = $feed->get_item_quantity(20); // specify number of items
$items = $feed->get_items(0, $limit); // create an array of items
foreach ($items as $item) : 
    echo $item->get_description(); 
endforeach;

Problem is, certain individual articles I need to filter out. Google news items have guid tags. Given the guid of the item, how can I tell SimplePie to ignore the given item?

Thanks-


Source: (StackOverflow)

Getting image url from RSS feed using simplepie

I am very new to php and simplepie. I would like to be able to use simplepie to store an image url to a variable. For my example, I will use the ebay rss feed (http://deals.ebay.com/feeds/rss). The image that I am trying to get the url of is in a <image src= tag. When i use the code

foreach ($feed->get_items() as $item):
?>
<?php echo $item->get_description(); ?>
<?php endforeach; ?>

The image and description are shown, but I am unable to store the image url to a variable. How can I use simplepie to store an image url to a variable?

Thanks


Source: (StackOverflow)

PHP: Ignoring errors and notices in a specific class

My site is completely custom, as such I like to know when I have poorly written code. I use set_exception_handler and set_error_handler to use custom classes to log errors to a file. This includes notices and warnings.

Within my own code, this is fine as I get very few logs and those that I do get are things I actually want to fix.

However, i've just started using simplePie and because it's PHP4 compatible I get tons of notices, primarily for things like calling functions statically or passing things by reference incorrectly.

It's too much for me to go through and fix simplePie, if it wasn't I wouldn't be using it in the first place.

Is there a way that I can specifically ignore errors generated by a certain file or class? Here's an overview of what of my very basic exception handling looks like:

    set_exception_handler("CustomExceptionHandler");
    set_error_handler("customErrorHandler");

    /**
     * CustomExceptionHandler()
     *
     * This is used if an exception is thrown that isn't caught.
     *
     * @param   object  $e  The exception as an object
     */
    function CustomExceptionHandler(Exception $e) {
        exitToError($e->getMessage());
    }

    /**
     * customErrorHandler()
     *
     * This is called for any errors no matter what their level.
     */
    function customErrorHandler($errno, $errstr, $errfile, $errline) {
        if(in_array($errno, array(E_USER_ERROR, E_RECOVERABLE_ERROR))) {
            throw new CustomErrorException($errstr, 0, $errno, $errfile, $errline);
        } else {
            CustomException::logError($errstr, $errno, $errfile, $errline);
        }
        return FALSE;
    }

/**
     * class CustomErrorException
     *
     * Used by custom_error_handler() to convert all fatal
     * errors to exceptions.
     *
     * @see custom_error_handler()
     * @see http://www.php.net/manual/en/class.errorexception.php
     */
    class CustomErrorException extends CustomException {
        /**
         * $severity
         *
         * The severity level of the exception
         *
         * @access  protected
         * @var     int
         */
        protected $severity;

        /**
         * __construct()
         *
         * Constructs the new exception
         *
         * @access  public
         * @param   string  $message    The Exception message
         * @param   int     $code       The Exception code
         * @param   int     $severity   The severity level of the exception
         * @param   string  $filename   The filename where the exception was thrown
         * @param   int     $lineno     The line number where the exception was thrown
         */
        public function __construct($message, $code = null, $severity = E_ERROR, $filename = null, $lineno= null) {
            $this->message  = $message;
            $this->code     = $code;
            $this->severity = (int)$severity;
            $this->file     = $filename;
            $this->line     = $lineno;

            self::logError($this->message,$this->code,$this->file,$this->line,$this->getTraceAsString());
        }
    }

    /**
     * class CustomException
     *
     * Overwrites Exception to give us more control on how
     * exceptions are handled and logged.
     *
     * @see http://www.php.net/manual/en/language.exceptions.extending.php
     */
    class CustomException extends Exception {

        /**
         * __construct
         *
         * We call the parent contruct as we still want it to do all its magic. We just want
         * overwrite this method so that we can log the error exactly how we want.
         */
        public function __construct($message, $code = 0, Exception $previous = NULL) {
            parent::__construct($message, $code);
            self::logError($this->getMessage(),$this->getCode(),$this->getFile(),$this->getLine(),$this->getTraceAsString());
        }

        /**
         * __toString()
         *
         * We overwrite this function so that we can use our stringBuilder function.
         */
        public function __toString() {
            return self::stringBuilder($this->getMessage(),$this->getCode(),$this->getFile(),$this->getLine(),$this->getTraceAsString());
        }

        /**
         * stringBuilder()
         *
         * We use this method so that we have a standard method of building error
         * strings that anything can tap into.
         *
         * @access  public
         * @param   string  $message    the exception message
         * @param   int     $code       the code assigned to this exception
         * @param   string  $file       the file where the exception occurred
         * @param   int     $line       the line where the exception occurred
         * @param   string  $trace      backtrace
         */
        public function stringBuilder($message, $code, $file, $line, $trace='') {
            //return "[".date("d-M-Y H:i:s")."] ".$this->getMessage()." in ".$this->getFile().":".$this->getLine()."\nStack trace:\n".$this->getTraceAsString()."\n";
            return "[".date("d-M-Y H:i:s")."] ".$message." in ".$file.":".$line."\n";
        }

        /**
         * logError()
         *
         * We use a method so that we have a standard way of saving errors
         * to a log.
         *
         * We use XML because it's easy to parse.
         *
         * @access  public
         * @param   string  $message    the exception message
         * @param   int     $code       the code assigned to this exception
         * @param   string  $file       the file where the exception occurred
         * @param   int     $line       the line where the exception occurred
         * @param   string  $trace      backtrace
         * @todo    We could improve it to write to the xml file using DomDocument
         *          as laid out here http://www.xml-training-guide.com/append-delete-data-from-xml-using-php.html
         */
        public function logError($message, $code, $file, $line, $trace='') {
            //Save it to a standard text file to guarentee saving the error
            file_put_contents(ROOT_URL.ERROR_LOG_TXT,self::stringBuilder($message, $code, $file, $line, $trace),FILE_APPEND);
        }
    }

and here is an example of two of the errors simplePie throws up:

[01-Aug-2010 00:50:33] Assigning the return value of new by reference is deprecated in ***\SimplePie.php:738
[01-Aug-2010 00:50:34] Non-static method SimplePie_Misc::parse_date() should not be called statically in ***\SimplePie.php:60

Source: (StackOverflow)

Simplepie RSS feeds to MySQL database server

I am currently using Simplepie to pull in my RSS feeds as shown in the configuration below. I want to move the $url to my database because my site loads way too slow. I have each url as a key value pair to the site's name. I want to keep this association because I use for instance "abc" to pull the image out of my directory which I use to style it for each feed, as you can see in the foreach loop below.

My question is, since I am not that clear on how arrays and tables work together, how would I rewrite this script to work with the database the same way?

I should also mention that I have already made a table in MySQL with the rows "id" "name" and "url". Any clarification will help.

<?php
require_once('php/autoloader.php');
$feed = new SimplePie();
// Create a new instance of SimplePie
// Load the feeds
$urls = array(
  'http://abcfamily.go.com/service/feed?id=774372' => 'abc',
  'http://www.insideaolvideo.com/rss.xml' => 'aolvideo',
  'http://feeds.bbci.co.uk/news/world/rss.xml' => 'bbcwn',
  'http://www.bing.com' => 'bing',
  'http://www.bravotv.com' => 'bravo',
  'http://www.cartoonnetwork.com' => 'cartoonnetwork',
  'http://feeds.cbsnews.com/CBSNewsMain?format=xml' => 'cbsnews',
  'http://www.clicker.com/' => 'clicker',
  'http://feeds.feedburner.com/cnet/NnTv?tag=contentBody.1' => 'cnet',
  'http://www.comedycentral.com/' => 'comedycentral',
  'http://www.crackle.com/' => 'crackle',
  'http://www.cwtv.com/feed/episodes/xml' => 'cw',
  'http://disney.go.com/disneyxd/' => 'disneyxd',
  'http://www.engadget.com/rss.xml' => 'engadget',
  'http://syndication.eonline.com/syndication/feeds/rssfeeds/video/index.xml' => 'eonline',
  'http://sports.espn.go.com/espn/rss/news' => 'espn',
  'http://facebook.com' => 'facebook',
  'http://flickr.com/espn/rss/news' => 'flickr',
  'http://www.fxnetworks.com//home/tonight_rss.php' => 'fxnetworks',
  'http://www.hgtv.com/' => 'hgtv',
  'http://www.history.com/this-day-in-history/rss' => 'history',
  'http://rss.hulu.com/HuluRecentlyAddedVideos?format=xml' => 'hulu',
  'http://rss.imdb.com/daily/born/' => 'imdb',
  'http://www.metacafe.com/' => 'metacafe',
  'http://feeds.feedburner.com/Monkeyseecom-NewestVideos?format=xml' => 'monkeysee',
  'http://pheedo.msnbc.msn.com/id/18424824/device/rss/' => 'msnbc',
  'http://www.nationalgeographic.com/' => 'nationalgeographic',
  'http://dvd.netflix.com/NewReleasesRSS' => 'netflix',
  'http://feeds.nytimes.com/nyt/rss/HomePage' => 'newyorktimes',
  'http://www.nick.com/' => 'nickelodeon',
  'http://www.nickjr.com/' => 'nickjr',
  'http://www.pandora.com/' => 'pandora',
  'http://www.pbskids.com/' => 'pbskids',
  'http://www.photobucket.com/' => 'photobucket',
  'http://feeds.reuters.com/Reuters/worldNews' => 'reuters',
  'http://www.revision3.com/' => 'revision3',
  'http://www.tbs.com/' => 'tbs',
  'http://www.theverge.com/rss/index.xml' => 'theverge',
  'http://www.tntdrama.com/' => 'tnt',
  'http://www.tvland.com/' => 'tvland',
  'http://www.vimeo.com/' => 'vimeo',
  'http://www.vudu.com/' => 'vudu',
  'http://feeds.wired.com/wired/index?format=xml' => 'wired',
  'http://www.xfinitytv.com/' => 'xfinitytv',
  'http://www.youtube.com/topic/4qRk91tndwg/most-popular#feed' => 'youtube',
);
$feed->set_feed_url(array_keys($urls));
$feed->enable_cache(true);
$feed->set_cache_location('cache');
$feed->set_cache_duration(1800); // Set the cache time
$feed->set_item_limit(1);
$success = $feed->init(); // Initialize SimplePie
$feed->handle_content_type(); // Take care of the character encoding
?>
<?php require_once("inc/connection.php"); ?>
<?php require_once("inc/functions.php"); ?>
<?php include("inc/header.php"); ?>
<?php
// Sort it
$feed_items = array();
// $feed_items is an array
$items = $feed->get_items();
//$items is everything that $items = $feed->get_items(); produces
$urls = array_unique($urls);
// $url = is an empty $
foreach ($urls as $url => $image) {
  $unset = array();
  $feed_items[$url] = array();
  foreach ($items as $i => $item) {
    if ($item->get_feed()->feed_url == $url) {
      $feed_items[$url][] = $item;
      $unset[] = $i;
    }
  }
  foreach ($unset as $i) {
    unset($items[$i]);
  }
}
foreach ($feed_items as $feed_url => $items) {
  if (empty($items)) { ?>
  <div class="item element" data-symbol="<?php echo $urls[$feed_url] ?>" name="<?php echo $urls[$feed_url] ?>">
  <div class="minimise"><img src="images/boreds/<?php echo $urls[$feed_url] ?>.png"/>
  <div class="minimise2">
    <a rel='nofollow' href="<?php echo $feed_url; ?>"><h2>Visit <?php echo $urls[$feed_url] ?> now!</h2></a>
  </div></div>
  <div class="maximise">
    <a rel='nofollow' href="<?php echo $feed_url; ?>"><h2>Visit <?php echo $urls[$feed_url] ?> now!</h2></a>
  </div></div>

  <?
    continue;
  }
  $first_item = $items[0];
  $feed = $first_item->get_feed();
  ?>

  <?php

$feedCount = 0;
foreach ($items as $item) {
  $feedCount++;
  ?>
<div class="item element" " data-symbol="<?php echo $urls[$feed_url] ?>" name="<?php echo $urls[$feed_url] ?>">
<div class="minimise"><strong id="amount"><?php echo ''.$feedCount; ?></strong>
  <img src="images/boreds/<?php echo $urls[$feed_url] ?>.png"/>
  <div class="minimise2"><a rel='nofollow' href="<?php echo $item->get_permalink(); ?>">
  <h2><?php echo $item->get_title(); ?></h2></a>
</div></div>
<div class="maximise"><a rel='nofollow' href="<?php echo $item->get_permalink(); ?>">
   <h2><?php echo $item->get_title(); ?></h2></a><br><p><?php echo $item->get_description(); ?></p>
</div></div>
<?php
  }
}
?>
<?php require("inc/footer2.php"); ?>

Source: (StackOverflow)

PHP way of parsing HTML string

I have a php string that contains the below HTML I am retrieving from an RSS feed. I am using simple pie and cant find any other way of splitting these two datasets it gets from <description>. If anyone knows of a way in simple pie to select children that would be great.

<div style="example"><div style="example"><img title="example" alt="example" src="example.jpg"/></div><div style="example">EXAMPLE TEXT</div></div>

to:

$image = '<img title="example" alt="example" src="example.jpg">';
$description = 'EXAMPLE TEXT';

Source: (StackOverflow)

How to inject multiple classes that share the same interface in Laravel 4

Say I have an interface CrawlerInterface with implementation PageCrawler and FeedCrawler; if we happen to need both classes in a controller, how can that be achieved with constructor injection?

Previously we use a central ServiceProvider to register (i.e. App::bind) such classes, but in most cases we only have 1 implementation of an interface, so said problem hasn't occured to us yet.

PS: I also wonder if this problem suggests we should split the controller.


Updates:

Thanks for the comments and response, to explain, said interface has only one public method: crawl($uri), and both page/feed crawler implements it as given a resource identifier, return resource.


My follow up question:

Say we are in a calculator scenario where Addition, Subtraction and Multiplication share the same interface Operation, which has only 1 public method run, at some point we will still encounter this problem right? How do we handle situation like these in general with ServiceProvider?


Source: (StackOverflow)

Why am I getting memory leaks in SimplePie when using $item->get_permalink()?

I'm using SimplePie with PHP 5.3 (with gc enabled) to parse my RSS feeds. This works well and without problems when doing something like the following:

$simplePie = new SimplePie();
$simplePie->set_feed_url($rssURL);
$simplePie->enable_cache(false);
$simplePie->set_max_checked_feeds(10);
$simplePie->set_item_limit(0);
$simplePie->init();
$simplePie->handle_content_type();

foreach ($simplePie->get_items() as $key => $item) {
    $item->get_date("Y-m-d H:i:s");
    $item->get_id();
    $item->get_title();
    $item->get_content();
    $item->get_description();
    $item->get_category();
}

Memory debugging over 100 iterations (with different RSS feeds):

SimplePie without using get_permalink()

But when using $item->get_permalink(), my memory debugging looks like this over 100 iterations (with different RSS feeds).

Code to produce problem:

foreach ($simplePie->get_items() as $key => $item) {
    $item->get_date("Y-m-d H:i:s");
    $item->get_id();
    $item->get_title();
    $item->get_permalink(); //This creates a memory leak
    $item->get_content();
    $item->get_description();
    $item->get_category();
}

SimplePie get_permalink memory leak

Things I've tried:

  • Using get_link instead of get_permalink
  • Using __destroy as mentioned here (even though it should be fixed for 5.3)

Current debugging process:

I seem to have traced the problem down to SimplePie_Item::get_permalink -> SimplePie_Item::get_link -> SimplePie_Item::get_links -> SimplePie_Item::sanitize -> SimplePie::sanitize -> SimplePie_Sanitize::sanitize -> SimplePie_Registry::call -> SimplePie_IRI::absolutize as of now.

What can I do to fix this?


Source: (StackOverflow)

Split an RSS feed into separate feeds

I'm trying to find a way to split a single RSS feed into separate feeds based on keyword matches.

Yahoo Pipes was my first thought, but I couldn't find a way to do it. Although Pipes has a split module, there is only one output module available for any pipe. So I can't see how I would ever get more than one feed from a pipe.

The whole point in doing this is to make a single dense feed much more manageable in Google Reader. (Hey Google: How about some Gmail-style filters?!)

[Clarification Start]

I envisioned a main feed coming in and then filters building 'sub-feeds' based on certain keywords. The catch is that I would subscribe to both the filtered main feed and the 'sub-feeds.' The filters decrease the number of items in the main feed. They are 'filtered off.' I would subscribe to the filtered main feed in order to watch for other sub-feeds that need to be pulled off in the future.

The main issue with setting up separate Pipes is that I would need to maintain filters for each sub-feed and then I would also have to duplicate those filters to apply the whole set to the main feed. It's doable, but not ideal. I may not have any other option.

[Clarification End]

My second thought was to possibly use Pipes to apply tags to the feed items hoping that Google Reader would honor those tags. This doesn't seem possibly either.

I took a look at MagpieRSS and Simplepie to consider rolling my own solution. I found plenty of documentation for merging feeds, but nothing along the lines of splitting feeds. My skills are pretty basic, so without a pretty specific tutorial, this may be out of my league.

If anyone has ideas, I'd be grateful. Thanks.


Source: (StackOverflow)

How to get custom data from wordpress RSS with SimplePie

I have made some changes to a RSS feed in my Wordpress, and I'm using fetch_feed() to show data to another website. Imagine there are 2 websites called #Wordpress1 and #Wordpress2. This is the code i've added to #wordpress1's functions.php file

add_action('rss2_item', 'dw_add_data_to_rss');
function dw_add_data_to_rss(){
    global $post;

    if( $post->post_type == 'product' ) {
        $product = new WC_Product( $post->ID );

        $output = '';
        $thumbnail_ID = get_post_thumbnail_id( $post->ID );
        $thumbnail = wp_get_attachment_image_src($thumbnail_ID, 'thumbnail');
        $output  = '<post-thumbnail>';
        $output .= '<url>'. $thumbnail[0] .'</url>';
        $output .= '<width>'. $thumbnail[1] .'</width>';
        $output .= '<height>'. $thumbnail[2] .'</height>';
        $output .= '</post-thumbnail>';

        $output .= '<price>' . number_format( $product->get_price() ) . ' ' . get_woocommerce_currency_symbol() . '</price>';

        echo $output;
    }
}

this code adds product price and thumbnail to Rss feed now we need to display these data on #Wordpress2 , but i don't know how to do it

$rss = fetch_feed( 'http://localhost/wp/feed/?post_type=product' );
if ( ! is_wp_error( $rss ) ) {
    $maxitems  = $rss->get_item_quantity( 10 ); 
    $rss_items = $rss->get_items( 0, $maxitems );
}

foreach ( $rss_items as $item ) {
    echo '<a rel='nofollow' href="'. $item->get_permalink() .'"><img src="{MY_IMAGE_FROM_RSS}"> <span class="price">{MY_PRICE_FROM_RSS}</span></a>';
}

what should i use instead of MY_IMAGE_FROM_RSS and MY_PRICE_FROM_RSS in above code


Source: (StackOverflow)

Static function caches that aren't being cleaned in PHP 5.3.28?

I'm using SimplePie with PHP 5.2.17 to parse my RSS feeds through the WordPress plugin feedwordpress. This works well and without problems if I make sure to use this patch to simplepie (in the file IRI.php).

However, if I change my PHP to use version 5.3.28 - the memory leak (or some other memory leak) starts and my site crashes. Any idea what might be causing it / how to solve it?

(or in other words, is there a reason this patch should work in 5.2 and not in PHP 5.3?)

Thanks.


Source: (StackOverflow)

Getting deprecated error with Simplepie

I have installed the latest Simplepie code (1.2.1) and I am using the demo code they provide:

<?php

require 'simplepie.inc';

$url = 'http://news.google.com/news?ned=us&topic=h&output=rss';

$feed = new SimplePie();
$feed->set_feed_url($url);
$feed->init();

// default starting item
$start = 0;

// default number of items to display. 0 = all
$length = 0; 

// if single item, set start to item number and length to 1
if(isset($_GET['item']))
{
    $start = $_GET['item'];
    $length = 1;
}

// set item link to script uri
$link = $_SERVER['REQUEST_URI'];

// loop through items
foreach($feed->get_items($start,$length) as $key=>$item)
{

    // set query string to item number
    $queryString = '?item=' . $key;

    // if we're displaying a single item, set item link to itself and set query string to nothing
    if(isset($_GET['item']))
    {
            $link = $item->get_link();
            $queryString = '';        
    }

    // display item title and date    
    echo '<a rel='nofollow' href="' . $link . $queryString . '">' . $item->get_title() . '</a>';
    echo ' <small>'.$item->get_date().'</small><br>';

    // if single item, display content
    if(isset($_GET['item']))
    {
            echo ' <small>'.$item->get_content().'</small><br>';
    }
    echo '<br>';
}

?>

However, when I load the page in my browser, I get dozens of lines saying:

Deprecated: Assigning the return value of new by reference is deprecated in /home/pliggs/public_html/rss/simplepie.inc on line 7722

Anyone know what's wrong?

I ran their compatibility test and it shows all things have passed.


Source: (StackOverflow)

Strange warning when trying to extend class

I started getting strange warnings, after extending a class that extends another Event_Feed_Item > My_Feed_Item > SimplePie_Item

- Cannot extend from interface 
 'SimplePie_Item'
- Implementors of methods in 
 'SimplePie_Item'

However on runtime it works fine. Also SimplePie_Item is not an interface, it's a simple Class.. Just need to know if I'm running into trouble, or if i can just ignore this warning

EDIT I'm extending this class
Also this is the skeleton from my classes

class My_Feed_Item extends SimplePie_Item // error is triggered here
{
    protected $_source_id = null;

    public function set_source ($source_id)
    {
    }
    public function get_image ()
    {
    }
    public function get_venue_id ()
    {
    }
    public function get_venue_nm ()
    {
    }
    public function cleanString($str)
    {
    }
}

class Event_Feed_Item extends My_Feed_Item
{
    public function get_event_nm ()
    {

    }
    public function get_event_start_dt ()
    {

    }
    public function get_event_end_dt ()
    {
    }

}

Source: (StackOverflow)