Exemplo n.º 1
0
 /**
  * Checks if the feed is valid.
  *
  * @return	bool
  * @param	string $URL					An URL where the feed is located or the XML of the feed.
  * @param	string[optional] $type		The type of feed, possible values are: url, string.
  */
 public static function isValid($URL, $type = 'url')
 {
     // redefine var
     $URL = (string) $URL;
     $type = (string) SpoonFilter::getValue($type, array('url', 'string'), 'url');
     // validate
     if ($type == 'url' && !SpoonFilter::isURL($URL)) {
         throw new SpoonFeedException('This (' . $URL . ') isn\'t a valid url.');
     }
     // load xmlstring
     if ($type == 'url') {
         // check if allow_url_fopen is enabled
         if (ini_get('allow_url_fopen') == 0) {
             throw new SpoonFeedException('allow_url_fopen should be enabled, if you want to get a remote URL.');
         }
         // open the url
         $handle = @fopen($URL, 'r');
         // validate the handle
         if ($handle === false) {
             throw new SpoonFeedException('Something went wrong while retrieving the URL.');
         }
         // read the string
         $xmlString = @stream_get_contents($handle);
         // close the hanlde
         @fclose($handle);
     } else {
         $xmlString = $URL;
     }
     // convert to simpleXML
     $XML = @simplexml_load_string($xmlString);
     // invalid XML?
     if ($XML === false) {
         return false;
     }
     // check if all needed elements are present
     if (!isset($XML->title) || !isset($XML->id) || !isset($XML->entry)) {
         return false;
     }
     // loop items
     foreach ($XML->entry as $item) {
         // validate items
         if (!SpoonFeedAtomRSSItem::isValid($item)) {
             return false;
         }
     }
     // fallback
     return true;
 }