/** * Populate the feed object from a XML feed string. * * @param string A XML feed (RSS 2.0 format) * * @return sfRss10Feed The current object * * @throws Exception If the argument is not a well-formatted RSS feed */ public function fromXml($feedXml) { preg_match('/^<\\?xml\\s*version="1\\.0"\\s*encoding="(.*?)".*?\\?>$/mi', $feedXml, $matches); if (isset($matches[1])) { $this->setEncoding($matches[1]); } $feedXml = simplexml_load_string($feedXml); if (!$feedXml) { throw new Exception('Error creating feed from XML: string is not well-formatted XML'); } $authorString = (string) $feedXml->channel[0]->managingEditor; $pos = strpos($authorString, '('); if ($pos !== false) { $this->setAuthorEmail(trim(substr($authorString, 0, $pos))); $this->setAuthorName(trim(substr($authorString, $pos + 1, strlen($authorString) - $pos - 2))); } else { $this->setAuthorEmail(trim($authorString)); } $this->setTitle((string) $feedXml->channel[0]->title); $this->setLink((string) $feedXml->channel[0]->link); $this->setDescription((string) $feedXml->channel[0]->description); $this->setLanguage((string) $feedXml->channel[0]->language); if ($feedXml->channel[0]->image) { $image = new sfFeedImage(array("image" => (string) $feedXml->channel[0]->image->url, "imageX" => (int) $feedXml->channel[0]->image->width, "imageY" => (int) $feedXml->channel[0]->image->height, "link" => (string) $feedXml->channel[0]->image->link, "title" => (string) $feedXml->channel[0]->image->title)); $this->setImage($image); } $categories = array(); foreach ($feedXml->channel[0]->category as $category) { $categories[] = (string) $category; } $this->setCategories($categories); foreach ($feedXml->channel[0]->item as $itemXml) { $url = (string) $itemXml->link; $authorString = (string) $itemXml->author; $pos = strpos($authorString, '('); if ($pos !== false) { $authorEmail = trim(substr($authorString, 0, $pos)); $authorName = trim(substr($authorString, $pos + 1, strlen($authorString) - $pos - 2)); } else { $authorEmail = trim($authorString); $authorName = ''; } $dc = $itemXml->children("http://purl.org/dc/elements/1.1/"); if (!$authorName) { $authorName = (string) $dc->creator; } $pubdate = strtotime(str_replace(array('UT', 'Z'), '', (string) $itemXml->pubDate)); if (!$pubdate) { if ((string) $dc->date) { $pubdate = strtotime(str_replace(array('UT', 'Z'), '', (string) $dc->date)); } else { if (preg_match('/\\d{4}\\/\\d{2}\\/\\d{2}/', $url, $matches)) { $pubdate = strtotime($matches[0]); } else { $pubdate = 0; } } } $content = $itemXml->children("http://purl.org/rss/1.0/modules/content/"); $categories = array(); foreach ($itemXml->category as $category) { $categories[] = (string) $category; } if ($enclosureElement = $itemXml->enclosure) { $enclosure = new sfFeedEnclosure(); $enclosure->setUrl((string) $enclosureElement['url']); $enclosure->setLength((string) $enclosureElement['length']); $enclosure->setMimeType((string) $enclosureElement['type']); } else { $enclosure = null; } $this->addItemFromArray(array('title' => (string) $itemXml->title, 'link' => $url, 'description' => (string) $itemXml->description, 'content' => (string) $content->encoded, 'authorName' => $authorName, 'authorEmail' => $authorEmail, 'pubDate' => $pubdate, 'comments' => (string) $itemXml->comments, 'uniqueId' => (string) $itemXml->guid, 'enclosure' => $enclosure, 'categories' => $categories, 'feed' => $this)); } }
/** * Populate the feed object from a XML feed string. * * @param string A XML feed (Atom 1.0 format) * * @return sfAtom1Feed The current object * * @throws Exception If the argument is not a well-formatted Atom feed */ public function fromXml($feedXml) { preg_match('/^<\\?xml\\s*version="1\\.0"\\s*encoding="(.*?)".*?\\?>$/mi', $feedXml, $matches); if (isset($matches[1])) { $this->setEncoding($matches[1]); } $feedXml = simplexml_load_string($feedXml); if (!$feedXml) { throw new Exception('Error creating feed from XML: string is not well-formatted XML'); } $attributes = $feedXml->attributes('http://www.w3.org/XML/1998/namespace'); $this->setLanguage((string) $attributes['lang']); $this->setTitle((string) $feedXml->title); $feedXml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); if ($titles = $feedXml->xpath('atom:link[@rel="alternate"]')) { $this->setLink((string) $titles[0]['href']); } if ($titles = $feedXml->xpath('atom:link[@rel="self"]')) { $this->setFeedUrl((string) $titles[0]['href']); } $this->setSubtitle((string) $feedXml->subtitle); $this->setAuthorName((string) $feedXml->author->name); $this->setAuthorEmail((string) $feedXml->author->email); $this->setAuthorLink((string) $feedXml->author->uri); $image = new sfFeedImage(array("favicon" => (string) $feedXml->icon, "image" => (string) $feedXml->logo)); $this->setImage($image); $categories = array(); foreach ($feedXml->category as $category) { $categories[] = (string) $category['term']; } $this->setCategories($categories); foreach ($feedXml->entry as $itemXml) { $categories = array(); foreach ($itemXml->category as $category) { $categories[] = (string) $category['term']; } $url = (string) $itemXml->link['href']; $pubdate = strtotime((string) $itemXml->published); if (!$pubdate) { if ((string) $itemXml->updated) { $pubdate = strtotime((string) $itemXml->updated); } else { if ((string) $itemXml->modified) { $pubdate = strtotime((string) $itemXml->modified); } else { if (preg_match('/\\d{4}\\/\\d{2}\\/\\d{2}/', $url, $matches)) { $pubdate = strtotime($matches[0]); } else { $pubdate = 0; } } } } $itemXml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom'); if ($enclosures = $itemXml->xpath('atom:link[@rel="enclosure"]')) { $enclosure = new sfFeedEnclosure(); $enclosure->setUrl((string) $enclosures[0]['href']); $enclosure->setLength((string) $enclosures[0]['length']); $enclosure->setMimeType((string) $enclosures[0]['type']); } else { $enclosure = null; } $content = (string) $itemXml->content; if (!$content) { $content = preg_replace('/<\\/?content[^>]*>/iU', '', $itemXml->content->asXml()); } $this->addItemFromArray(array('title' => (string) $itemXml->title, 'link' => $url, 'authorName' => (string) $itemXml->author->name, 'authorEmail' => (string) $itemXml->author->email, 'authorLink' => (string) $itemXml->author->uri, 'pubDate' => $pubdate, 'description' => (string) $itemXml->summary, 'content' => $content, 'uniqueId' => (string) $itemXml->id, 'enclosure' => $enclosure, 'categories' => $categories, 'feed' => $this)); } return $this; }