arthurkushman /
querypath
| 1 | <?php |
||
| 2 | /** |
||
| 3 | * Using QueryPath to Generate an RSS feed. |
||
| 4 | * |
||
| 5 | * This file contains an example of how QueryPath can be used |
||
| 6 | * to generate an RSS feed. |
||
| 7 | * |
||
| 8 | * It uses two stubs -- one for the main RSS file, and one for an RSS entry -- |
||
| 9 | * and it merges data into the stubs. |
||
| 10 | * |
||
| 11 | * The method exhibited here is one of the more primitive ways of templating |
||
| 12 | * information. See the {@link techniques.php techniques} example for multiple |
||
| 13 | * methods of looping. An even more advanced method would be to use the |
||
| 14 | * {@link QPTPL} library. |
||
| 15 | * |
||
| 16 | * |
||
| 17 | * @author M Butcher <[email protected]> |
||
| 18 | * @license LGPL The GNU Lesser GPL (LGPL) or an MIT-like license. |
||
| 19 | */ |
||
| 20 | |||
| 21 | require_once '../src/QueryPath/QueryPath.php'; |
||
| 22 | |||
| 23 | // This is the stub RSS document. |
||
| 24 | $rss_stub ='<?xml version="1.0"?> |
||
| 25 | <rss version="2.0" |
||
| 26 | xmlns:dc="http://purl.org/dc/elements/1.1/"> |
||
| 27 | <channel> |
||
| 28 | <title></title> |
||
| 29 | <link></link> |
||
| 30 | <description></description> |
||
| 31 | <language>en</language> |
||
| 32 | <generator>QueryPath</generator> |
||
| 33 | </channel> |
||
| 34 | </rss> |
||
| 35 | '; |
||
| 36 | |||
| 37 | // This is the stub RSS element. |
||
| 38 | $rss_item_stub = '<?xml version="1.0"?> |
||
| 39 | <item> |
||
| 40 | <title>Untitled</title> |
||
| 41 | <link></link> |
||
| 42 | <description> |
||
| 43 | </description> |
||
| 44 | <comments></comments> |
||
| 45 | <category></category> |
||
| 46 | <pubDate></pubDate> |
||
| 47 | <guid isPermaLink="false"></guid> |
||
| 48 | </item>'; |
||
| 49 | |||
| 50 | // Here are some dummy items. For the same of |
||
| 51 | // simplicity, we are just using a nested array. Of |
||
| 52 | // course, this could be a database lookup or whatever. |
||
| 53 | $items = array( |
||
| 54 | array( |
||
| 55 | 'title' => 'Item 1', |
||
| 56 | 'link' => 'http://example.com/item1', |
||
| 57 | 'description' => '<strong>This has embedded <em>HTML</em></strong>', |
||
| 58 | 'comments' => 'http://example.com/item1/comments', |
||
| 59 | 'category' => 'Some Term', |
||
| 60 | 'pubDate' => date('r'), |
||
| 61 | 'guid' => '123456-789', |
||
| 62 | ), |
||
| 63 | array( |
||
| 64 | 'title' => 'Item 2', |
||
| 65 | 'link' => 'http://example.com/item2', |
||
| 66 | 'description' => '<strong>This has embedded <em>HTML</em></strong>', |
||
| 67 | 'comments' => 'http://example.com/item2/comments', |
||
| 68 | 'category' => 'Some Other Term', |
||
| 69 | 'pubDate' => date('r'), |
||
| 70 | 'guid' => '123456-790', |
||
| 71 | ), |
||
| 72 | ); |
||
| 73 | |||
| 74 | // The main QueryPath, which holds the channel. |
||
| 75 | $qp = qp($rss_stub, 'title') |
||
| 76 | ->text('A QueryPath RSS Feed') |
||
| 77 | ->next('link')->text('http://example.com') |
||
|
0 ignored issues
–
show
Bug
introduced
by
Loading history...
|
|||
| 78 | ->next('description')->text('QueryPath: Find your way.') |
||
| 79 | ->parent(); |
||
| 80 | |||
| 81 | // For each element in the array above, we create a new |
||
| 82 | // QueryPath and then populate the XML fragment with data. |
||
| 83 | foreach ($items as $item) { |
||
| 84 | |||
| 85 | // Begin with the stub RSS item, with title currently selected. |
||
| 86 | $qpi = qp($rss_item_stub, 'title') |
||
| 87 | // Add a title. |
||
| 88 | ->text($item['title']) |
||
| 89 | // Add a link. Note that we are giving no args to next() for the |
||
| 90 | // sake of simplicity. |
||
| 91 | ->next()->text($item['link']) |
||
| 92 | // Go to next element and add a description. Note that the text() |
||
| 93 | // call will automatically encode HTML. < will become < and so on. |
||
| 94 | ->next()->text($item['description']) |
||
| 95 | // Go on down the list... |
||
| 96 | ->next()->text($item['comments']) |
||
| 97 | ->next()->text($item['category']) |
||
| 98 | ->next()->text($item['pubDate']) |
||
| 99 | ->next()->text($item['guid']); |
||
| 100 | |||
| 101 | // Now we append it. |
||
| 102 | $qp->append($qpi->top()); |
||
| 103 | } |
||
| 104 | |||
| 105 | // If we were running this on a server, we would need to set the content |
||
| 106 | // type: |
||
| 107 | // header('Content-Type: application/rss+xml'); |
||
| 108 | |||
| 109 | // Write the outpt as XML. |
||
| 110 | $qp->writeXML(); |
||
| 111 | ?> |