Hello all, I don't know how to return more than 10 results per submission. On my site, ideally someone can search, for example "Biology of the cell" and all books that match that description appear. What do I have to add to my code to get more pages? When I run the search below, amazon tells me there are 687 pages of results. thanks, Simon here is code so far: <?php class Amazon { // public key var $publicKey = ""; // private key var $privateKey = ""; // affiliate tag var $affiliateTag='affiliateTag'; /** *Get a signed URL *@param string $region used to define country *@param array $param used to build url *@return array $signature returns the signed string and its components */ public function generateSignature($param) { // url basics $signature['method']='GET'; $signature['host']='ecs.amazonaws.'.$param['region']; $signature['uri']='/onca/xml'; // necessary parameters $param['Service'] = "AWSECommerceService"; $param['AWSAccessKeyId'] = $this->publicKey; $param['Timestamp'] = gmdate("Y-m-d\TH:i:s\Z"); $param['Version'] = '2009-10-01'; ksort($param); foreach ($param as $key=>$value) { $key = str_replace("%7E", "~", rawurlencode($key)); $value = str_replace("%7E", "~", rawurlencode($value)); $queryParamsUrl[] = $key."=".$value; } // glue all the "params=value"'s with an ampersand $signature['queryUrl']= implode("&", $queryParamsUrl); // we'll use this string to make the signature $StringToSign = $signature['method']."\n".$signature['host']."\n".$signature['uri']."\n".$signature['queryUrl']; // make signature $signature['string'] = str_replace("%7E", "~", rawurlencode( base64_encode( hash_hmac("sha256",$StringToSign,$this->privateKey,True ) ) ) ); return $signature; } /** * Get signed url response * @param string $region * @param array $params * @return string $signedUrl a query url with signature */ public function getSignedUrl($params) { $signature=$this->generateSignature($params); return $signedUrl= "http://".$signature['host'].$signature['uri'].'?'.$signature['queryUrl'].'&Signature='.$signature['string']; } } ?> <?php $Amazon=new Amazon(); $parameters=array( "region"=>"com", "AssociateTag"=>'affiliateTag', 'ResponseGroup'=>'ItemAttributes, Images, Offers', "Operation"=>"ItemSearch", "BrowseNode"=>100, "SearchIndex"=>"Books", "Keywords"=>"biology of the cell" ); $queryUrl=$Amazon->getSignedUrl($parameters); echo $queryUrl; echo '<br/> <a href="'.$queryUrl.'">XML</a><p>'; $XML = simplexml_load_file($queryUrl); print_r ($XML); ?> PHP:
You have to supply the "Page" parameter - each page has 10 items (be it products, reviews, offers etc.) and you have to form some kind of loop if you want to retrieve all the items at once. Essentially you would add another parameter to your $parameters array (Pages). Each XML returned will tell you the number of pages, so you know how far to loop through (or you could loop through until you get an error). I made a mistake once of doing this and putting all the results into an Array. This of course uses memory, so if you retrieve 1000s of products that is a good way to slow down your server. Instead, do what you need to do during the loop.
Thanks for getting responding... In Amazon's user agreement, they state that you cannot exceed one call per second. See https://affiliate-program.amazon.com/gp/advertising/api/detail/agreement.html section (p). Won't looping violate this policy? If so, do they really care? In addition, I am a pretty meager php programmer (got my current code off a website), any chance you have the loop code lying around and are willing to share it?
You can use a sleep(1) command in php to delay the script by a second. However, this obviously makes it very slow. It is a bit of a pain that they limit your requests, because what they do is return a "Your request has been throttled as you have exceeded 1 call per second". So you would be best building it into your script to check for that message, and if you get it sleep(1) and try again. I dont really have generic loop code that would help you. Do something like: while (!finished) { .. get xml (using a page number) .. parse it .. add results to an array .. if no results, set $finished = true; .. increase page number by 1 }