You are here:  » Ebay RSS Feeds to XML


Ebay RSS Feeds to XML

Submitted by bigmac on Tue, 2010-03-30 11:45 in

Hi David,

I wonder if I could ask for a little of your help with a problem that is driving me absolutely nuts!!

I currently have a way of importing csv files to a package I run and bought magicparser to parse an ebay rss feed to output it into csv format that will then allow me to import it into my other software using cron.

I have a couple issues I am trying to reslove.

Firstly, an ebay feed doesn't display the price with a decimal i.e. 2299 instead of 22.99.

Secondly, when I create the file using magic parser & your recommended csv writing script, there are no separators to define the data and therefore I can't import the created csv file.

Finally, there is also a blank row after each populated row that I need to remove.

All I am after from the data is:

Title, Deeplink & Price.

Here is the php script I am running.

<?php
$xml 
"http://shop.ebay.co.uk/i.html?_nkw=ipod&_rss=1";
require(
"MagicParser.php");
$csv "file.csv";
  function 
myRecordHandler($record)
  {
    global 
$csv;
    
// strip commas, new-line and carriage return characters from all fields
    
foreach($record as $key => $value)
    {
      
$record[$key] = str_replace(","," ",$value);
      
$record[$key] = str_replace("\n"," ",$value);
      
$record[$key] = str_replace("\r"," ",$value);
    }
    
fwrite($csv,implode(",",$record)."\n");
  }
  
$csv fopen($csv,"a");
  if (!
$csv) { print "Could not create output file - check permissions!";exit(); }
  
MagicParser_parse($xml,"myRecordHandler");
  
fclose($csv);
?>

My apologies for bothering you; I have searched and searched but I am really stumped.

Many thanks.

Hamish

Submitted by support on Tue, 2010-03-30 11:53

Hi Hamish,

If you're only after those 3 fields, I would output them directly instead of using a foreach loop to output all fields. Decimalising the price should just be a case of dividing by 100. Have a go with something like this;

<?php
  $xml 
"http://shop.ebay.co.uk/i.html?_nkw=ipod&_rss=1";
  require(
"MagicParser.php");
  
$csv "file.csv";
  
$search = array("\n","\r",",");
  function 
myRecordHandler($record)
  {
    global 
$csv;
    global 
$search;
    
fwrite($csv,str_replace($search,"",$record["TITLE"]).",");
    
fwrite($csv,str_replace($search,"",$record["LINK"]).",");
    
fwrite($csv,sprintf("%.2f",($record["RX:CURRENTPRICE"]/100))."\n");
  }
  
$csv fopen($csv,"a");
  if (!
$csv) { print "Could not create output file - check permissions!";exit(); }
  
MagicParser_parse($xml,"myRecordHandler");
  
fclose($csv);
?>

Note that you are currently opening $csv with mode "a" (append), you may want to start afresh with this version by deleting file.csv before running for the first time...

Hope this helps!
Cheers,
David.

Submitted by bigmac on Tue, 2010-03-30 16:30

Hi David,

Wow, thank you for such a super fast response, it really is appreciated.

I know I said that I only really needed 3 fields but is there anyway I can pull out the image as well? I have tried adding to your amended code but haven't managed it yet.

Many thanks again.

Kind regards,

Hamish

Submitted by support on Tue, 2010-03-30 16:45

Hi Hamish,

The image URL only appears as part of the HTML that is encapsulated within the <description> element ($record["description"] within myRecordHandler), however it can be extracted using an appropriate regular expression and the preg_match() function. Have a go with this;

<?php
  $xml 
"http://shop.ebay.co.uk/i.html?_nkw=ipod&_rss=1";
  require(
"MagicParser.php");
  
$csv "file.csv";
  
$search = array("\n","\r",",");
  function 
myRecordHandler($record)
  {
    global 
$csv;
    global 
$search;
    
preg_match('/(?<=src=")(.*?)(?=")/',$record["DESCRIPTION"],$matches);
    
$imageURL $matches[0];
    
fwrite($csv,str_replace($search,"",$record["TITLE"]).",");
    
fwrite($csv,str_replace($search,"",$record["LINK"]).",");
    
fwrite($csv,$imageURL.",");
    
fwrite($csv,sprintf("%.2f",($record["RX:CURRENTPRICE"]/100))."\n");
  }
  
$csv fopen($csv,"a");
  if (!
$csv) { print "Could not create output file - check permissions!";exit(); }
  
MagicParser_parse($xml,"myRecordHandler");
  
fclose($csv);
?>

Regular expressions are a huge topic in their own right - they are really powerful for this sort of data extraction; but luckily I just happened to do something very similar to this just the other day so it was fresh in my mind!

Hope this helps!
Cheers,
David.

Submitted by bigmac on Tue, 2010-03-30 17:16

Awesome!! Thank you very much indeed.

Have a good evening.

Cheers,

H