I tried the following code and i it does not parse the the xml feed, although it works through your demo. My hosting account says fopen works and the account allows access to feeds via web address, please help.
<?php
require("MagicParser.php");
$feed = "{link saved}";
$filename = "feed.xml";
$format_string = MagicParser_getFormat($filename);
if (!$format_string)
{
print "<p>".MagicParser_getErrorMessage()."</p>";
exit;
}
else
{
print "<p><strong>Autodetected Format String:</strong> ".$format_string."</p>";
}
print "<p>Contents of first record:</p>";
function myRecordHandler($record)
{
print "<table border='1'>";
foreach($record as $key => $value)
{
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
}
print "</table>";
// return a non-zero value to stop reading any more records
//return 1;
}
MagicParser_parse($feed,"myRecordHandler",$format);
?>
Andrew
David
I have tried your code and it works for the first record, but if you try and display all the records it only displays the first three. Is it because the file is to big as i have tried it smaller feeds and it works ok. Is there another method to reading large feeds?
Andrew
Hi Andrew,
The initial part of the code above uses the $filename variable rather than the feed URL ($feed) - the feed URL needs to be passed to the Magic Parser functions directly. The following code works fine with your feed (myRecordHandler returns 1 to stop after first record)...
<?php
require("MagicParser.php");
$feed = "{link saved}";
$format_string = MagicParser_getFormat($feed);
if (!$format_string)
{
print "<p>".MagicParser_getErrorMessage()."</p>";
exit;
}
else
{
print "<p><strong>Autodetected Format String:</strong> ".$format_string."</p>";
}
print "<p>Contents of first record:</p>";
function myRecordHandler($record)
{
print "<table border='1'>";
foreach($record as $key => $value)
{
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
}
print "</table>";
// return a non-zero value to stop reading any more records
return 1;
}
MagicParser_parse($feed,"myRecordHandler",$format);
?>
When parsing large feeds, it's always best to supply a Format String rather than use auto-detection, which in the case of your feed is xml|VOUCHERCODES/ITEM/, so the following code will display the same output as the code above:
<?php
require("MagicParser.php");
print "<p>Contents of first record:</p>";
function myRecordHandler($record)
{
print "<table border='1'>";
foreach($record as $key => $value)
{
print "<tr>";
print "<th>".$key."</th>";
print "<td>".htmlentities($value)." </td>";
print "</tr>";
}
print "</table>";
// return a non-zero value to stop reading any more records
return 1;
}
$feed = "{link saved}";
MagicParser_parse($feed,"myRecordHandler","xml|VOUCHERCODES/ITEM/");
?>
Hope this helps!
Cheers,
David.