You are here:  » Affiliate Product Feed


Affiliate Product Feed

Submitted by bassie on Sun, 2009-06-21 21:40 in

Dear all,

I just bought MP.
As an affiliate: from time to time a (travel) productfeed is avaible and I want to display the feed in a HTML page on my site. Inlcuding text and images.
For that reason I created a php page with some standard MP code.
What it does is parsing the xml feed into a HTML table with text and showing stuff like IMG_SMALL etc.
I want it to look a nice HTML presentation with the products (travel) provided by the xml productfeed.

How can I achieve this?

Example:
{link saved}

Code:

<?php
  
require("mp/MagicParser.php");
  
$filename "http://xml.ds1.nl/ (details are left out)";
  
$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($filename,"myRecordHandler",$format);
?>

Sorry for not expressing myself clearly.

Many thanks in advance!

Submitted by support on Mon, 2009-06-22 07:19

Hello bassie,

Basically, it's just a case of constructing the HTML required using PHP's print statement, and including in the HTML you generate values from each of the fields that you want to display. For example, you have an image URL in the $record["IMG_LARGE"] fields, so you could use this to construct the <img> HTML.

Furthermore, you might want to look at importing the feed to a MySQL database and displaying the information from there rather than having every request of a page on your website result in a request to the server hosting the feed (although caching might be an easier alternative).

Anyway, one step at a time, keeping with displaying just the first record (so myRecordHandler returns 1 to stop the parser), and also incorporating the auto-detected Format String directly into the call to MagicParser_parse(), have a go with something like this, which will display the title as a link, the long description paragraph, and the image:

<?php
  
require("mp/MagicParser.php");
  
$filename "http://xml.ds1.nl/ (details are left out)";
  function 
myRecordHandler($record)
  {
    print 
"<h1><a href='".$record["LINK"]."'>".$record["TITLE"]."</a></h1>";
    print 
"<p>".$record["DESCRIPTION"]."</p>";
    print 
"<img src='".$record["IMG_LARGE"]."' />";
    
// return a non-zero value to stop reading any more records
    
return 1;
  }
  
MagicParser_parse($filename,"myRecordHandler","xml|ITEMS/ITEM/");
?>

Hope this helps!
Cheers,
David.

Submitted by bassie on Sun, 2009-07-05 06:11

Hi David!

Many thanks for the help.
I created my first HTML page with a XML feed (10 items)!
Yes this is working, great support!

You are right about importing a feed into a MySQL DB (want to learn more later).
But these are my very first steps and I am learning.

Is it okay if I ask following (my next step in learning).
The myRecordHandler is set to zero and the whole (feed)list is displayed in one HTML page.
What if my feed has more then 10 items, = 100 items, and I want to display max. 20 items in a HTML page?
The visitor must be able to hit some button or text link for the next page with the next 10 items/page.

Many thanks in avdvance,

Submitted by support on Sun, 2009-07-05 08:56

Hi bassie,

Sure - have a look at the code in the first reply in the following thread regarding next / previous links...

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

Cheers,
David.