项目作者: omothm

项目描述 :
Atom feed parser
高级语言: PHP
项目地址: git://github.com/omothm/atom-splitter.git
创建时间: 2019-05-01T21:18:23Z
项目社区:https://github.com/omothm/atom-splitter

开源协议:GNU General Public License v3.0

下载


atom-splitter

A PHP library to retrieve and parse Atom feed from a URL.

What It Does

AtomSplitter fetches Atom feed from a given URL (using cURL) and parses its feed entries into a PHP array. For example, a feed like this:

  1. <entry>
  2. <title>Some Title</title>
  3. <link href="http://example.org/2012/12/12/some-title"/>
  4. <id>12345</id>
  5. <updated>2012-12-12T12:12:12Z</updated>
  6. <author>
  7. <name>Omar Othman</name>
  8. <uri>http://omothm.com</uri>
  9. <email>omar@omothm.com</email>
  10. </author>
  11. <content>Some text.</content>
  12. </entry>
  13. <entry>
  14. <title>Another Title</title>
  15. <link href="http://example.org/2012/12/13/another-title"/>
  16. <id>67890</id>
  17. <updated>2012-12-13T12:12:12Z</updated>
  18. <author>
  19. <name>Author 1</name>
  20. </author>
  21. <author>
  22. <name>Author 2</name>
  23. </author>
  24. <content>Some text.</content>
  25. </entry>

would be converted into this:

  1. Array
  2. (
  3. [0] => Array
  4. (
  5. [authors] => Array
  6. (
  7. [0] => Array
  8. (
  9. [name] => Omar Othman
  10. [uri] => http://omothm.com
  11. [email] => omar@omothm.com
  12. )
  13. )
  14. [title] => Some Title
  15. [link] => http://example.org/2012/12/12/some-title
  16. [id] => 12345
  17. [updated] => 2012-12-12T12:12:12Z
  18. [content] => Some text.
  19. )
  20. [1] => Array
  21. (
  22. [authors] => Array
  23. (
  24. [0] => Array
  25. (
  26. [name] => Author 1
  27. )
  28. [1] => Array
  29. (
  30. [name] => Author 2
  31. )
  32. )
  33. [title] => Another Title
  34. [link] => http://example.org/2012/12/13/another-title
  35. [id] => 67890
  36. [updated] => 2012-12-13T12:12:12Z
  37. [content] => Some text.
  38. )
  39. )

Note: This tool parses <entry> nodes only. Any nodes other than <entry> are ignored.

Usage

  1. After adding AtomSplitter.php to your website (let’s say in php/), import it into the page where you want to use it.
    1. require "php/AtomSplitter.php";
    2. use \com\omothm\AtomSplitter;
  2. Initialize an AtomSplitter object with the URL to the atom feed and run it.
    1. $url = "https://en.blog.wordpress.com/feed/atom/";
    2. $splitter = new AtomSplitter($url);
    3. $ret = $splitter->run();
  3. If successful, $ret will be TRUE. Retrieve the entries array.
    1. if ($ret === TRUE) {
    2. $entries = $splitter->get_entries();
    3. } else {
    4. $error = $splitter->get_error();
    5. }
  4. Get individual information about each entry.

    1. // Get information about the first entry (index 0)
    2. $title = $entries[0]["title"];
    3. $link = $entries[0]["link"];
    4. $id = $entries[0]["id"];
    5. $updated = $entries[0]["updated"];
    6. $content = $entries[0]["content"];
    7. $authors = $entries[0]["authors"];
    8. $first_author_name = $authors[0]["name"]; // or $entries[0]["authors"][0]["name"]
    9. $first_author_uri = $authors[0]["uri"];
    10. $first_author_email = $authors[0]["email"];

    The information shown here about each entry are all the information parsed about that entry. Other information are ignored.

    Other features

  • Raw content. If you want to see what the actual XML looks like, use this function:
    1. $raw_content = $splitter->get_raw_content();
    This function will return the XML feed as retrieved from the source.
  • Timeout. You can set timeout and number of trials on the object to limit the time and trials of fetching the feed.
    1. $timeout = 3; // in seconds
    2. $trials = 3;
    3. $splitter = new AtomSplitter($url, $timeout, $trials);
    The default is 0 for the timeout (indefinite) and 1 for trials.