recordcount = 0; $fp = fopen ($wsurl, "r"); if ($fp) { $xml = ""; while ($data=fgets($fp, 32000)) $xml .= $data; fclose ($fp); $this->xml = utf8_decode($xml); } else return ""; if ($this->debug) echo "

".htmlentities($xml)."

"; return $xml; } function parse () { $items = $this->extractstring ($this->xml, "", ""); if ($items) { $i = 0; $item = $this->extractall ($items, "", ""); $n = count ($item); //echo $n; for ($i=0; $i<$n; $i++) { $asin = $this->extractstring ($item[$i], "", ""); $smallimage = $this->extractstring ($item[$i], "", ""); $smallimage_url = $this->extractstring ($smallimage, "", ""); $smallimage_height = $this->extractstring ($smallimage, "", ""); $smallimage_width = $this->extractstring ($smallimage, "", ""); $mediumimage = $this->extractstring ($item[$i], "", ""); $mediumimage_url = $this->extractstring ($mediumimage, "", ""); $mediumimage_height = $this->extractstring ($mediumimage, "", ""); $mediumimage_width = $this->extractstring ($mediumimage, "", ""); $largeimage = $this->extractstring ($item[$i], "", ""); $largeimage_url = $this->extractstring ($largeimage, "", ""); $largeimage_height = $this->extractstring ($largeimage, "", ""); $largeimage_width = $this->extractstring ($largeimage, "", ""); $itemattributes = $this->extractstring ($item[$i], "", ""); //if ($this->itemtype=="Music") //{ $artist = $this->extractstring ($itemattributes, "", ""); $label = $this->extractstring ($itemattributes, ""); //} //else //{ $author = $this->extractstring ($itemattributes, "", ""); $publisher = $this->extractstring ($itemattributes, "", ""); //} $title = $this->extractstring ($itemattributes, "", ""); $listprice = $this->extractstring ($itemattributes, "", ""); $amount = $this->extractstring ($listprice, "", ""); $currencycode = $this->extractstring ($listprice, "", ""); $this->item[$i] = new AmazonItem; $this->item[$i]->amazon_asin = $asin; $this->item[$i]->smallimage_url = $smallimage_url; $this->item[$i]->smallimage_height = $smallimage_height; $this->item[$i]->smallimage_width = $smallimage_width; $this->item[$i]->mediumimage_url = $mediumimage_url; $this->item[$i]->mediumimage_height = $mediumimage_height; $this->item[$i]->mediumimage_width = $mediumimage_width; $this->item[$i]->largeimage_url = $largeimage_url; $this->item[$i]->largeimage_height = $largeimage_height; $this->item[$i]->largeimage_width = $largeimage_width; $this->item[$i]->artist = $artist; $this->item[$i]->label = $label; $this->item[$i]->author = $author; $this->item[$i]->publisher = $publisher; $this->item[$i]->title = $title; $this->item[$i]->amount = $amount/100; $this->item[$i]->currencycode = $currencycode; $this->item[$i]->url = "http://www.amazon.fr/exec/obidos/ASIN/".$this->item[$i]->amazon_asin."/".$this->affID; $this->recordcount++; } } return $this->recordcount; } function extractall ($xml, $tag1, $tag2) { $ok = true; $i = 0; while ($ok==true) { $i1 = strpos($xml, $tag1); $i2 = strpos($xml, $tag2); if ( ($i1!==false) && ($i2!==false) ) { //echo $i1 . " / " . $i2 . " - "; $i1 = $i1 + strlen ($tag1); $len = $i2 - $i1; $data[$i] = substr ($xml, $i1, $len); $xml = substr ($xml, $i2+strlen($tag2), strlen($xml)); //echo "
" . htmlentities($data[$i]) . "
\n\n\n"; $i++; } else $ok = false; } return $data; } function extractstring ($xml, $tag1, $tag2) { $i1 = strpos($xml, $tag1); $i2 = strpos($xml, $tag2); if ( ($i1!==false) && ($i2!==false) ) { $i1 = $i1 + strlen ($tag1); $len = $i2 - $i1; $data = substr ($xml, $i1, $len); } else $data = ""; return $data; } function search_by_asin ($asin) { $wsurl = "http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService&SubscriptionId=" . $this->subscriptionID . "&Operation=ItemLookup&IdType=ASIN&ItemId=" . $asin . "&ResponseGroup=Medium,OfferFull"; $this->load ($wsurl); if (strpos($this->xml, "parse(); $ok = true; } else { $this->error = "Error"; $ok = false; } return $ok; } function search_by_keyword ($keyword) { $wsurl = "http://webservices.amazon.fr/onca/xml?Service=AWSECommerceService&SubscriptionId=" . $this->subscriptionID . "&Operation=ItemSearch&SearchIndex=" . $this->itemtype . "&Keywords=" . $keyword . "&ResponseGroup=Medium,OfferFull"; $this->load ($wsurl); if (strpos($this->xml, "parse(); $ok = true; } else { $this->error = "Error"; $ok = false; } return $ok; } }; // ================================================== // SYSTEME DE GESTION DE CACHE DES IMAGES // ================================================== function copyimage ($filefrom, $fileto) { $handle = fopen ($filefrom, "rb"); while (!feof($handle)) $contents .= fread($handle, 8192); fclose ($handle); $handle = fopen ($fileto, "wb"); fwrite ($handle, $contents); } function get_aws_image_size ($aws, $pos, $size, $picsrc, $picsrc_height, $picsrc_width) { switch ($size) { case 1: $picsrc = $aws->item[$pos]->smallimage_url; $picsrc_height = $aws->item[$pos]->smallimage_height; $picsrc_width = $aws->item[$pos]->smallimage_width; break; case 2: $picsrc = $aws->item[$pos]->mediumimage_url; $picsrc_height = $aws->item[$pos]->mediumimage_height; $picsrc_width = $aws->item[$pos]->mediumimage_width; break; case 3: $picsrc = $aws->item[$pos]->largeimage_url; $picsrc_height = $aws->item[$pos]->largeimage_height; $picsrc_width = $aws->item[$pos]->largeimage_width; break; }; } function get_image ($dir, $pos, $size, $bcache, $aws, $picsrc, $picsrc_height, $picsrc_width) { if ($bcache==false) { get_aws_image_size ($aws, $pos, $size, &$picsrc, &$picsrc_height, &$picsrc_width); } else { switch ($size) { case 1: $sizestring = "-small"; break; case 2: $sizestring = "-medium"; break; case 3: $sizestring = "-large"; break; }; $filenamelocal = $dir . $aws->item[$pos]->amazon_asin . $sizestring . ".jpg"; if (file_exists ($filenamelocal) ) { // dimensions et url (local) $picsrc = $filenamelocal; $size = getimagesize($picsrc); $picsrc_height = $size[1]; $picsrc_width = $size[0]; } else { // dimensions et url (aws) get_aws_image_size ($aws, $pos, $size, &$picsrc, &$picsrc_height, &$picsrc_width); // mise en cache if ($picsrc) copyimage ($picsrc, $filenamelocal); } } } // ================================================== // ================================================== // FONCTIONS D'AFFICHAGE // ================================================== function show ($aws, $i) { $itemtype = $aws->itemtype; $by = $from = ""; if ($itemtype=="Books") { $by = $aws->item[$i]->author; $from = $aws->item[$i]->publisher; } if ($itemtype=="Music") { $by = $aws->item[$i]->artist; $from = $aws->item[$i]->label; } $temp .= "\n\t\n\t\n\t\n
\n"; if (!$aws->cache_dir) $aws->cache_active = false; get_image ($aws->cache_dir, $i, 1, $aws->cache_active, $aws, &$picsrc, &$picsrc_height, &$picsrc_width); //if ($aws->item[$i]->smallimage_url) // $temp .= "

\n"; if ($picsrc) $temp .= "\n"; $temp .= "\t
    \n"; $temp .= "\t\t

" . $aws->item[$i]->title . "
par " . $by; if ($from) $temp .= " chez " . $from; $temp .= "

Prix : "; if ($aws->item[$i]->amount) $temp .= $aws->item[$i]->amount . " " . $aws->item[$i]->currencycode; else $temp .= "inconnu"; $temp .= "  (item[$i]->amazon_asin."/$affid\" target=\"blank\">commander)

\n"; $temp .= "\t
\n"; return $temp; } function show_by_asin ($aws, $affid, $asin, $itemtype) { $aws->itemtype = $itemtype; if ($aws->search_by_asin ($asin)==true) { $temp = show ($aws, 0); } else $temp = $aws->error; return $temp; } function show_book_asin ($aws, $affid, $asin) { return show_by_asin ($aws, $affid, $asin, "Books"); } function show_music_asin ($aws, $affid, $asin) { return show_by_asin ($aws, $affid, $asin, "Music"); } function show_by_keyword ($aws, $affid, $keyword, $itemtype) { $aws->itemtype = $itemtype; if ($aws->search_by_keyword ($keyword)==true) { $temp = ""; $n = min ($aws->recordcount, $aws->maxcount); for ($i=0; $i<$n; $i++) { $temp .= show ($aws, $i); $temp .= "
\n"; } } else $temp = $aws->error; return $temp; } function show_book_keyword ($aws, $affid, $keyword) { return show_by_keyword ($aws, $affid, $keyword, "Books"); } function show_music_keyword ($aws, $affid, $keyword) { return show_by_keyword ($aws, $affid, $keyword, "Music"); } // ========================= ?>