SproutCMS

This is the code documentation for the SproutCMS project

source of /sprout/Helpers/RssFeed.php

  1. <?php
  2. /**
  3.  * Copyright (C) 2017 Karmabunny Pty Ltd.
  4.  *
  5.  * This file is a part of SproutCMS.
  6.  *
  7.  * SproutCMS is free software: you can redistribute it and/or modify it under the terms
  8.  * of the GNU General Public License as published by the Free Software Foundation, either
  9.  * version 2 of the License, or (at your option) any later version.
  10.  *
  11.  * For more information, visit <http://getsproutcms.com>.
  12.  *
  13.  * This class was originally from Kohana 2.3.4
  14.  * Copyright 2007-2008 Kohana Team
  15.  */
  16. namespace Sprout\Helpers;
  17.  
  18. use DateTime;
  19. use SimpleXMLElement;
  20.  
  21.  
  22. class RssFeed
  23. {
  24.  
  25. /**
  26.   * Download and parse a RSS feed
  27.   *
  28.   * Supports a subset of RSS 2.0 and also a subset of MRSS (Media RSS) 2.0
  29.   *
  30.   * News items are returned as arrays, with the following keys
  31.   * name Title
  32.   * text Short desc of the news post. Plain text
  33.   * date DateTime object
  34.   * url Link URL
  35.   * image Optional image URL
  36.   *
  37.   * @param string $url Feed to load and parse
  38.   * @return array News items
  39.   */
  40. public static function parse($url)
  41. {
  42. $feed = HttpReq::get($url);
  43.  
  44. $simple = new SimpleXMLElement($feed);
  45.  
  46. $out = [];
  47. foreach ($simple->channel->item as $rss_item) {
  48. $out_item = [
  49. 'name' => (string)$rss_item->title,
  50. 'date' => new DateTime($rss_item->pubDate),
  51. 'url' => (string)$rss_item->link,
  52. ];
  53.  
  54. $text = (string)$rss_item->description;
  55. $text = html_entity_decode($text);
  56. $text = strip_tags($text);
  57. $out_item['text'] = $text;
  58.  
  59. $media = $rss_item->children('http://search.yahoo.com/mrss/');
  60. foreach($media as $media_item) {
  61. $attrs = $media_item->attributes();
  62. if ((string)$attrs['medium'] == 'image') {
  63. $out_item['image'] = (string)$attrs['url'];
  64. }
  65. }
  66.  
  67. $out[] = $out_item;
  68. }
  69.  
  70. return $out;
  71. }
  72.  
  73. }
  74.