[UPHPU] xml rss parsing in php 4 and 5

Brandon Stout hplsbyufan at imapmail.org
Mon Jul 10 13:43:21 MDT 2006


My localhost runs php5. My public host runs php4 still. My php 5 code
works, but my php 4 code does not.  I believe the problem lies in the
foreach() fuction.  I can find no equivalent the php5 item(0) method in
php4.

This code works on php5, using the DOM extension:

<?php

// MySQL vars

$srv = "localhost";
$usr = "user";
$pwd = "password";
$db = "rssdb";
$connect = @new mysqli("$srv", "$usr", "$pwd", "$db");
$connect -> query("SET NAMES 'utf8'");

// check connection

if (mysqli_connect_errno()) {
 $errno = mysqli_connect_errno();
 $err   = mysqli_connect_error();
 echo <<<STR
<h3>PHP could not access MySQL</h3>
<p>Error Number: $errno<br />
Internal error: "$err"</p>
STR;
 exit();
}

// sql to fetch rss feeds from MySQL

$xmlqry = "(
 SELECT xmlcontents FROM rssfeeds WHERE xmlcontents !=''
)";

// prepare the query

$xmldocs = @$connect->query($xmlqry);

// check query

if ($xmldocs === FALSE) {
 $errno  = $connect -> errno;
 $err = $connect -> error;
 echo <<<STR
<h3>PHP could not execute the xmldocs query</h3>
<p>Error number: $errno<br />
"Internal error: "$err"</p>
STR;
 $connect -> close();
 exit;
}

// Begin DomDocument class/object

$doc     = @new DomDocument();

while (($row = $xmldocs -> fetch_array()) !== NULL) {
 $xmldoc      = " $row[xmlcontents]\r\n";
 $doc         -> loadxml($xmldoc);
 $doc         -> preserveWhiteSpace = false;
 $xpath       = new DOMXPath($doc);
 $docItemQry  = '//item';
 $rssItems    = $xpath -> query($docItemQry);
 foreach ($rssItems as $rssItem) {
  $rssTitles  = $rssItem -> getElementsByTagName('title');
  $rssTitle   = trim($rssTitles -> item(0) -> nodeValue);
  $rssLinks   = $rssItem -> getElementsByTagName('link');
  $rssLink    = trim($rssLinks -> item(0) -> nodeValue);
  $rssDescs   = $rssItem -> getElementsByTagName('description');
  $rssDesc    = trim($rssDescs -> item(0) -> nodeValue);
  echo "  <li><a href=\"" . $rssLink . "\" rel=\"external\">"
  . $rssTitle . "</a></li>\r\n";
 }
}
?>

I've tried to use equivalent code (dom xml extension) with php 4 (my
public server), but it does *_not_* work:

<?php

// MySQL vars

$srv = "localhost";
$usr = "user";
$pwd = "password";
$db = "rssdb";
$mysql = @mysql_connect("$srv", "$usr", "$pwd");

// check connection

if (!$mysql) {
 die('Could not connect: ' . mysql_error());
 mysql_close($mysql);
}

$dbselect = mysql_select_db($db, $mysql);

if (!$dbselect) {
 die("PHP could not find the database: " . mysql_error());
 mysql_close($mysql);
}

// sql to fetch rss feeds from MySQL

$xmlqry = "(
 SELECT xmlcontents FROM rssfeeds WHERE xmlcontents !=''
)";

// prepare the query

$xmldocs = mysql_query($xmlqry, $mysql);

// check query

if (!$xmldocs) {
 die('<p>PHP could not perform the specified query.</p>'
 . '<p>MySQL gave this error: '
 . mysql_error()
 . '</p>'
 );
 mysql_close($mysql);
}

// Begin DomDocument class/object

$doc     = @new DomDocument();
while ($row = mysql_fetch_row($xmldocs)) {
 $xmldoc      = " $row[xmlcontents]\r\n";
 $doc         -> domxml_open_mem($xmldoc);
 $xpath       = $doc -> xpath_new_context();
 $rssItems    = $xpath -> xpath_eval('//item');
 foreach ($rssItems as $rssItem) {
  $rssTitles  = $rssItem -> get_elements_by_tagname('title');
  $rssTitle   = trim($rssTitles -> get_content);
  $rssLinks   = $rssItem -> get_elements_by_tagname('link');
  $rssLink    = trim($rssLinks -> node_value);
  $rssDescs   = $rssItem -> get_elements_by_tagname('description');
  $rssDesc    = trim($rssDescs -> node_value);
  echo "  <li><a href=\"" . $rssLink . "\" rel=\"external\">"
  . $rssTitle . "</a></li>\r\n";
 }
}
?>

Any ideas?

Brandon Stout
http://mscis.org



More information about the UPHPU mailing list