Code Monkey home page Code Monkey logo

qrss's Introduction

qrss

An API Class to parse the RSS feed into json with cache option.

Getting Start

Grab the QRss class and use require it in your php file.

require 'src/QRss.php';

// To fetch an RSS feed as json use
(new Qrss('https://news.google.com/?output=rss'))->json();

// For fresh copy you can use fresh() which will ignore cache
(new Qrss('https://news.google.com/?output=rss'))->fresh()->json()

// Get the feed ignoring validation adding novalidate()
(new QRss('https://en.blog.wordpress.com/feed/'))->novalidate()->json();

// There is also an option to get data in plain text using text() instead of json()
(new Qrss('https://news.google.com/?output=rss'))->text();

Override Parser

You can also extend the parse method to customize the output.

class MyQrss extends QRss {

     protected function parse($xml)
     {
         // you have all the xml elements as SimpleXMLElement object
         // parse it however you want, return the array from this
          return [
              'title' => (string) $xml->channel->title
          ];
      }
 }

 (new MyQrss('https://news.google.com/?output=rss'))->json();

qrss's People

Contributors

saqueib avatar vtisnado avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

qrss's Issues

Get information from namespace

Hello,

If a RSS feed contains elements with namespace (see below), what is the way to get informations like content:encoded or dc:xxx in JSON?

<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>feed title</title>
    <image>
      <url>http://www.xxxxx.com/images/logo.gif?v1.0</url>
      <title>feed image title</title>
      <link>http://www.xxxxx.com/</link>
    </image>
    <link>http://www.xxxxx.com/</link>
    <description>feed description</description>
    <language>en-us</language>
    <dc:creator>feed creator</dc:creator>
    <item>
      <link>http://www.xxxxx.com/aaaa/test-1234567890</link>
      <guid>http://www.xxxxx.com/aaaa/test-1234567890</guid>
      <title>item title</title>
      <pubDate>Thu, 31 Aug 2017 00:00:00 GMT</pubDate>
      <dc:language>en-us</dc:language>
      <dc:creator>item creator</dc:creator>
      <description>item description</description>
      <content:encoded>
<![CDATA[<h2>Test content</h2>
<p><img src="http://www.xxxxx.com/img/img_1.png" alt="..." title="..."  style="float:left;margin:0 1em 1em 0;border:1px solid #000;" />
content ... content - <span style="color:#666">TEST</span><br /><strong style="color:#333">style test</strong>
</p>
<p style="clear:left">
<a href="http://www.xxxxx.com/aaa/bbb/test-1122334455666">More...</a>
</p>]]>
      </content:encoded>
    </item>
  </channel>
</rss>

Do you think you could add it to your PHP class?

Thanks in advance and have a nice day.

Optional XML validation

Hey @saqueib nice work!

I'm just wondering if you can made the xml validation optional like cache, so we can use the library to parse all kind of xml files.

Maybe adding something like this:

private $no_validate = false;

public function novalidate()
    {
        $this->no_validate = true;
        return $this;
    }
        if( ! $this->no_validate ) {
            return ($rss && $rss->channel) ? $rss : false;
        }
        return ($rss);

Thanks in advanced.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.