homepage Welcome to WebmasterWorld Guest from 54.196.168.78
register, free tools, login, search, subscribe, help, library, announcements, recent posts, open posts,
Subscribe to WebmasterWorld
Home / Forums Index / Code, Content, and Presentation / XML Development
Forum Library, Charter, Moderators: httpwebwitch

XML Development Forum

    
Amazon program to PHP
Sandro87




msg:3924824
 3:03 pm on Jun 2, 2009 (gmt 0)

Hello,
I just signed up to amazon and axelmusic, amazon has a good implemented XML feed and axelmusic said they also have a XML feed by request.

What I need to do is to get some information of every item for my website coming from stores like price, release date, list price and store them into my db. So basically I need to parse the XML feed and get variables in PHP so I can play with them. I don't know and I'm sure it can be done because a lot of sites do it.

Anyway is there anyone who uses Amazon? I tried to analyze the full item feed but I couldn't find where are the list price, release date, date added.

Thank you.

 

janharders




msg:3924828
 3:19 pm on Jun 2, 2009 (gmt 0)

search for "ecs documentation" if you want technical documents, or ask your favorite search engine for "amazon scripts php", there are plenty free scripts out there you can learn from or extend.

Sandro87




msg:3924898
 5:11 pm on Jun 2, 2009 (gmt 0)

I'm starting to understand with the documentation, the problem is that all guides miss the explanation how to "convert" Xml to PHP!

Sandro87




msg:3924978
 6:39 pm on Jun 2, 2009 (gmt 0)

I found a script to convert xml to array.
I was thinking that if I retrieve the prices for a lot of items with a while PHP function considering this operation usually take 1.5 second each wouldn't that crash for exceeding execution time? If yes, I just wasted my time and I don't see how to solve this since hostings don't allow to set 0 seconds

janharders




msg:3925007
 7:10 pm on Jun 2, 2009 (gmt 0)

mh, how many items are we talking about?
generally, you have two limiting factors: php max execution time and amazon's limit of 1 request / second (allthough that is a guideline, they won't terminate your account if you do 10 requests per second on peak times, it's more of an average rate you shouldn't need to be over).

amazon's limit can also be worked around by putting multiple operations into one request. the php max execution time might be harder to work with. parsing xml ist quite time consuming, so you might have to get used to retrieving few items if your host has a very restrictive setting.

what exactly do you want to do?

Sandro87




msg:3925140
 10:30 pm on Jun 2, 2009 (gmt 0)

