public function testCanGetItemRss2OutputItem() { $this->dispatch('items/browse?output=rss2'); $string = $this->response->getBody(); try { $feed = Zend_Feed::importString($string); } catch (Zend_Feed_Exception $e) { $this->fail("Feed does not load properly."); } $item = $feed->current(); if (!$item) { $this->fail("Feed does not have an item"); } }
public function import($feed) { // download XML try { $xml = $this->_fetch($feed->url); $rss = Zend_Feed::importString($xml); } catch (Exception $e) { throw new Yadda_Feed_Exception('Error getting feed: ' . (string) $e); } $stubs = array(); // iterate over each entry foreach ($rss as $entry) { $stub = new Yadda_Feed_Stub(); $guid = $entry->guid(); if (empty($guid)) { $guid = $entry->link(); } $stub->setGuid($guid); $title = trim(html_entity_decode($entry->title(), ENT_QUOTES, 'utf-8')); $stub->setTitle($title); $stub->setLink($entry->link()); $description = trim(html_entity_decode(strip_tags($entry->description()), ENT_QUOTES, 'utf-8')); if (!empty($description)) { $stub->setDescription($description); } $date = $entry->pubDate(); if (!empty($date)) { $stub->setDate(date('Y-m-d H:i:s', strtotime($date))); } $price = $this->_getPrice($title . ' ' . (string) $description); if ($price !== null) { $stub->setPrice($price); } // images $description = $entry->description(); if (!empty($description)) { $matches = array(); preg_match_all('#src="(https?://[^"]+)"#', $description, $matches); if (isset($matches[1]) && sizeof($matches[1]) > 0) { $stub->addImage($matches[1][0]); } } $stubs[] = $stub; } return $stubs; }
public function __construct($xml = null) { if ($xml) { // first, lets find the cmis namespace to use $sx = new SimpleXMLElement($xml); $namespaces = $sx->getDocNamespaces(); $ns = isset($namespaces['cmis']) ? $namespaces['cmis'] : 'http://docs.oasis-open.org/ns/cmis/core/200901'; // go through the XML and pull out the things we're interested in Zend_Feed::registerNamespace('cmis', $ns); // 'http://www.cmis.org/2008/05'); $this->rawXml = $xml; $feed = Zend_Feed::importString($this->rawXml); foreach ($feed as $item) { $this->loadFromFeed($item); } } }
function __construct($xml) { parent::__construct(); if ($xml) { // first, lets find the cmis namespace to use $sx = new SimpleXMLElement($xml); $namespaces = $sx->getDocNamespaces(); $ns = isset($namespaces['cmis']) ? $namespaces['cmis'] : 'http://docs.oasis-open.org/ns/cmis/core/200901'; // go through the entries and create some objects Zend_Feed::registerNamespace('cmis', $ns); // 'http://www.cmis.org/2008/05'); $this->rawXml = $xml; $feed = Zend_Feed::importString($this->rawXml); foreach ($feed as $entry) { $obj = new SeaMistObject(); $obj->loadFromFeed($entry); $this[] = $obj; } } }
/** * Test the return of a link() call (atom) */ public function testAtomGetLink() { $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom'); $this->assertTrue($feed instanceof Zend_Feed_Atom); $feed = Zend_Feed::importString($feed->saveXML()); $this->assertTrue($feed instanceof Zend_Feed_Atom); $href = $feed->link('self'); $this->assertTrue($href == 'http://www.example.com'); }
/** * Performs a Twitter search query. * * @throws Zend_Http_Client_Exception */ public function search($query, array $params = array()) { $_query = array(); $_query['q'] = $query; foreach ($params as $key => $param) { switch ($key) { case 'geocode': case 'lang': case 'since_id': $_query[$key] = $param; break; case 'rpp': $_query[$key] = intval($param) > 100 ? 100 : intval($param); break; case 'page': $_query[$key] = intval($param); break; case 'show_user': $_query[$key] = 'true'; } } $response = $this->restGet('/search.' . $this->_responseType, $_query); switch ($this->_responseType) { case 'json': return Zend_Json::decode($response->getBody()); break; case 'atom': return Zend_Feed::importString($response->getBody()); break; } return; }
/** * Test the return of a link() call (atom) */ public function testAtomGetLink() { $feed = Zend_Feed::importBuilder(new Zend_Feed_Builder($this->_getFullArray()), 'atom'); $this->assertType('Zend_Feed_Atom', $feed); $feed = Zend_Feed::importString($feed->saveXml()); $this->assertType('Zend_Feed_Atom', $feed); $href = $feed->link('self'); $this->assertEquals('http://www.example.com', $href); }
/** * Handles (RSS & Atom) Type.FEED parsing using Zend's feed parser * * @param RemoteContentRequest $result * @param string $url * @param int $numEntries * @param boolean $getSummaries * @return response string, either a json encoded feed structure or an error message */ private function parseFeed($result, $url, $numEntries = 3, $getSummaries = false) { require 'external/Zend/Feed.php'; $channel = array(); if ((int) $result->getHttpCode() == 200) { $content = $result->getResponseContent(); try { $feed = Zend_Feed::importString($content); if ($feed instanceof Zend_Feed_Rss) { // Try get author if ($feed->author()) { $author = $feed->author(); } else { if ($feed->creator()) { $author = $feed->creator(); } else { $author = null; } } // Loop over each channel item and store relevant data $counter = 0; $channel['Entry'] = array(); foreach ($feed as $item) { if ($counter >= $numEntries) { break; } $_entry = array(); $_entry['Title'] = $item->title(); $_entry['Link'] = $item->link(); if (!is_string($_entry['Link']) && isset($_entry['Link'][1]) && $_entry['Link'][1] instanceof DOMElement) { $_entry['Link'] = $_entry['Link'][1]->getAttribute('href'); } if ($getSummaries && $item->description()) { $_entry['Summary'] = $item->description(); } $date = 0; if ($item->date()) { $date = strtotime($item->date()); } else { if ($item->pubDate()) { $date = strtotime($item->pubDate()); } } $_entry['Date'] = $date; $channel['Entry'][] = $_entry; // Remember author if first found if (empty($author) && $item->author()) { $author = $item->author(); } else { if ($item->creator()) { $author = $item->creator(); } } $counter++; } $channel['Title'] = $feed->title(); $channel['URL'] = $url; $channel['Description'] = $feed->description(); if ($feed->link()) { if (is_array($feed->link())) { foreach ($feed->link() as $_link) { if ($_link->nodeValue) { $channel['Link'] = $_link->nodeValue; } } } else { $channel['Link'] = $feed->link(); } } if ($author != null) { $channel['Author'] = $author; } } elseif ($feed instanceof Zend_Feed_Atom) { // Try get author if ($feed->author()) { if ($feed->author->name()) { $author = $feed->author->name(); } else { if ($feed->author->email()) { $author = $feed->author->email(); } else { $author = $feed->author(); } } } else { $author = null; } // Loop over each entries and store relevant data $counter = 0; $channel['Entry'] = array(); foreach ($feed as $entry) { if ($counter >= $numEntries) { break; } $_entry = array(); $_entry['Title'] = $entry->title(); // get Link if rel="alternate" if ($entry->link('alternate')) { $_entry['Link'] = $entry->link('alternate'); } else { // if there's no alternate, pick the one without "rel" attribtue $_links = $entry->link; if (is_array($_links)) { foreach ($_links as $_link) { if (empty($_link['rel'])) { $_entry['Link'] = $_link['href']; break; } } } else { $_entry['Link'] = $_links['href']; } } if ($getSummaries && $entry->summary()) { $_entry['Summary'] = $entry->summary(); } $date = 0; if ($entry->updated()) { $date = strtotime($entry->updated()); } else { if ($entry->published()) { $date = strtotime($entry->published()); } } $_entry['Date'] = $date; $channel['Entry'][] = $_entry; // Remember author if first found if (empty($author) && $entry->author()) { if ($entry->author->name()) { $author = $entry->author->name(); } else { if ($entry->author->email()) { $author = $entry->author->email(); } else { $author = $entry->author(); } } } elseif (empty($author)) { $author = null; } $counter++; } $channel['Title'] = $feed->title(); $channel['URL'] = $url; $channel['Description'] = $feed->subtitle(); // get Link if rel="alternate" if ($feed->link('alternate')) { $channel['Link'] = $feed->link('alternate'); } else { // if there's no alternate, pick the one without "rel" attribtue $_links = $feed->link; if (is_array($_links)) { foreach ($_links as $_link) { if (empty($_link['rel'])) { $channel['Link'] = $_link['href']; break; } } } else { $channel['Link'] = $_links['href']; } } if (!empty($author)) { $channel['Author'] = $author; } } else { throw new Exception('Invalid feed type'); } $resp = json_encode($channel); } catch (Zend_Feed_Exception $e) { $resp = 'Error parsing feed: ' . $e->getMessage(); } } else { // feed import failed $resp = "Error fetching feed, response code: " . $result->getHttpCode(); } return $resp; }
/** * Fetches content and returns it in JSON format * * @param string $url the url to fetch * @param GadgetSigner $signer the request signer to use * @param string $method the http method to use (get or post) in making the request */ public function fetchJson($url, $signer, $method) { try { $token = $this->context->extractAndValidateToken($signer); } catch (Exception $e) { $token = ''; // no token given, safe to ignore } $url = $this->validateUrl($url); // Fetch the content and convert it into JSON. // TODO: Fetcher needs to handle variety of HTTP methods. $result = $this->fetchContentDivert($url, $method, $signer); if (!isset($result)) { //OAuthFetcher only $metadata = $this->oauthFetcher->getResponseMetadata(); $json = array($url => $metadata); $json = json_encode($json); $output = UNPARSEABLE_CRUFT . $json; $this->setCachingHeaders(); header("Content-Type: application/json; charset=utf-8", true); echo $output; $this->end(); } $status = (int) $result->getHttpCode(); //header("HTTP/1.1 $status", true); if ($status == 200) { $output = ''; if (isset($_REQUEST['contentType']) && $_REQUEST['contentType'] == 'FEED') { require 'external/Zend/Feed.php'; $numEntries = $_REQUEST['numEntries']; $getSummaries = !empty($_REQUEST['getSummaries']) && $_REQUEST['getSummaries'] != 'false' ? true : false; $channel = array(); $request = new RemoteContentRequest($url); $request = $this->context->getHttpFetcher()->fetch($request, $this->context); if ((int) $result->getHttpCode() == 200) { $content = $result->getResponseContent(); try { $feed = Zend_Feed::importString($content); if ($feed instanceof Zend_Feed_Rss) { // Try get author if ($feed->author()) { $author = $feed->author(); } else { if ($feed->creator()) { $author = $feed->creator(); } else { $author = null; } } // Loop over each channel item and store relevant data $counter = 0; $channel['Entry'] = array(); foreach ($feed as $item) { if ($counter >= $numEntries) { break; } $_entry = array(); $_entry['Title'] = $item->title(); $_entry['Link'] = $item->link(); if ($getSummaries && $item->description()) { $_entry['Summary'] = $item->description(); } $date = 0; if ($item->date()) { $date = strtotime($item->date()); } else { if ($item->pubDate()) { $date = strtotime($item->pubDate()); } } $_entry['Date'] = $date; $channel['Entry'][] = $_entry; // Remember author if first found if (empty($author) && $item->author()) { $author = $item->author(); } else { if ($item->creator()) { $author = $item->creator(); } } $counter++; } $channel['Title'] = $feed->title(); $channel['URL'] = $url; $channel['Description'] = $feed->description(); if ($feed->link()) { if (is_array($feed->link())) { foreach ($feed->link() as $_link) { if ($_link->nodeValue) { $channel['Link'] = $_link->nodeValue; } } } else { $channel['Link'] = $feed->link(); } } if ($author != null) { $channel['Author'] = $author; } } elseif ($feed instanceof Zend_Feed_Atom) { // Try get author if ($feed->author()) { if ($feed->author->name()) { $author = $feed->author->name(); } else { if ($feed->author->email()) { $author = $feed->author->email(); } else { $author = $feed->author(); } } } else { $author = null; } // Loop over each entries and store relevant data $counter = 0; $channel['Entry'] = array(); foreach ($feed as $entry) { if ($counter >= $numEntries) { break; } $_entry = array(); $_entry['Title'] = $entry->title(); // get Link if rel="alternate" if ($entry->link('alternate')) { $_entry['Link'] = $entry->link('alternate'); } else { // if there's no alternate, pick the one without "rel" attribtue $_links = $entry->link; if (is_array($_links)) { foreach ($_links as $_link) { if (empty($_link['rel'])) { $_entry['Link'] = $_link['href']; break; } } } else { $_entry['Link'] = $_links['href']; } } if ($getSummaries && $entry->summary()) { $_entry['Summary'] = $entry->summary(); } $date = 0; if ($entry->updated()) { $date = strtotime($entry->updated()); } else { if ($entry->published()) { $date = strtotime($entry->published()); } } $_entry['Date'] = $date; $channel['Entry'][] = $_entry; // Remember author if first found if (empty($author) && $entry->author()) { if ($entry->author->name()) { $author = $entry->author->name(); } else { if ($entry->author->email()) { $author = $entry->author->email(); } else { $author = $entry->author(); } } } elseif (empty($author)) { $author = null; } $counter++; } $channel['Title'] = $feed->title(); $channel['URL'] = $url; $channel['Description'] = $feed->subtitle(); // get Link if rel="alternate" if ($feed->link('alternate')) { $channel['Link'] = $feed->link('alternate'); } else { // if there's no alternate, pick the one without "rel" attribtue $_links = $feed->link; if (is_array($_links)) { foreach ($_links as $_link) { if (empty($_link['rel'])) { $channel['Link'] = $_link['href']; break; } } } else { $channel['Link'] = $_links['href']; } } if (!empty($author)) { $channel['Author'] = $author; } } else { throw new Exception('Invalid feed type'); } $resp = json_encode($channel); } catch (Zend_Feed_Exception $e) { $resp = 'Error parsing feed: ' . $e->getMessage(); } } else { // feed import failed $resp = "Error fetching feed, response code: " . $result->getHttpCode(); } } else { $resp = $result->getResponseContent(); } $json = array($url => array('body' => $resp, 'rc' => $status)); $json = json_encode($json); $output = UNPARSEABLE_CRUFT . $json; $cookie = $result->getResponseHeader('Set-Cookie'); $this->header("Set-Cookie: {$cookie}", true); $this->setCachingHeaders(); // header("Content-Type: application/json; charset=utf-8", true); echo $output; } else { @ob_end_clean(); $error = $result->getHttpCode(); $content = $result->getResponseContent(); header("HTTP/1.0 {$error} Error", true); echo "<html><body><h1>{$content}</h1></body></html>"; } $this->end(); }
/** * Fetches content and returns it in JSON format * * @param string $url the url to fetch * @param GadgetSigner $signer the request signer to use * @param string $method the http method to use (get or post) in making the request */ public function fetchJson($url, $signer, $method) { try { $token = $this->context->extractAndValidateToken($signer); } catch (Exception $e) { $token = ''; // no token given, safe to ignore } $url = $this->validateUrl($url); // Fetch the content and convert it into JSON. // TODO: Fetcher needs to handle variety of HTTP methods. $result = $this->fetchContentDivert($url, $method, $signer); if (!isset($result)) { //OAuthFetcher only $metadata = $this->oauthFetcher->getResponseMetadata(); $json = array($url => $metadata); $json = json_encode($json); $output = UNPARSEABLE_CRUFT . $json; $this->setCachingHeaders(); header("Content-Type: application/json; charset=utf-8", true); echo $output; die; } $status = (int) $result->getHttpCode(); //header("HTTP/1.1 $status", true); if ($status == 200) { $output = ''; if (isset($_GET['contentType']) && $_GET['contentType'] == 'FEED') { require 'src/common/Zend/Feed.php'; $numEntries = $_GET['numEntries']; $getSummaries = $_GET['getSummaries']; $channel = array(); $request = new RemoteContentRequest($url); $request = $this->context->getHttpFetcher()->fetch($request, $this->context); if ((int) $result->getHttpCode() == 200) { $content = $result->getResponseContent(); try { $feed = Zend_Feed::importString($content); if ($feed instanceof Zend_Feed_Rss) { $channel = array('title' => $feed->title(), 'link' => $feed->link(), 'description' => $feed->description(), 'pubDate' => $feed->pubDate(), 'language' => $feed->language(), 'category' => $feed->category(), 'items' => array()); // Loop over each channel item and store relevant data $counter = 0; foreach ($feed as $item) { if ($counter >= $numEntries) { break; } $counter++; $channel['items'][] = array('title' => $item->title(), 'link' => $item->link(), 'author' => $item->author(), 'description' => $getSummaries ? $item->description() : '', 'category' => $item->category(), 'comments' => $item->comments(), 'pubDate' => $item->pubDate()); } } elseif ($feed instanceof Zend_Feed_Atom) { $channel = array('title' => $feed->title(), 'link' => $feed->link(), 'id' => $feed->id(), 'subtitle' => $feed->subtitle(), 'items' => array()); $counter = 0; foreach ($feed as $entry) { if ($counter >= $numEntries) { break; } $channel['items'][] = array('id' => $entry->id(), 'title' => $entry->title(), 'link' => $entry->link(), 'summary' => $entry->summary(), 'content' => $entry->content(), 'author' => $entry->author(), 'published' => $entry->published(), 'updated' => $entry->updated()); } } else { throw new Exception('Invalid feed type'); } $resp = json_encode($channel); } catch (Zend_Feed_Exception $e) { $resp = 'Error parsing feed: ' . $e->getMessage(); } } else { // feed import failed $resp = "Error fetching feed, response code: " . $result->getHttpCode(); } } else { $resp = $result->getResponseContent(); } $json = array($url => array('body' => $resp, 'rc' => $status)); $json = json_encode($json); $output = UNPARSEABLE_CRUFT . $json; $this->setCachingHeaders(); header("Content-Type: application/json; charset=utf-8", true); echo $output; } else { @ob_end_clean(); header("HTTP/1.0 404 Not Found", true); echo "<html><body><h1>404 - Not Found</h1></body></html>"; } die; }
/** * Upload action * * Handles atom file uploading, not pretty but gets the work done for now. * * @return void */ public function uploadAction() { try { $this->_helper->viewRenderer->setNoRender(); $feed = Zend_Feed::importString($this->_request->getRawBody()); } catch (Exception $e) { $this->_response->setHttpResponseCode(400); // 415 unsuported media? $this->_response->setHeader('Status', '400 Unable to read feed', true); $this->view->errorMessages = array('Unable to read feed'); return $this->_forward('error'); } $photoModel = $this->_getPhotoModel(); $id = null; foreach ($feed as $entry) { // // Entry is related to another entry // if (isset($entry->link)) { // Set description if ('' != $entry->content() && $entry->content->offsetGet('mode') == 'xml') { $description = (string) $entry->content; } // Set tags if ('' != ($tags = $entry->{"dc:subject"})) { $tags = implode(',', explode(' ', $tags)); } $entryData = array(); if (!is_array($entry->link)) { $id = $entry->link('related'); $photo = $photoModel->fetchEntry($id); $entryData = $photo->toArray(); $entryData['description'] = $description; $entryData['tags'] = $tags; $photoModel->update($entryData, $id); } else { foreach ($entry->link as $link) { $id = $link->getDom()->getAttribute('href'); try { $photo = $photoModel->fetchEntry($id); $entryData = $photo->toArray(); $entryData['description'] = $description; $entryData['tags'] = $tags; $photoModel->update($entryData, $id); } catch (Exception $e) { // TODO: Log.... } } } // // A new entry is uploaded // } else { if ('' != $entry->content()) { $file = $this->_createTmpFile(base64_decode($entry->content())); $entryData = array(); $entryData['title'] = $entry->title; $entryData['created_on'] = date('Y-m-d H:i:s', strtotime($entry->issued)); $id = $photoModel->add($entryData, $file); } else { continue; } } $url = 'http://' . $this->_request->getHttpHost() . $this->_request->getBaseUrl(); $this->_response->setHttpResponseCode(201); $this->_response->setHeader('Status', '201 Created'); $this->_response->setHeader('Location', $url . $this->_helper->url('show', 'photo', null, array('id' => $id))); $this->view->url = $url; $this->view->photo = $photoModel->fetchEntry($id); $this->render('upload'); } }
/** * Reads the content of the RSS feed and cache it in the database * @return Zend_Feed_Abstract - the feed object * @throws FeedReadException in case of failure */ public function read($feedId) { $rowSet = $this->find($feedId); if (!count($rowSet)) { throw new FeedReadException("Feed id {$feedId} not found in db."); } $row = $rowSet->current(); $cache = $this->getFeedsCache(); if (!($xml = $cache->load($feedId))) { $xml = file_get_contents($row->url); if (!$xml) { throw new FeedReadException("Failed retrieving content for feed " . $row->url); } $date = new Zend_Date(); $row->updated = $date->get(Zend_Date::W3C); $cache->save($xml, $feedId); } try { $feed = Zend_Feed::importString($xml); } catch (Zend_Exception $e) { return null; } return $feed; }