public function testapiTest() { $obj = new AmazonProductAPI(); try { $result = $obj->searchProducts("spice girls", AmazonProductAPI::DVD, "TITLE"); $data = $result->Items->Item->ItemAttributes->Title; echo $data . "\n"; $this->assertNotNull($data); } catch (Exception $e) { echo $e->getMessage(); } try { $result = $obj->searchProducts("024543602859", AmazonProductAPI::DVD, "UPC"); $data = $result->Items->Item->ItemAttributes->Title; echo $data . "\n"; $this->assertNotNull($data); } catch (Exception $e) { echo $e->getMessage(); } try { $result = $obj->getItemByUpc("014633190168", AmazonProductAPI::GAMES); $data = $result->Items->Item->ItemAttributes->Title; echo $data . "\n"; $this->assertNotNull($data); } catch (Exception $e) { echo $e->getMessage(); } try { $result = $obj->getItemByAsin("B001AVCFK6", AmazonProductAPI::DVD); $data = $result->Items->Item->ItemAttributes->Title; echo $data . "\n"; $this->assertNotNull($data); } catch (Exception $e) { echo $e->getMessage(); } try { $result = $obj->getItemByKeyword("tom petty", AmazonProductAPI::MUSIC); $data = $result->Items->Item->ItemAttributes->Title; echo $data . "\n"; $this->assertNotNull($data); } catch (Exception $e) { echo $e->getMessage(); } }
<?php define('FS_ROOT', realpath(dirname(__FILE__))); require_once FS_ROOT . "/../../www/config.php"; require_once FS_ROOT . "/../../www/lib/amazon.php"; require_once FS_ROOT . "/../../www/lib/site.php"; require_once FS_ROOT . "/../../www/lib/book.php"; $b = new Book(); $s = new Sites(); $site = $s->get(); $title = "Long Arm, The - Franz Habl "; $obj = new AmazonProductAPI($site->amazonpubkey, $site->amazonprivkey, $site->amazonassociatetag); try { $amaz = $obj->searchProducts($title, AmazonProductAPI::BOOKS, "TITLE"); $item = array(); $item["asin"] = (string) $amaz->Items->Item->ASIN; $item["url"] = (string) $amaz->Items->Item->DetailPageURL; $item["cover"] = (string) $amaz->Items->Item->LargeImage->URL; $item["author"] = (string) $amaz->Items->Item->ItemAttributes->Author; $item["dewey"] = (string) $amaz->Items->Item->ItemAttributes->DeweyDecimalNumber; $item["ean"] = (string) $amaz->Items->Item->ItemAttributes->EAN; $item["isbn"] = (string) $amaz->Items->Item->ItemAttributes->ISBN; $item["publisher"] = (string) $amaz->Items->Item->ItemAttributes->Publisher; $item["publishdate"] = (string) $amaz->Items->Item->ItemAttributes->PublicationDate; $item["pages"] = (string) $amaz->Items->Item->ItemAttributes->NumberOfPages; $item["title"] = (string) $amaz->Items->Item->ItemAttributes->Title; $item["review"] = ""; if (isset($amaz->Items->Item->EditorialReviews)) { $item["review"] = trim(strip_tags((string) $amaz->Items->Item->EditorialReviews->EditorialReview->Content)); } print_r($item);
/** * retrieve an image from amazon for a product * * @return array array of products */ function Products_adminImportDataFromAmazon() { $pid = (int) $_REQUEST['id']; $ean = $_REQUEST['ean']; if (strlen($ean) == 12) { $ean = Products_adminImportDataFromAmazonGetEan13CheckNum($ean); } if (strlen($ean) != 13) { return array('message' => 'EAN too short'); } $access_key = $_REQUEST['access_key']; $private_key = $_REQUEST['secret_key']; $associate_tag = $_REQUEST['associate_key']; $pdata = Product::getInstance($pid); // { image if (!isset($pdata->images_directory) || !$pdata->images_directory || $pdata->images_directory == '/' || !is_dir(USERBASE . '/f/' . $pdata->images_directory)) { if (!is_dir(USERBASE . '/f/products/product-images')) { mkdir(USERBASE . '/f/products/product-images', 0777, true); } $pdata->images_directory = '/products/product-images/' . (int) ($pid / 1000) . '/' . $pid; mkdir(USERBASE . '/f' . $pdata->images_directory, 0755, true); Product::getInstance($pid)->set('images_directory', $pdata->images_directory); } $image_exists = 0; $dir = new DirectoryIterator(USERBASE . '/f' . $pdata->images_directory); foreach ($dir as $f) { if ($f->isDot()) { continue; } $image_exists++; } // } if ($image_exists) { return array('message' => 'already_exists'); } $obj = new AmazonProductAPI($access_key, $private_key, $associate_tag); try { $result = $obj->getItemByEan($ean, ''); if (!@$result->Items->Item) { return array('message' => 'not found'); } // { description $description = (array) $result->Items->Item->EditorialReviews->EditorialReview->Content; $description = $description[0]; $do_description = 1; if ($description) { $meta = json_decode(dbOne('select data_fields from products where id=' . $pid, 'data_fields'), true); foreach ($meta as $k => $v) { if (!isset($v['n'])) { unset($meta[$k]); continue; } if ($v['n'] == 'description') { if ($v['v']) { $do_description = 0; } else { unset($meta[$k]); } } } if ($do_description) { $meta[] = array('n' => 'description', 'v' => $description); } Product::getInstance($pid)->set('data_fields', json_encode($meta)); } // } // { image $img = (array) $result->Items->Item->LargeImage->URL; $img = $img[0]; if (!$image_exists) { copy($img, USERBASE . '/f/' . $pdata->images_directory . '/default.jpg'); } // } return array('message' => 'found and imported'); } catch (Exception $e) { return array('message' => 'error... ' . $e->getMessage()); } }
/** * Retrieve info from Amazon for a title. */ public function fetchAmazonProperties($title) { $obj = new AmazonProductAPI($this->pubkey, $this->privkey, $this->asstag); try { $result = $obj->searchProducts($title, AmazonProductAPI::MP3, "TITLE"); } catch (Exception $e) { //if first search failed try the mp3downloads section try { // sleep for 1 second sleep(1); $result = $obj->searchProducts($title, AmazonProductAPI::MUSIC, "TITLE"); } catch (Exception $e2) { $result = false; } } return $result; }
<?php require_once dirname(__FILE__) . '/../../../www/config.php'; require_once nZEDb_LIBS . 'AmazonProductAPI.php'; use nzedb\db\Settings; // Test if your amazon keys are working. $pdo = new Settings(); $pubkey = $pdo->getSetting('amazonpubkey'); $privkey = $pdo->getSetting('amazonprivkey'); $asstag = $pdo->getSetting('amazonassociatetag'); $obj = new AmazonProductAPI($pubkey, $privkey, $asstag); $e = null; try { $result = $obj->searchProducts("Adriana Koulias The Seal", AmazonProductAPI::BOOKS, "TITLE"); } catch (Exception $e) { $result = false; } if ($result !== false) { print_r($result); exit($pdo->log->header("\nLooks like it is working alright.")); } else { print_r($e); exit($pdo->log->error("\nThere was a problem attemtping to query amazon. Maybe your keys or wrong, or you are being throttled.\n")); }
<?php /* Example usage of the Amazon Product Advertising API */ include "amazon_api_class.php"; $obj = new AmazonProductAPI(); try { $result = $obj->searchProducts("Harry Potter", AmazonProductAPI::DVD, "TITLE"); } catch (Exception $e) { echo $e->getMessage(); } print_r($result); echo "Sales Rank : {$result->Items->Item->SalesRank}<br>"; echo "ASIN : {$result->Items->Item->ASIN}<br>"; echo "<br><img src=\"" . $result->Items->Item->MediumImage->URL . "\" /><br>";
public function fetchAmazonProperties($title) { $obj = new \AmazonProductAPI($this->pubkey, $this->privkey, $this->asstag); try { $result = $obj->searchProducts($title, \AmazonProductAPI::BOOKS, 'TITLE'); } catch (\Exception $e) { $result = false; } return $result; }
/** * Retrieve properties for an item from Amazon. */ public function fetchAmazonProperties($title, $node) { $obj = new AmazonProductAPI($this->pubkey, $this->privkey, $this->asstag); try { $result = $obj->searchProducts($title, AmazonProductAPI::GAMES, "NODE", $node); } catch (Exception $e) { $result = false; } return $result; }
/** * @param $title * * @return bool|mixed */ public function fetchAmazonProperties($title) { $result = false; $obj = new \AmazonProductAPI($this->pubkey, $this->privkey, $this->asstag); // Try Music category. try { $result = $obj->searchProducts($title, \AmazonProductAPI::MUSIC, "TITLE"); } catch (\Exception $e) { // Empty because we try another method. } // Try MP3 category. if ($result === false) { usleep(700000); try { $result = $obj->searchProducts($title, \AmazonProductAPI::MP3, "TITLE"); } catch (\Exception $e) { // Empty because we try another method. } } // Try Digital Music category. if ($result === false) { usleep(700000); try { $result = $obj->searchProducts($title, \AmazonProductAPI::DIGITALMUS, "TITLE"); } catch (\Exception $e) { // Empty because we try another method. } } // Try Music Tracks category. if ($result === false) { usleep(700000); try { $result = $obj->searchProducts($title, \AmazonProductAPI::MUSICTRACKS, "TITLE"); } catch (\Exception $e) { // Empty because we exhausted all possibilities. } } return $result; }
<?php /* Example usage of the Amazon Product Advertising API */ include "amazon_api_class.php"; $obj = new AmazonProductAPI(); $result = ''; try { $result = $obj->searchProducts("X-Men Origins", AmazonProductAPI::DVD, "TITLE"); } catch (Exception $e) { echo $e->getMessage(); } print_r($result); exit; echo "Sales Rank : {$result->Items->Item->SalesRank}<br>"; echo "ASIN : {$result->Items->Item->ASIN}<br>"; echo "<br><img src=\"" . $result->Items->Item->MediumImage->URL . "\" /><br>";
<?php require_once 'amazon_product_api_class.php'; $public = ''; //amazon public key here $private = ''; //amazon private/secret key here $site = 'com'; //amazon region $affiliate_id = ''; //amazon affiliate id $amazon = $amazon = new AmazonProductAPI($public, $private, $site, $affiliate_id); $similar = array('Operation' => 'SimilarityLookup', 'ItemId' => 'B0006N149M', 'Condition' => 'All', 'ResponseGroup' => 'Medium'); $result = $amazon->queryAmazon($similar); $similar_products = $result->Items->Item; foreach ($similar_products as $si) { $item_url = $si->DetailPageURL; //get its amazon url $img = $si->MediumImage->URL; //get the image url echo "<li>"; echo "<img src='{$img}'/>"; echo "<a href='{$item_url}'>" . $si->ASIN . "</a>"; echo $si->ItemAttributes->ListPrice->FormattedPrice; //item price echo "</li>"; }
<?php /* Example usage of the Amazon Product Advertising API */ include "AmazonProductAPI.class.php"; $amazon = new AmazonProductAPI(); error_reporting(0); $DEBUG = true; $query = !empty($_REQUEST['q']) ? $_REQUEST['q'] : 'X-Men Origins'; $category = !empty($_REQUEST['category']) ? $_REQUEST['category'] : AmazonProductAPI::DVD; try { $result = $amazon->searchProducts($query, $category, "TITLE"); } catch (Exception $e) { echo $e->getMessage(); } $resultList = '<ul>'; foreach ($result->Items->Item as $item) { $resultList .= '<li><a href="http://www.amazon.com/gp/product/' . $item->ASIN . '/?ref=' . $amazon->getAssociateTag() . '&tag=' . $amazon->getAssociateTag() . '" title="' . $item->ItemAttributes->Title . '" target="_blank">'; if ($item->MediumImage->URL) { $resultList .= '<img src="' . $item->MediumImage->URL . '" /></a>'; } else { $resultList .= '<img src="http://farm2.static.flickr.com/1192/534117370_7eec8198e8.jpg" alt="Image Coming Soon..." width="120" /></a>'; } if ($item->SalesRank) { $resultList .= '<br/>Rank: ' . $item->SalesRank; } $resultList .= '</li>'; } $resultList .= '</ul>'; ?> <html> <head>
private function doAmazon($q, $name = '', $id = 0, $nfo = '', $region = 'com', $case = false, $row = '') { $amazon = new \AmazonProductAPI($this->pdo->getSetting('amazonpubkey'), $this->pdo->getSetting('amazonprivkey'), $this->pdo->getSetting('amazonassociatetag')); $ok = false; try { switch ($case) { case 'upc': $amaz = $amazon->getItemByUpc(trim($q), $region); break; case 'asin': $amaz = $amazon->getItemByAsin(trim($q), $region); break; case 'isbn': $amaz = $amazon->searchProducts(trim($q), '', "ISBN"); break; default: $amaz = false; } } catch (\Exception $e) { echo 'Caught exception: ', $e->getMessage() . PHP_EOL; unset($s, $amaz, $amazon); } if (isset($amaz) && isset($amaz->Items->Item)) { sleep(2); $type = $amaz->Items->Item->ItemAttributes->ProductGroup; switch ($type) { case 'Audible': case 'Book': case 'eBooks': $ok = $this->_doAmazonBooks($amaz, $id); break; case 'Digital Music Track': case 'Digital Music Album': case 'Music': $ok = $this->_doAmazonMusic($amaz, $id); break; case 'Bluray': case 'Movies': case 'DVD': case 'DVD & Bluray': $ok = $this->_doAmazonMovies($nfo, $amaz, $id); break; case 'Video Games': $ok = $this->_doAmazonVG($amaz, $id); break; default: echo PHP_EOL . $this->pdo->log->error("Amazon category {$type} could not be parsed for " . $name) . PHP_EOL; } } return $ok; }
function get_amazon_link($title, $runtime, $cast) { require "amazon/amazon_api_class.php"; $title = preg_replace('/[^a-z0-9]+/i', ' ', $title); $obj = new AmazonProductAPI(); try { $result = $obj->searchProducts($title, AmazonProductAPI::VIDEO, "TITLE"); } catch (Exception $e) { echo $e->getMessage(); } $link = ["link" => "", "rent" => "", "buy" => "", "asin" => ""]; $test = true; foreach ($result->Items->Item as $k) { if ($test) { echo $k; $test = false; } if ($k->ItemAttributes->Binding == 'Amazon Instant Video') { if (abs($k->ItemAttributes->RunningTime - $runtime) < 5 && strpos($cast, (string) $k->ItemAttributes->Actor) !== false || abs($k->ItemAttributes->RunningTime - $runtime) < 5 && levenshtein($title, (string) $k->ItemAttributes->Title) <= 3) { $link['link'] = $k->DetailPageURL; $link['asin'] = $k->ASIN; break; } } } if (strlen($link['link']) > 0) { $html = file_get_contents($link['link']); if (!strpos($html, 'movie is currently unavailable') && strlen($html) > 20) { //$pos = strpos($html, '<strong>Release year:</strong> '); //$release = substr($html, $pos+31, 4); //echo $release; $html = substr($html, strpos($html, '<ul class="button-list">'), 2000); $matches = array(); $rent = ""; $buy = ""; preg_match_all("/Rent SD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $rent .= substr($matches[0][0], 8); } preg_match_all("/Rent HD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $rent .= "|" . substr($matches[0][0], 8); } preg_match_all("/Buy SD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $buy .= substr($matches[0][0], 7); } preg_match_all("/Buy HD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $buy .= "|" . substr($matches[0][0], 7); } $link['rent'] = $rent; $link['buy'] = $buy; } else { $link['link'] = ''; } } return $link; }
public static function get_link($movieId) { $link = self::find_by_id($movieId); $exists = $link ? true : false; if ($link && days_from_now($link->timestamp) < LINK_UPDATE_DAYS) { // good } else { $exists = $link ? true : false; $movie = Movie::find_by_id($movieId); $link = new Amazon(); $link->id = $movieId; $link->timestamp = gen_timestamp(); require_once LIB_PATH . DS . "amazon_api_class.php"; $runtime = $movie->runtime; $title = preg_replace('/[^a-z0-9]+/i', ' ', $movie->title); $obj = new AmazonProductAPI(); try { $result = $obj->searchProducts($title, AmazonProductAPI::VIDEO, "TITLE"); } catch (Exception $e) { echo $e->getMessage(); } $cast = Actor::list_of_names($movie->id); foreach ($result->Items->Item as $k) { if ($k->ItemAttributes->Binding == 'Amazon Instant Video') { if (abs($k->ItemAttributes->RunningTime - $runtime) < 5 && strpos($cast, (string) $k->ItemAttributes->Actor) !== false || abs($k->ItemAttributes->RunningTime - $runtime) < 5 && levenshtein($title, (string) $k->ItemAttributes->Title) <= 3) { $link->link = $k->DetailPageURL; $link->asin = $k->ASIN; break; } } } if (strlen($link->link) > 0) { $html = file_get_contents($link->link); if (!strpos($html, 'movie is currently unavailable') && strlen($html) > 20) { $html = substr($html, strpos($html, '<ul class="button-list">'), 2000); $matches = array(); $rent = ""; $buy = ""; preg_match_all("/Rent SD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $rent .= substr($matches[0][0], 8); } preg_match_all("/Rent HD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $rent .= "|" . substr($matches[0][0], 8); } preg_match_all("/Buy SD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $buy .= substr($matches[0][0], 7); } preg_match_all("/Buy HD [\$][1-9]+[.][1-9][1-9]/", $html, $matches); if (sizeof($matches[0]) > 0) { $buy .= "|" . substr($matches[0][0], 7); } $link->rent = $rent; $link->buy = $buy; } } if ($exists) { $link->update(); } else { $link->create(); } } return $link; }
function search_amz_products() { $search = $this->input->post('search'); $category = $this->input->post('category'); $page = (int) $this->input->post('page'); $page = $page == 0 ? 1 : $page; if ($search != '' && $category != '') { require_once 'application/libraries/amazon_product_api_class.php'; $public = 'AKIAIAY2PFVI2RPY2M2A'; //amazon public key here $private = 'kE6lCqkxagPlAmCMfC411HPq2nw6nGlvkGZXb8ad'; //amazon private/secret key here $site = 'com'; //amazon region $affiliate_id = 'azto-20'; //amazon affiliate id $amazon = $amazon = new AmazonProductAPI($public, $private, $site, $affiliate_id); $params = array("ItemPage" => $page, "Operation" => "ItemSearch", "SearchIndex" => $category, "Keywords" => $search, "ResponseGroup" => "Medium,Reviews"); $result = $amazon->queryAmazon($params); //echo '<pre>'; print_r($result); echo '</pre>'; die('stop'); $total_products = (int) $result->Items->TotalResults[0]; $total_pages = (int) $result->Items->TotalPages[0]; $similar_products = $result->Items->Item; $product_arr = array(); foreach ($similar_products as $si) { $product_asin = (string) $si->ASIN[0]; $product_name = (string) $si->ItemAttributes->Title[0]; $imagesets = $si->ImageSets->ImageSet; $product_image = array(); if (!empty($imagesets)) { foreach ($imagesets as $imageset) { $product_image[] = (string) $imageset->LargeImage->URL[0]; } } $product_price = (string) $si->ItemAttributes->ListPrice->FormattedPrice[0]; $product_price = (double) str_replace('$', '', $product_price); $product_manufacturer = (string) $si->ItemAttributes->Manufacturer[0]; $product_model = (string) $si->ItemAttributes->Model[0]; $product_link = (string) $si->DetailPageURL[0]; $product_features = array(); $features = $si->ItemAttributes->Feature; if (!empty($features)) { foreach ($features as $k => $v) { $product_features[] = (string) $v[0]; } } //print_r($product_features); die(''); $product_dimensions = $si->ItemAttributes->ItemDimensions; $product_arr[] = array('product_asin' => $product_asin, 'product_name' => $product_name, 'product_image' => $product_image, 'product_price' => $product_price, 'product_manufacturer' => $product_manufacturer, 'product_model' => $product_model, 'product_link' => $product_link, 'product_features' => $product_features, 'product_dimensions' => $product_dimensions); } if (!empty($product_arr)) { echo json_encode(array('status' => 1, 'message' => 'Get products successfully!', 'total_products' => $total_products, 'total_pages' => $total_pages, 'products' => $product_arr)); } else { echo json_encode(array('status' => 0, 'message' => 'Products not exist!')); } } else { echo json_encode(array('status' => 0, 'message' => 'Please enter keyword and category for searching products!')); } }