i want to create a simple search box with a search button (kinda like google adsense search) for amazon. haha..wrong code... Code (markup): is a code snippet i found online which works..but doesn't pass the affiliate link ofcourse. anyone have any pointers on how to do this?
You do mean Amazon, right? Because the code you quoted was for eBay. Wouldn't it be easier just to use the search widget supplied through Amazon Associates? http://affiliate-program.amazon.com/gp/associates/network/build-links/searchbox/main.html
sorry..i meant amazon. yea..that'll work but i was hoping to be able to make a simple search box...just a rectangular input box and button. nice and simple.
Yes, sure, and the widget is a pain because it's an iframe. I believe all you need in the way of parameters is a link like this: [url]http://www.amazon.com/s/?url=search-alias%3Daps&field-keywords=sausage&tag=your-affiliate-code[/url] Code (markup): You need a couple of hidden fields to store value "url" as "search-alias%3Daps" (some sort of alias to the actual url, works fine, so no worries I reckon - though we can't be sure what 'aps' is) and to store "tag" as "your-affiliate-code", and then the GET url is obviously http://www.amazon.com/s/. Really can't guarantee it will work - please don't take my word for it. Test it a few days, pretend to order some things through the search and use a tag you have made just for that purpose so you are sure the clicks came through it, and then see if the clicks come up in your daily report (takes a good 24 hours to show).
thanks for the help. i'll give it a try. i emailed amazon and they gave me back the templated response to use the iframe...when i told them i didn't want to use their iframe search haha.
Surprise surprise... Right, so try constructing a link like the one I suggested using a simple form, and see if it works. I haven't tried it myself, but I understand that pretty much any link to Amazon will insert your affiliate cookie if you use your affiliate code as "tag" value.
I put together a VERY BASIC PHP function which will search Amazon using Amazon's web services AWS. You will need to add your access key and associates ID. "Blended" searches all Amazon's categories. For more specific searches replace "Blended" with the desired SearchIndex such as Books, Tools, DVD etc. Please note that the function needs additional work for handling products that don't include a photo. If you need help with that I will see what I can do. <?php function amazon_search($keyword) { $AWSAccessKeyId = ' '; // AWS Access Key $AssociateTag = ' '; // Associates ID $SearchIndex = 'Blended'; $xml = simplexml_load_file('http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AssociateTag='.$AssociateTag.'&AWSAccessKeyId=0VDDM3242PBSEPZ6D4R2&Operation=ItemSearch&Keywords='.$keyword.'&ResponseGroup=Medium&SearchIndex='.$SearchIndex); foreach ($xml->Items->Item as $Item) { $Title = $Item->ItemAttributes->Title; $URL = $Item->SmallImage->URL; $DetailPageURL = $Item->DetailPageURL; echo '<img src="'.$URL.'" border="0" alt=""><br>'; echo '<a href="'.$DetailPageURL.'">'.$Title .'</a><p>'; } } ?> <form name="search" action="<?php echo $php_self; ?>" method="post"> Keyword: <input name="keyword" type="text" /> <input type="submit" value="Search" /> </FORM> <?php amazon_search($_POST['keyword']); ?> PHP:
Nice work! Yes, you could easily solve the image problem by doing something like: if (!$Item->SmallImage->URL) {$URL = $DefaultPic;} else {$URL = $Item->SmallImage->URL;} /* NOT TESTED!!*/ Code (markup): ... where $DefaultPic is the URL to a default pic to display if there is no Small Image URL present. There is probably any other number of possible situations you would have to trap too - the part of programming we all hate... Actually, I think the OP wanted a search box which would take the user direct to Amazon rather than display the results on his own site, but it's a handy little bit of code. Shows how simple using Amazon Web Services can be.
dang THANKS! that's more than what i was expecting and it's much better. running into a few errors.... Notice: Undefined index: keyword in /var/www/vhosts/httpdocs/ on line 33 Fatal error: Call to undefined function: simplexml_load_file() in /var/www/vhosts/httpdocs/on line 15
Are you using PHP5 hosting? simplexml_load_file doesn't work with PHP4. I can probably supply code for PHP4 if necessary.
PHP4's XML parser leaves a lot to be desired as does my coding! However this should work pretty well for simple Amazon searches. If you want to view the XML uncomment the echo XML line. <? function amazon_search($keyword) { $AWSAccessKeyId = " "; //AWS Access Key $AssociateTag = " "; // Associates ID $keyword = str_replace (" " , "+" , $keyword); $SearchIndex = "Kitchen"; $url = 'http://ecs.amazonaws.com/onca/xml?Service=AWSECommerceService&AssociateTag='.$AssociateTag.'&AWSAccessKeyId='.$AWSAccessKeyId.'&Operation=ItemSearch&Keywords='.$keyword.'&ResponseGroup=Small,Images&Version=2005-02-23&SearchIndex='.$SearchIndex; //echo "<a href='$url'>XML</a><p>"; $xml = @implode("",file($url)); $parser = xml_parser_create(); xml_parser_set_option($parser,XML_OPTION_SKIP_WHITE,1); xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING,0); xml_parse_into_struct($parser,$xml,$values,$index); xml_parser_free($parser); for($i=0; $i<count($index['Item']); $i++) { if($values[$index['Item'][$i]]['type']=='open') { for($j=$index['Item'][$i]; $j<$index['Item'][$i+1]; $j++) { if($values[$j]['tag'] == 'URL'){ $URL = $values[$j]['value']; }elseif($values[$j]['tag'] == 'Width'){ $Width = $values[$j]['value']; if ( $Width < 90 ) { echo '<img src="'.$URL.'"/><br>'; } } } for($j=$index['ItemAttributes'][$i]; $j<$index['ItemAttributes'][$i+1]; $j++) { if($values[$j]['tag'] == 'Title'){ $Title = $values[$j]['value']; for($j=$index['Item'][$i]; $j<$index['Item'][$i+1]; $j++) { if($values[$j]['tag'] == 'DetailPageURL'){ $DetailPageURL = $values[$j]['value']; echo '<a href="'.$DetailPageURL.'">'.$Title .'</a><p>'; } } } } } } } ?> <form name="search" action="<?php echo $php_self; ?>" method="POST">Keyword: <input name="keyword" type="text" /><input type="submit" value="Search" /></form> <?php amazon_search($_POST['keyword']); ?> PHP:
thanks! i copied and pasted it and it gave me the following errors: Notice: Undefined index: keyword in /var/www/vhosts/httpdocs/test/amazon.php on line 66 Notice: Undefined index: Item in /var/www/vhosts/httpdocs/test/amazon.php on line 27 Code (markup): line 66 for me was: <?php amazon_search($_POST['keyword']); ?> Code (markup): line 27 is: for($i=0; $i<count($index['Item']); $i++) { Code (markup):
Well I assume the error is because the function has no keyword to process? Possibly something to do with register_globals? You might try adding the following code above the Amazon url. You will then be able to select a default search. Just replace "pepsi" with whatever you prefer. if ( $keyword == "" ) { $keyword = "pepsi"; } else { $keyword = $keyword; } Code (markup):
Actually, this whole code should really be rewritten for PHP5 - PHP4 is soon to be deprecated, and PHP5 has a whole bunch of XML parsing functions now which should make things a bit simpler.