phpapiweb-services

using amazons search not working


I have made a function that uses keywords like "Dell laptop x500" or something and it is trying to search for it. I did a hacky way of just adding it to the keywords search url, but it will give me different results compared to if i typed the text in the search box and pressed submit. Then it grabs the first results link back. Sometimes this works correctly and sometimes it does not.

function getAmazonLink($keywords){
        $keywords = preg_replace("/[^a-z0-9_\s-]/", "%20", $keywords);
        $link = "http://www.amazon.com/s/ref=nb_sb_noss_2?url=search-alias%3Daps&field-keywords=$keywords";
        //return $link;
        $content = getContents($link);
        $doc = new DOMDocument();
        $doc->loadHTML($content);
        $as = $doc->getElementsByTagName('a');
        foreach ( $as as $a){
            if($a->parentNode->nodeName == 'h3'){
                if($a->parentNode->getAttribute('class') == 'newaps'){
                    if($a->parentNode->parentNode->getAttribute('id') == 'result_0'){

                        return $a->getAttribute('href');
                    }
                }
            }
        }
        return $link;

Solution

  • Amazon, like many other online stores, will tailor the search results based on your account's purchase/search history. Since your webapp isn't using a logged in Amazon account, it is getting results which aren't tailored to anyone's account history. In the comments you asked if there is a way to "work around this", but there's nothing to work around -- it's giving you valid results, just not ones which are tailored to a specific person's Amazon account. This is the expected result, not a bug.