File:  [mozdev] / aecreations / www / xml_test.php
Revision 1.35: download - view: text, annotated - select for diffs - revision graph
Wed Sep 17 20:48:58 2008 UTC (9 years, 2 months ago) by ateng
Branches: MAIN
CVS tags: HEAD
Output errors as comments

<h3>AE Creations Blog Summary Test</h3>

<p>Using PHP to read the Atom or RSS feed of <a href="http://aecreations.blogspot.com/">the AE Creations blog</a>.  First three blog entries, showing only the title and post dates:</p>

<?php

function handleError($errno, $errstr, $errfile, $errline)
{
  echo '<!-- Error ' . $errno . ": " . $errstr . " [" . $errfile . ":" . $errline . "] -->\n";
}


set_error_handler('handleError');

$doc = new DOMDocument();
$doc->formatOutput = true;
$doc->load('http://aecreations.blogspot.com/feeds/posts/default?start-index=1&max-results=3');
$entries = $doc->getElementsByTagName('entry');

trigger_error("This is a test error!", E_USER_WARNING);

$titles = array();
$dates = array();
$post_urls = array();

for ($i = 0; $i < 3; $i++) {
  $children = $entries->item($i)->childNodes;

  foreach ($children as $child) {
    if ($child->nodeName == 'title') {
      $titles[$i] = $child->nodeValue;
    }

    if ($child->nodeName == 'published') {
      $dtz = timezone_open('America/Los_Angeles');
      $dt = date_create($dates[$i] = $child->nodeValue, $dtz);
      $dates[$i] = date_format($dt, "d M Y");
    }

    if ($child->nodeName == 'link') {
      if ($child->getAttribute('rel') == 'alternate') {
        $post_urls[$i] = $child->getAttribute('href');
      }
    }
  }
}

echo "<dl>\n";

for ($j = 0; $j < count($titles); $j++) {
  echo '<dt><strong>' . $titles[$j] . "</strong> - " . $dates[$j] . " </dt>\n";
  echo "  <dd>${post_urls[$j]}</dd>\n";
}

echo "</dl>\n";

?>


<p>The XML dump should appear below; use View &rarr; Source in the browser to see the formatted XML output.</p>

<hr>

<?php

echo $doc->saveXML() . "\n";

?>

<hr>


FreeBSD-CVSweb <freebsd-cvsweb@FreeBSD.org>