aircar Posted October 9, 2009 Share Posted October 9, 2009 This is the code I'm using to mine Amazon.com for books to compliment the theme (keyword) of my site. I do not have a strong (???) understanding of php, and this script was purchased online for a couple of dollar, but came with no support. I have not been able to make it work, and do not know how to effectively debug it. The file is Amazon.php <?php $search=$mainkeyword; $mtime0=getmicrotime(); //time at start if ($_GET) { $language =$_GET['language' ]; $locale =$_GET['locale' ]; $mode =$_GET['mode' ]; $page =$_GET['page' ]; if (!$search) $search =$_GET['search' ]; $searchtype=$_GET['searchtype']; $show_array=$_GET['show_array']; $show_url =$_GET['show_url' ]; $show_xml =$_GET['show_xml' ]; $type =$_GET['type' ]; } //countries with a local Amazon $Alocale=array( 'ca' => 'Canada' , // (since 2005-01-21) 'uk' => 'UK' , 'us' => 'USA' , ); //You can remove or disable the local Amazon's here that you don't want to support. //servers per country $Aserver=array( 'ca' => array( 'nor' => 'http://www.amazon.ca' , //Canadian normal server 'xml' => 'http://xml.amazon.com' , //Canadian xml server ), 'uk' => array( 'nor' => 'http://www.amazon.co.uk' , //UK normal server 'xml' => 'http://xml-eu.amazon.com', //UK xml server ), 'us' => array( 'nor' => 'http://www.amazon.com' , //USA normal server 'xml' => 'http://xml.amazon.com' , //USA xml server ), ); //product categories per country server //source for this array: kit3_1/AmazonWebServices/API%20Guide/using_international_data.htm //they are kept in the order of that list $Amode=array( 'us' => array( 'books' => 'books' , 'music' => 'popular music' , 'classical' => 'classical music' , 'dvd' => 'DVD' , 'vhs' => 'video movies' , 'electronics' => 'electronics' , 'kitchen' => 'kitchen and housewares' , 'software' => 'software' , 'videogames' => 'computer and video games', 'magazines' => 'magazines' , 'toys' => 'toys and games' , 'photo' => 'camera and photo' , 'baby' => 'baby' , 'garden' => 'outdoor living' , 'pc-hardware' => 'computers' , 'tools' => 'tools and hardware' , 'hpc' => 'Health and personal care', //20050412: new, suggested by DvH 'gourmet' => 'Gourmet' , //20050412: new, suggested by DvH ), 'uk' => array( 'books-uk' => 'books' , 'music' => 'popular music' , 'classical' => 'classical music' , 'dvd-uk' => 'DVD' , 'vhs-uk' => 'video movies' , 'electronics-uk' => 'electronics' , 'kitchen-uk' => 'kitchen and housewares' , 'software-uk' => 'software' , 'video-games-uk' => 'computer and video games', 'toys-uk' => 'toys and games' , ), ); //search types //see for example kit3_1/AmazonWebServices/API%20Guide/index.html $Asearchtype=array( 'ActorSearch' => 'Actor/Actress' , 'ArtistSearch' => 'Artist/Musician', 'AsinSearch' => 'ASIN/ISBN' , //give an ASIN as the search string 'AuthorSearch' => 'Author' , 'BlendedSearch' => 'Blended' , //this will search in several categories (modes) 'BrowseNodeSearch' => 'Browse node' , 'DirectorSearch' => 'Director' , 'ExchangeSearch' => 'Exchange' , 'KeywordSearch' => 'Keyword' , 'ListmaniaSearch' => 'Listmania' , 'ManufacturerSearch' => 'Manufacturer' , 'MarketplaceSearch' => 'Marketplace' , 'PowerSearch' => 'Power' , 'SellerSearch' => 'Seller' , 'SimilaritySearch' => 'Similarity' , 'TextStreamSearch' => 'TextStream' , //will search inside of books? 'UpcSearch' => 'UPC' , 'WishlistSearch' => 'Wishlist' , ); //error messages //and how to handle them $Aerror=array( 'There are no exact matches for the search.'=>'print', ); //all the possible languages $Alanguage=array( 'en' => 'English', //also spoken in the USA and Australia and parts of Canada ); //is character a regula abc or ABC letter? function is_alpha($c) { return $c>='A' and $c<='Z' or $c>='a' and $c<='z'; } //set these to correspond with your site //the user can overrule them $Adefault=array( 'language' =>'en', //what language to render the page in 'locale' =>'us', //which country's products? 'mode' =>'books', //what product category? 'page' =>'1', //first page is 1 not 0 'search' =>'totally+hot+info', //what to search for? 'searchtype'=>'KeywordSearch', //what kind of search? 'show_array'=>false, //debug: show complete array? 'show_url' =>false, //debug: show XML request url to be send to Amazon? 'show_xml' =>false, //debug: show incoming XML code? 'type' =>lite, //heavy will cause Amazon to send a much bigger (typically six times bigger) file ); //other flags: $f_rich =true; //when set to true this will convert the URL so you'll get 7.5% on qualifying books instead of 5%. It doesn't work yet outside of the USA. //change it to false when you prefer to get only 5% ;-) //see http://forums.prospero.com/n/mb/message.asp?webtag=am-assosdev&msg=6.1&ctx=0, or search the forum for 'nosim' //This is only helpful for certain books //correction: keep this option set to true, to prevent the showing of Amazon's annoying distraction pages. $f_user_search=false; //add a user search form and allow user searches? //============================================================================== //==You don't have to change anything after this line, to start earning money.== //============================================================================== //if the user hasn't overruled them yet, use the default if (!isset($language )) $language =$Adefault['language' ]; //20040815: isset()'s added if (!isset($locale )) $locale =$Adefault['locale' ]; if (!isset($mode )) $mode =$Adefault['mode' ]; if (!isset($page )) $page =$Adefault['page' ]; if (!isset($search )) $search =$Adefault['search' ]; if (!isset($searchtype)) $searchtype=$Adefault['searchtype']; if (!isset($show_url )) $show_url =$Adefault['show_url' ]; if (!isset($show_xml )) $show_xml =$Adefault['show_xml' ]; if (!isset($show_array)) $show_array=$Adefault['show_array']; if (!isset($type )) $type =$Adefault['type' ]; //use which servers? $norserver=$Aserver[$locale]['nor']; $xmlserver=$Aserver[$locale]['xml']; $dev_token='D2WMCOIPS9D14E'; //you can replace this with your own developer token if you have changed the script a lot //get one from http://associates.amazon.com/exec/panama/associates/join/developer/application.html //see: //http://forums.prospero.com/n/mb/message.asp?webtag=am-assocdevxml&msg=378.1&ctx=0 //form the URL to send to Amazon as a search request $file="$xmlserver/onca/xml3?". //should also still work with xml2 "$searchtype" .'='.urlencode($search) .'&'. 'dev-t' .'='.$dev_token .'&'. 'f' .'='.'xml' .'&'. 'locale' .'='.$locale .'&'. 'mode' .'='.$mode .'&'. 'page' .'='.$page .'&'. 't' .'='.$Aassociates_id[$locale].'&'. 'type' .'='.$type ; if ($show_url) { //change this to true, if you want to see the exact search URL printed echo "<hr />\n"; echo "Debugging:<br />\n"; echo "Asking Amazon's XML interface for:<br />\n"; echo $file."<br />\n"; echo "<hr />\n"; } //do you want to use caching? $cache_time=3600; //in seconds how long a cache entry may be used //Use 0 for no caching or don't make a cache directory //Amazon has a maximum of one day for most data, //but one hour for prices unfortunately, I think //if you want to cache make a subdirectory called cache //and make sure Apache/PHP can write into it. Under Unix: chmod a+w cache $time0=time(); $cache='cache/'; //directory where to cache things //should we cache? $f_cache=($cache_time && is_dir ($cache) && is_readable($cache) && is_writable($cache)); //first we are cleaning out the cache if ($f_cache and $hd=opendir($cache)) { while ($fn=readdir($hd)) { if ($time0-filemtime($cache.$fn)>=$cache_time) { //is the file too old? if (!($fn=='.' or $fn=='..')) unlink($cache.$fn); //remove it } } closedir($hd); } $cache_file_name=$cache.md5($file); //determine the name of the cache file if ($f_cache and //are we caching? is_readable($cache_file_name) and //file already readable in cache? $hf=fopen($cache_file_name,'r')) { //can it be opened? $A=unserialize(fread($hf,filesize($cache_file_name))); fclose($hf); } else { $mtime1=getmicrotime(); //time before Amazon if ($hf=fopen($file,'r')) { //else open the file from Amazon for ($sfile='';$buf=fread($hf,1024) { //read the complete file (binary safe) 20040715: $sfile='' added $sfile.=$buf; } fclose($hf); $mtime2=getmicrotime(); //time after Amazon if ($show_xml) { //print the raw XML? echo "<pre>\n"; echo htmlentities($sfile); //print the file echo "</pre>\n"; echo "<hr />\n"; } $A=xml2array(utf8_decode($sfile)); //interpret the XML if ($f_cache and !($A['ErrorMsg'] or $A['ProductInfo']['ErrorMsg'])) { if ($hf=fopen($cache_file_name,'w')) { fwrite($hf,serialize($A)."\n"); fclose($hf); } } } else die(xu("can't get data from Amazon").".<br />\n"); } //echo "<h2>".xu('searching for')." '$search'</h2>"; //you can change the h2's to h1's //now print the stuff if ($A) { //echo "Searchtype is '$searchtype'<br />\n"; //echo "<p />\n"; if ($show_array) { //show the array? print_array($A); //show the complete array } if ($e=$error_msg) { switch ($Aerror[$e]) { case 'print': echo "<!--$e--><br />\n"; break; default: echo xu('<!--Amazon returned the error').': '.$e."<br />\n"; echo "<br />\n"; echo xu('please add this error message to the '). "<a href='http://www.chipdir.nl/amazon/errorlist.php?do=add&da=".urlencode($e)."&ur=".$file."'>". xl('database')."</a>.<br />-->\n"; break; } } else { switch ($searchtype) { case 'BlendedSearch': foreach ($A['BlendedSearch']['ProductLine'] as $d) { echo "<hr />\n"; echo "<b>".xu('category' ).": ".$d['Mode' ]."</b><br />\n"; echo "<b>".xu('relevance').": ".$d['RelevanceRank']."</b><br />\n"; print_mode($d); } break; default: // echo "<hr />\n"; print_mode($A); break; } } } function print_mode($A) { global $language; global $locale; global $mode; global $page; global $search; global $searchtype; $np=$A['ProductInfo']['TotalResults']; // echo '<b>'.xu('number of products').": $np</b><br />\n"; // echo '<b>'.xu('page').' '.($page).' '.xl('of').' '.(int)(($np+9)/10)."</b><br />\n"; // echo "<p />\n"; echo '<table border="0" cellpadding="0" cellspacing="0" id="tableamaz">' . "\n"; if (is_array($D=$A['ProductInfo']['Details']) and $D[0]) { $count=1; foreach ($D as $i => $E) { printProduct($E); global $num_amazon; $count++; echo "<!-- $count $num_amazon -->\n"; if ($count > $num_amazon) {break;} } } else { printProduct($D); } echo "</table>\n"; } if ($f_user_search) { //the next section adds a search box for the users echo "<hr />\n"; echo "<form method='get' action='".$_SERVER['PHP_SELF']."'>\n"; //echo "<input type='hidden' name='language' value='$language' />\n"; //echo "<input type='hidden' name='locale' value='$locale' />\n"; //print " <input type='text' name='ty'>Search type </input><br />\n"; echo "<table>"; echo "<tr><td>".xu('product category:')."</td><td><select name='mode'>\n"; foreach ($Amode[($locale=='uk' || $locale=='jp')?'us':$locale] as $i=>$d) { //uk and jp not ready yet echo "<option value='$i'".($mode==$i?' selected':'').">".xl($d)."</option>\n"; } echo "</select></td></tr>\n"; echo "<tr><td>".xu('search type:')."</td><td><select name='searchtype'>\n"; foreach ($Asearchtype as $i=>$d) { echo "<option value='$i'".($searchtype==$i?' selected':'').">".$d."</option>\n"; } echo "</select></td></tr>\n"; echo "<tr><td>".xu('which server:')."</td><td><select name='locale'>\n"; foreach ($Alocale as $i=>$d) { echo "<option value='$i'".($locale==$i?' selected':'').">".xl($d)."</option>\n"; } echo "</select></td></tr>\n"; echo "<tr><td>".xu('language for page:')."</td><td><select name='language'>\n"; foreach ($Alanguage as $i=>$d) { echo "<option value='$i'".($language==$i?' selected':'').">".xu($d)."</option>\n"; } echo "</select></td></tr>\n"; echo "<tr>"; echo "<td>".xu('search string:')."</td>"; echo "<td><input type='text' name='search' value='$search' /></td>\n"; echo "</tr>\n"; echo "</table>"; echo "<input type='submit' value='Search' />\n"; echo "</form>\n"; //end of the search box section } //convert an XML-string to an array function xml2array($s) { if ($Atmp1=explode('>',$s,2) and ereg('^<\?(xml .*)\?$',$Atmp1[0],$Atmp2)) { $A['xml']=$Atmp2[1]; xml2array_sub($A,$Atmp1[1]); } return $A; } function xml2array_sub(&$A,$s) { global $error_msg; for ($c=0;ereg('<([^<>/ ]*)( [^<>]*)?/>(.*)',$s,$Atmp1) or ereg('<([^<>/ ]*)( [^<>]*)?>(.*)',$s,$Atmp1) and $Atmp2=explode('</'.$Atmp1[1].'>',$Atmp1[3],2);++$c) { $E=array(); $tag=$Atmp1[1]; if ($Atmp1[2]) { $Atmp3=explode(' ',substr($Atmp1[2],1)); foreach ($Atmp3 as $d) { $Atmp4=explode('=',$d,2); $Atmp4[1]=ereg_replace('^"(.*)"$','\1',$Atmp4[1]); $E[$Atmp4[0]]=$Atmp4[1]; } } if ($Atmp2) { if (!xml2array_sub($E,$Atmp2[0])) { $E=$Atmp2[0]; } $s=$Atmp2[1]; //rest of the xml-code $Atmp2=false; } else { $s=$Atmp1[3]; //rest of the xml-code } if (isset($A[$tag])) { //does it already exist? 20040715: isset() added if (!is_array($A[$tag]) or !$A[$tag][0]) { //not already a numberically indexed array? $Atag=$A[$tag]; //remove element again unset($A[$tag]); $A[$tag][]=$Atag; //make old element first element of it's former self } if ($E) { $A[$tag][]=$E; //add new element } } else { if ($E) { $A[$tag]=$E; //put it in the array under it's tag name if ($tag=='ErrorMsg') $error_msg=$E; //make this a global error message, because it can be anywhere in the XML code } } } return $c; } //print one Amazon product function printProduct($E) { global $Aassociates_id; global $dev_token; global $f_rich; //do you want 15% or 5%? (only for discounted books) global $locale; global $mode; global $norserver; global $search; if ($E) { //rebuild the URL if you want 7.5% instead of 5% and want to get rid of the distracting inbetween page: $url=($f_rich /*and $mode=='books' and $locale=='us'*/)?"$norserver/exec/obidos/ASIN/".$E['Asin'].'/ref=nosim/'.$Aassociates_id[$locale].'?dev-t='.$dev_token:$E['url']; echo "<tr>"; #echo "<td valign='top' width='30%'>"; #echo "</td>"; echo "<td valign='top' width='100%' align='center'>"; if ($d=$E['ProductName']) { echo "<a href='$url' target='_blank'>$d</a>"; } if ($d=$E['ImageUrlMedium']) { echo "<br><a href='$url' target='_blank'><img vspace='5' hspace='0' src='$d' border=0 width='100' alt=\"".$search." product image\"></a></td></tr><tr><td align='center'>"; } echo ''; if (is_array($dd=$E['Authors']['Author'])) { // MORE THAN 1 AUTHOR!!! Show All Authors echo "-"; echo 'By' . ": "; foreach ($dd as $i=>$d) { if ($i) print ', '; echo "<b>" . $d . "</b>"; } echo "<br>"; } else if ($d=$dd) { // ONLY 1 AUTHOR!!! Show The Author echo "-"; echo ('By') . ": <b>" . $d . "</b>"; echo "<br>"; } $da=$E['Reviews']['AvgCustomerRating']; if ($da and $ds) print ", "; echo ""; echo ''; echo "-"; echo 'Price' . ": "; $do=$E['OurPrice' ]; $du=$E['UsedPrice']; // SHOW NEW PRICE if ($do) { echo "<b>$do</b> (" . 'New' . ")"; } // SHOW USED PRICE if ($du) { echo "<br><b>$du</b> (" . 'Used' . ")"; } echo ""; echo "<br><br>"; echo "</td>"; echo "</tr>\n"; } } function print_array($A) { echo "<table border=1>\n"; for (reset($A);list($ky,$da)=each($A) { echo "<tr><td valign=top>$ky</td><td>"; if (is_array($da)) print_array($da); else { echo "<pre>"; echo htmlentities($da); echo "</pre>"; } echo "</td></tr>\n"; } echo "</table>\n"; reset($A); } $mtime3=getmicrotime(); //time at end function getmicrotime() { list($us,$s)=explode(' ',microtime()); return (float)$us+(float)$s; } function xu($str) { echo($str); } ?> This is how I authorize the script into my web page through my config.php, everything else in the config works as it is supposed to. <?PHP //Modify your data in this section //---------------------------------------------------------------------------------- $username = "___"; // Admin username $password = "___"; // Admin password //---------------------------------------------------------------------------------- //---------------------------------------------------------------------------------- //First, define the modules you want to be displayed : Google, Yahoo, Amazon and the number of news displayed $show_google = "1"; // Show google ads ? $show_yahoo ="0"; //Show yahoo ads? WARNING : Be sure not to have both show_google and show_yahoo enabled $show_amazon = "1"; // Show Amazon feed ? //----------------------------------------------------------------------------------- // Configuration of your local installation and personal site definitions $templatefoldername="includes"; //folder where templates are installed relative to the DocumentRoot. $mysitename = "bikes"; //name for your site $mainkeyword = "bikes"; //primary site keyword //----------------------------------------------------------------------------------- // your personnal affiliate ID in the following programs $Aassociates_id=array( 'uk' => 'bikesx10host-21' , // <-- replace INVALID with your amazon.co.uk associate ID 'us' => 'bikes-20' ,// <-- replace INVALID with your amazon.com associate ID ); $num_amazon = "5"; //Max number of amazon products to return //------------------------------------------------------------------------------------- ?> This is the call for the script in my web page, all other calls and called items work, leading me to believe a problem in the Amazon.php file. I am not skilled enough to find the problem. <?php if(isset($keyword)) { ?> <tr> <td class="menuTitle">More Resources</td> </tr> <tr> <td><?php include("amazon.php"); ?></td> </tr> <?php } ?> This is the error display I get on my website whenever I try to use this script. Warning: fopen(http://xml.amazon.com/onca/xml3?KeywordSearch=bikes&dev-t=D2WMCOIPS9D14E&f=xml&locale=us&mode=books&page=1&t=x10host-20&type=lite) [function.fopen]: failed to open stream: HTTP request failed! HTTP/1.1 410 Gone in /home/___/public_html/bikes/includes/amazon.php on line 232 can't get data from Amazon. I appreciate any help, theories, repairs, or advise as to whether this will ever work. Thank you in advance Air EDIT: you can get much better help if you use CODE tags Quote Link to comment https://forums.phpfreaks.com/topic/177062-i-posted-this-problem-on-the-help-forum-and-was-advised-to-post-it-here-instead/ Share on other sites More sharing options...
pastcow Posted October 9, 2009 Share Posted October 9, 2009 I suggest you visit the link you tried to open http://xml.amazon.com/onca/xml3?KeywordSearch=bikes&dev-t=D2WMCOIPS9D14E&f=xml&locale=us&mode=books&page=1&t=x10host-20&type=lite It explains that "Amazon Ecommerce Web Service 3.0 has been deprecated after many years of useful service on March 31st 2008" I'm guessing that they no longer the API you are using... Quote Link to comment https://forums.phpfreaks.com/topic/177062-i-posted-this-problem-on-the-help-forum-and-was-advised-to-post-it-here-instead/#findComment-933610 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.