/** * 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->channel) || !isset($XML->channel->title) || !isset($XML->channel->link) || !isset($XML->channel->description) || !isset($XML->channel->item)) { return false; } // loop items foreach ($XML->channel->item as $item) { // validate items if (!SpoonFeedRSSItem::isValid($item)) { return false; } } // fallback return true; }