function testAtom03() { $uri = 'http://plnet.jp/test_feed/blogger-atom03.xml'; $feed = new FeedParser($uri); $feed->parse(); $this->assertEquals('p0t blogger', $feed->getTitle()); $this->assertEquals('http://komagata.blogspot.com', $feed->getLink()); $this->assertEquals('http://komagata.blogspot.com/favicon.ico', $feed->getFavicon()); //print_r($feed->data); }
function execute(&$controller, &$request, &$user) { $uri = $request->getParameter('uri'); $member = $user->getAttribute('member', GLU_NS); $redirect = $request->hasParameter('redirect') ? $request->getParameter('redirect') : null; $cc_id = $this->getContentCategoryIdByUri($uri) ? $this->getContentCategoryIdByUri($uri) : 8; // get feed $f = new FeedParser($uri); $parse_result = $f->parse(); if ($parse_result === false) { trigger_error(__CLASS__ . '::' . __FUNCTION__ . '(): ' . "Failed to parse feed uri: {$uri}", E_USER_NOTICE); header('Content-Type: text/plain; charset=utf-8'); echo 'false'; return VIEW_NONE; } $name = $f->getTitle(); $favicon = $f->getFavicon() ? $f->getFavicon() : SCRIPT_PATH . 'images/favicon.ico'; $db =& DBUtils::connect(false); $db->query('BEGIN'); // get feedId $feedId = $this->getFeedIdByUri($uri); if ($feedId === false) { // add feed $fields = array('uri' => $uri, 'link' => $f->getLink(), 'title' => $f->getTitle(), 'description' => $f->getDescription(), 'favicon' => $favicon, 'lastupdatedtime' => date("Y-m-d H:i:s")); $res = $db->autoExecute('feed', $fields, DB_AUTOQUERY_INSERT); if (DB::isError($res)) { $db->rollback(); trigger_error(__CLASS__ . '::' . __FUNCTION__ . '(): ' . "Failed to insert. " . $res->toString(), E_USER_ERROR); header('Content-Type: text/plain; charset=utf-8'); echo 'false'; return VIEW_NONE; } // specific mysql $feedId = $db->getOne('SELECT LAST_INSERT_ID()'); } // add member_to_feed $m2f_fields = array('member_id' => MemberUtils::get_id_by_account($member->account), 'feed_id' => $feedId); if (DBUtils::get('member_to_feed', $m2f_fields)) { echo "'already exists'"; // already exists return VIEW_NONE; } $res = $db->autoExecute('member_to_feed', $m2f_fields, DB_AUTOQUERY_INSERT); if (DB::isError($res)) { $db->rollback(); trigger_error(__CLASS__ . '::' . __FUNCTION__ . '(): ' . "Failed to insert m2f. " . $res->toString(), E_USER_WARNING); header('Content-Type: text/plain; charset=utf-8'); echo 'false'; return VIEW_NONE; } // add member_to_content_category_to_feed $m2cc2f_fields = array('member_id' => $member->id, 'content_category_id' => $cc_id, 'feed_id' => $feedId); $sql = "SELECT count(*)\n FROM member_to_content_category_to_feed\n WHERE member_id = ?\n AND content_category_id = ?\n AND feed_id = ? "; if ($db->getOne($sql, array_values($m2cc2f_fields)) == 0) { $res = $db->autoExecute('member_to_content_category_to_feed', $m2cc2f_fields, DB_AUTOQUERY_INSERT); if (DB::isError($res)) { $db->rollback(); trigger_error(__CLASS__ . '::' . __FUNCTION__ . '(): ' . 'Failed to insert m2cc2f. ' . $res->toString(), E_USER_WARNING); header('Content-Type: text/plain; charset=utf-8'); echo 'false'; return VIEW_NONE; } } // try to crawl $crawler =& new Crawler(); $crawl_result = $crawler->crawl($uri); if ($crawl_result === false) { $db->rollback(); trigger_error("AddFeedAction::execute(): Failed to crawl: {$uri}", E_USER_NOTICE); header('Content-Type: text/plain; charset=utf-8'); echo 'false'; return VIEW_NONE; } $db->commit(); if ($redirect) { Controller::redirect(SCRIPT_PATH . 'setting/feed'); } else { header('Content-Type: text/plain; charset=utf-8'); echo 'true'; } return VIEW_NONE; }
<?php // The only thing we need is include base class file require_once 'FeedParser.php'; // Get XML serialization of feed $xml = file_get_contents($_POST['filename']); // This is great. To work with feed we invoke only base class. All other work is // transparent. $feed = new FeedParser($xml); //Because we have interface for feeds, we invoke interface methods echo '<b>Type:</b>' . $feed->getFeedType() . "<br/>"; echo '<b>Title:</b>' . $feed->getTitle() . "<br/>"; echo '<b>Description:</b>' . $feed->getDescription() . "<br/>"; echo '<b>Feed link:</b>' . $feed->getFeedLink() . "<br/>"; echo '<b>Link:</b>' . $feed->getLink() . "<br/>"; $items = $feed->getItems(); // Stuff in your items can be empty, so you should somehow handle it. // I've prepared is_empty function for you - enjoy. $i = 1; foreach ($items as $item) { //Because we have interface for items, we invoke interface methods echo "<h1>"; if (is_empty($item->getLink())) { echo '<a href="#">'; } else { echo '<a href="' . $item->getLink() . '">'; } if (is_empty($item->getTitle())) { echo "No title"; } else { echo "{$i}. " . $item->getTitle();