You are here:  » Could not open file


Could not open file

Submitted by apwade on Thu, 2009-06-11 21:22 in

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)."&nbsp;</td>";
      print 
"</tr>";
    }
    print 
"</table>";
    
// return a non-zero value to stop reading any more records
    //return 1;
  
}
  
MagicParser_parse($feed,"myRecordHandler",$format);
?>

Andrew

Submitted by support on Fri, 2009-06-12 06:25

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)."&nbsp;</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)."&nbsp;</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.

Submitted by apwade on Fri, 2009-06-12 06:49

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

Submitted by support on Fri, 2009-06-12 08:06

Hi Andrew,

I just commented out the "return 1;" line of the second example; and it has displayed all items on my test server. Could you perhaps email me your script as an attachment, and a link to the script running on your server...

Thanks!
David.