Пример #1
0
 public function parse()
 {
     $this->xml->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
     $this->xml->feed->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
     $feed = new Feed();
     $feed->type = "atom";
     $feed->url = $this->url;
     $feed->setLastBuildDate((string) $this->xml->updated);
     $feed->title = (string) $this->xml->title;
     $feed->summary = (string) $this->xml->subtitle;
     $links = $this->xml->xpath('atom:link[@rel="alternate"]');
     if (!empty($links)) {
         $link = $links[0]->attributes();
         $feed->link = (string) $link->href;
     }
     if (!$feed->link) {
         $feed->link = $feed->url;
     }
     // Default link is the URL to the feed itself
     foreach ($this->xml->xpath('/atom:feed/atom:entry') as $entry) {
         $entry->registerXPathNamespace('atom', 'http://www.w3.org/2005/Atom');
         $article = new Article();
         $article->guid = (string) $entry->id;
         $article->published = 0;
         $pubDate = (string) $entry->published;
         if (!$pubDate) {
             $pubDate = (string) $entry->updated;
         }
         if ($pubDate) {
             if (($timestamp = strtotime($pubDate)) !== false) {
                 $article->published = $timestamp;
             }
         }
         $article->link_url = null;
         $links = $entry->xpath('atom:link[@rel="alternate"]');
         if ($links) {
             $link = $links[0]->attributes();
             $article->link_url = (string) $link->href;
         }
         // If still no link_url, try first link of any type
         if (!$article->link_url) {
             $links = $entry->xpath('atom:link');
             $link = $links[0]->attributes();
             $article->link_url = (string) $link->href;
         }
         // If a post has no GUID, use its link as a GUID instead
         if (!$article->guid) {
             $article->guid = $article->link_url;
         }
         if (!$article->guid) {
             continue;
         }
         if ((string) $entry->content) {
             $article->text = (string) $entry->content;
         } else {
             $article->text = (string) $entry->summary;
         }
         if (isset($entry->author) && isset($entry->author->name)) {
             $article->author = (string) $entry->author->name;
         }
         $article->title = (string) $entry->title;
         $feed->articles[] = $article;
     }
     return $feed;
 }
Пример #2
0
 public function parse()
 {
     $feed = new Feed();
     $this->xml->channel->registerXPathNamespace('sy', 'http://purl.org/rss/1.0/modules/syndication/');
     $feed->type = "rss";
     $feed->url = $this->url;
     $feed->setLastBuildDate((string) $this->xml->channel->lastBuildDate);
     $feed->title = (string) $this->xml->channel->title;
     $feed->summary = (string) $this->xml->channel->description;
     $feed->link = (string) $this->xml->channel->link;
     $sy = $this->xml->channel->children('http://purl.org/rss/1.0/modules/syndication/');
     $feed->setUpdateInformation((string) $sy->updatePeriod, (string) $sy->updateFrequency);
     if (!$feed->link) {
         $feed->link = $feed->url;
     }
     // Default link is the URL to the feed itself
     $rss1 = false;
     if ($this->xml->channel->items) {
         $rdf = $this->xml->channel->items->children('http://www.w3.org/1999/02/22-rdf-syntax-ns#');
         if (count($rdf) > 0) {
             $rss1 = true;
         }
     }
     if ($rss1) {
         // RSS 1.0
         $parent = $this->xml;
     } else {
         // RSS 2.0 and higher
         $parent = $this->xml->channel;
     }
     foreach ($parent->item as $item) {
         $item->registerXPathNamespace('dc', 'http://purl.org/dc/elements/1.1/');
         $item->registerXPathNamespace('content', 'http://purl.org/rss/1.0/modules/content/');
         $article = new Article();
         $article->guid = (string) $item->guid;
         $article->published = 0;
         if ($rss1) {
             $pubDate = (string) current($item->xpath('dc:date'));
         } else {
             $pubDate = (string) $item->pubDate;
         }
         if ($pubDate) {
             if (($timestamp = strtotime($pubDate)) !== false) {
                 $article->published = $timestamp;
             }
         }
         $article->link_url = (string) $item->link;
         $article->title = (string) $item->title;
         $article->author = current($item->xpath('dc:creator'));
         // If a post has no GUID, use its link as a GUID instead
         if (!$article->guid) {
             $article->guid = $article->link_url;
         }
         if (!$article->guid) {
             continue;
         }
         $encoded = $item->xpath('content:encoded');
         if ($encoded) {
             $article->text = (string) current($encoded);
         } else {
             $article->text = (string) $item->description;
         }
         $feed->articles[] = $article;
     }
     return $feed;
 }