Changes between Version 9 and Version 10 of UsingFeedsInCode


Ignore:
Timestamp:
Jul 20, 2006, 5:59:46 PM (15 years ago)
Author:
nick
Comment:

php example

Legend:

Unmodified
Added
Removed
Modified
  • UsingFeedsInCode

    v9 v10  
    77== PHP ==
    88
    9 ''' to follow '''
     9An example for outputting the title and summary from the recent changes list follows.
     10{{{
     11<?php
     12$want_array = array(
     13   "TITLE"    => "Title",
     14   "SUMMARY"  => "Summary"
     15);
     16$feed = "http://cotswolds.openguides.org/wiki/wiki.cgi?action=rc;format=atom";
     17
     18$tag_text = "";
     19function startElement($parser, $name, $attrs)
     20{
     21}
     22
     23function endElement($parser, $name)
     24{
     25   global $want_array;
     26   global $tag_text;
     27   if (isset($want_array[$name])) {
     28       echo "<b>$want_array[$name]:</b> $tag_text<br />";
     29   }
     30}
     31
     32function characterData($parser, $data)
     33{
     34   global $tag_text;
     35   $tag_text = $data;
     36}
     37
     38// Grab feed
     39$curl = curl_init($feed);
     40curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
     41$data = curl_exec($curl);
     42
     43// Run parser
     44$xml_parser = xml_parser_create();
     45xml_set_element_handler($xml_parser, "startElement", "endElement");
     46xml_set_character_data_handler($xml_parser, "characterData");
     47
     48if (!xml_parse($xml_parser, $data)) {
     49  die(sprintf("XML error: %s at line %d",
     50                           xml_error_string(xml_get_error_code($xml_parser)),
     51                           xml_get_current_line_number($xml_parser)));
     52}
     53xml_parser_free($xml_parser);
     54?>
     55}}}
    1056
    1157== Ruby On Rails ==