You are here:  » magicparser examples on one page


magicparser examples on one page

Submitted by atman on Tue, 2006-09-19 08:54 in

hi david,

i tried the examples on

http://www.magicparser.com/node/25

Amazon Web Services Product Search
BBC News Headlines (RSS)

I can make it work individually but not at the same time on the same page.

Is it possible for the two examples to be used on the same page at the same time?

i get this error.

Fatal error: Cannot redeclare magicparser_xml_starttag() (previously declared in /home/ACCOUNTUSERNAME/public_html/MagicParser.php:2) in /home/ACCOUNTUSERNAME/public_html/MagicParser.php on line 2

thank you.

atman

Submitted by support on Tue, 2006-09-19 09:22

Hi,

That's probably because you are including MagicParser.php twice - it should only be included once, so simply delete the SECOND call to:

require("MagicParser.php");

The second change you will need to make is to use a different name for the record handler function - they cannot both be called "myRecordHandler". Here's the BBC and Amazon examples in one script to show you what I mean:

<?php
  
require("MagicParser.php");
  function 
myBBCRecordHandler($item)
  {
    print 
"<h2><a href='".$item["LINK"]."'>".$item["TITLE"]."</a></h2>";
    print 
"<p>".$item["DESCRIPTION"]."</p>";
  }
  print 
"<h1>BBC News Headlines</h1>";
  
$url "http://newsrss.bbc.co.uk/rss/newsonline_uk_edition/front_page/rss.xml";    
  
MagicParser_parse($url,"myBBCRecordHandler","xml|RSS/CHANNEL/ITEM/");  
  if (!
$_GET["q"]) $_GET["q"] = "DVD Player";
  print 
"<h1>Amazon Web Services Product Search (Electronics)</h1>";
  print 
"<form method='GET'>";
  print 
"<input type='text' name='q' value='".htmlentities($_GET["q"])."' /> ";
  print 
"<input type='submit' value='Search' />";
  print 
"</form>";
  function 
myAmazonRecordHandler($item)
  {
    print 
"<h3><a href='".$item["DETAILPAGEURL"]."'>".$item["ITEMATTRIBUTES/TITLE"]."</a></h3>";
    print 
"<img src='".$item["SMALLIMAGE/URL"]."' />";
    print 
"<br /><br />";
  }
  if (
$_GET["q"])
  {
    
// construct Amazon Web Services REST Query URL
    
$url  "http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService";
    
$url .= "&Version=2005-03-23";
    
$url .= "&Operation=ItemSearch";
    
$url .= "&ContentType=text%2Fxml";
    
$url .= "&SubscriptionId=0VSBK4FNHZ2K95R3N2G2";
    
$url .= "&ResponseGroup=Images,Medium";
    
$url .= "&SearchIndex=Electronics";
    
$url .= "&Keywords=".urlencode($_GET["q"]);
    
// fetch the response and parse the results
    
MagicParser_parse($url,"myAmazonRecordHandler","xml|ITEMSEARCHRESPONSE/ITEMS/ITEM/");
  }
?>

Cheers,
David.

Submitted by atman on Tue, 2006-09-19 19:08

thanks again david for the nice tips and help.

cheers,

atman