For starting like 50 items but it will grow to thousands in the next months :(
I should try to create some kind of code that stops in the "while", but I don't think i'll be able to

Sandro87




msg:3925167
 11:33 pm on Jun 2, 2009 (gmt 0)

With my hosting seems like I can override the php time, But the problem still remains the amazon's limit in fact if I try a loop with while for 200 examples items there's a lot of data missing with intervals

janharders




msg:3925372
 6:54 am on Jun 3, 2009 (gmt 0)

if your application needs to get thousands of items for each call, something is wrong. you can do searches on amazon's side and page through the results.

also, if you know the ItemIDs, you can request multiple items in one request, so a while-loop for 100 items wouldn't result in 100 requests, but in 10 or less. check the docs for multiple operations.

Sandro87




msg:3925606
 1:04 pm on Jun 3, 2009 (gmt 0)

I didn't know that! Yes I know the ItemIDs indac I found that separating them with "," will give me all the results in one page. The problem is that the given xml converted to array is something like
[items]
[item]
price
[/item}
[item]
price
[/item}
[/items]

there's no identification for each item (only array 0,1,2 and so on) so I can't find a way to store the price to a relative proper ItemID into the database.
I could to a loop into a loop but if the items are more than 100 I should find a way to divide them into parts. I should work on that, any advice?

[edited by: Sandro87 at 1:29 pm (utc) on June 3, 2009]

janharders




msg:3925750
 4:42 pm on Jun 3, 2009 (gmt 0)

the array just filters out the price or does it just transform the xml to an array?
in that case, it should have Item->ASIN so you can identify the item.
if you need an "ASIN => price"-array, I'd suggest a foreach loop iterating over all the items,
like

$asinprice = array();
foreach($myArray['Items'] AS $item)
{
$asinprice[ $item['ASIN'][0]['Value'] ] = $item['Price'][0]['Value'];
}

not sure about the $item['ASIN'][0]['Value']-thingy, though I remember xml2array-functions in php usually construct something similar.

Sandro87




msg:3925868
 7:12 pm on Jun 3, 2009 (gmt 0)

just xml to array with all arrays into it.

No, because the xml structure of amazon puts the ASIN into a single tag so the array is like:

$item[0][value]
$item[1][value].

The ASIN tag is put into "item" in first line and then closed right away, it doesn't include all the info, the item tag does.

item
array 0
asin /asin
price /price
/item

item
array 1
asin /asin
price /price
/item

it should be like this to make it to work like your example that's why I'm kind of confused how to do it.

item
asin
price /price
/asin
/item

httpwebwitch




msg:3925871
 7:17 pm on Jun 3, 2009 (gmt 0)

I've had some success using PHP's "SimpleXML" class.
[ca3.php.net...]

It turns an XML string into a PHP object that can be traversed using simple loops and object syntax. It also has a fairly reliable XPATH method.

Working with SimpleXML isn't as "simple" as you'd hope, and it may take some work to get the hang of it. But once you "get" it, it's a nice tool to have in your XML/PHP arsenal.

Sandro87




msg:3927843
 2:39 pm on Jun 6, 2009 (gmt 0)

Thanks i'll try it!
Anyway I have another problem. Axelmusic (the store) gave me an xml feed as an affiliate only available with ALL the prices so no item request, you'll have to parse all the data. They told me that a lot of their affiliate use that with no problem. Doesn't work for me the problem is that the file is like 11MB big of xml so you can imagine how big really is. If I try to open it locally it takes 30 seconds to download then when PHP parses it, all 2GB of RAM is used and the browser crashes. If I try on the server (shared hosting) it won't even open, just give me "error" as result. No php error. I think it's the error of the server for too much RAM used. How do you think I can open it? Maybe their affiliates use 4xQuadcore/16GB servers to parse it :D
Maybe SimpleXML can do it.

[edited by: Sandro87 at 2:41 pm (utc) on June 6, 2009]

janharders




msg:3927865
 3:23 pm on Jun 6, 2009 (gmt 0)

No, SimpleXML won't do. You'll need a parser that can process elements while he reads it, thus not needing to read the complete xml before starting to actually work on the data.

Have a look at XML Parser and XMLReader in the php docs. Check which one is installed on your server and work with that.

Sandro87




msg:3927913
 4:35 pm on Jun 6, 2009 (gmt 0)

I was able to read it and extract info with SimpleXML. The major problem still remains though. With Amazon I can make requests for multiple items but I'll be sure that the results will be in order in fact I know that to array 0 corresponds the first time requests so it's really easy to manage.
With a full XML of Axelmusic it's complicated, whatever function or parsers I use no one will give me an ID of the store associated to the price because the tree is <item><id></id><price></price></item>, since the ID tag doesn't not contain the price and I can't know the item number (to process as an array) of the actual number the only thing I could think of is to parse every time and create 2 arrays with ID and price and then to join them. I should work but this means parsing 70.000 items. It's impossible and memory consuming! If they used another tree system it would be really easier

janharders




msg:3927917
 4:59 pm on Jun 6, 2009 (gmt 0)

why don't you parse their file once and put them into a database from which you can fetch the data on demand?

Sandro87




msg:3927928
 5:22 pm on Jun 6, 2009 (gmt 0)

Because of 70.000 items I need like 200 max.
I was able to create something but I miss a thing

// xml source
$xml_file = "am_titlesEUR.xml";

//id examples , they'd come from a databse query
$ids = array("031398109389","097361427843","786936787665","883929039616","883929034178","031398240785");

$xml = new SimpleXmlElement(file_get_contents($xml_file));

foreach($xml->result->row as $row){

// here I need to make: if this row is = to ONE of ANY values of the $ids arrya then do the function.

if ($row->ID == $ids[]){
echo $row->Title ." <b>". $row->Price ."</b><br/>";
}
}

Is there a php function for that?

Sandro87




msg:3927935
 5:41 pm on Jun 6, 2009 (gmt 0)

FOUND!

foreach($xml->result->row as $row){
if (in_array($row->ID, $ids)){
echo $row->Title ." <b>". $row->Price ."</b><br/>";
}
}

[edited by: Sandro87 at 5:41 pm (utc) on June 6, 2009]

Sandro87




msg:3928201
 12:37 pm on Jun 7, 2009 (gmt 0)

Do you have an idea how can I divide the loop of an array of +100items into 10 per loop in one script?

janharders




msg:3928491
 6:09 am on Jun 8, 2009 (gmt 0)

I'm not sure what you're trying to do with that loop. Can you explain what you are thinking of?

Sandro87




msg:3928577
 11:49 am on Jun 8, 2009 (gmt 0)

I was actually able to do it and it works. You said that I can request only 10 items per request right?I made a script to get the ids and divide them into loops of 10 ids

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / XML Development
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved