How To Create A Simple Amazon Search

Discussion in 'Amazon' started by chodomade, Jul 8, 2008.

  1. #1
    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?
     
    chodomade, Jul 8, 2008 IP
  2. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #2
    markowe, Jul 9, 2008 IP
  3. chodomade

    chodomade Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #3
    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.
     
    chodomade, Jul 9, 2008 IP
  4. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #4
    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).
     
    markowe, Jul 9, 2008 IP
  5. chodomade

    chodomade Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    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.
     
    chodomade, Jul 10, 2008 IP
  6. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #6
    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.
     
    markowe, Jul 10, 2008 IP
  7. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #7
    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:
     
    myhart, Jul 10, 2008 IP
  8. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #8
    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.
     
    markowe, Jul 10, 2008 IP
  9. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Thanks markowe for the default image code it works fine!
     
    myhart, Jul 10, 2008 IP
  10. chodomade

    chodomade Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #10
    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
     
    chodomade, Jul 10, 2008 IP
  11. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #11
    Are you using PHP5 hosting? simplexml_load_file doesn't work with PHP4. I can probably supply code for PHP4 if necessary.
     
    myhart, Jul 10, 2008 IP
  12. chodomade

    chodomade Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #12
    yea..i haven't upgraded to PHP5 yet. php4 only. i'd appreciate if you had the time. thanks!
     
    chodomade, Jul 10, 2008 IP
  13. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #13
    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:
     
    myhart, Jul 10, 2008 IP
  14. chodomade

    chodomade Peon

    Messages:
    159
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #14
    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):
     
    chodomade, Jul 11, 2008 IP
  15. myhart

    myhart Peon

    Messages:
    228
    Likes Received:
    11
    Best Answers:
    0
    Trophy Points:
    0
    #15
    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):
     
    myhart, Jul 11, 2008 IP
  16. markowe

    markowe Well-Known Member

    Messages:
    1,136
    Likes Received:
    26
    Best Answers:
    0
    Trophy Points:
    165
    #16
    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.
     
    markowe, Jul 11, 2008 IP
  17. jamie28

    jamie28 Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #17
    thanks for codes
     
    jamie28, Jul 12, 2008 IP
  18. Lump

    Lump Peon

    Messages:
    54
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #18
    thank you too
     
    Lump, Jul 27, 2008 IP