|
|
|
มีท่านใดเคยเขียน API ดึงสินค้าจาก Amazon บ้างครับ ขอแนวหน่อย |
|
|
|
|
|
|
|
ไปเจอมาลองดูตัวนี้น่ะครับ
Code (PHP)
<?php
error_reporting(E_ALL);
//----Requester----------------------------------------------------------------------------------
define("ACCESS_KEY_ID","AKIAIYQTZRGPKBBTNG4Q");
define("SECRET_ACCESS_KEY","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");
define("ECS_VERSION","2009-10-01");
define("MAX_AMAZON_PAGE", 400);
function ItemSearch($browseNode='', $catetoryName='', $pageNumber='1', $keyword){
if($browseNode == '')
return false;
$param = array(
"Operation" => "ItemSearch",
"Condition" => "All", // or "New" or "Used" or "Collectible" or "Refurbished"
"Keywords" => "$keyword",
"SearchIndex" => "$catetoryName", //"Blended",Jewelry
"Count" => "24",
"BrowseNode" => "$browseNode",
"ItemPage" => "$pageNumber",
"Title" => "",
"Sort" => "",
"ResponseGroup" => "Small");
$xml = get_xml_obj($param);
return $xml;
}
function get_xml_obj($param, $cacheperiod=900, $keyprefix='') {
ksort($param);
if(! ($str=lookupAWS($param)) ) {
echo 'lookupAWS returned false';
return false;
}
$obj = simplexml_load_string($str);
if(! $obj) {
echo 'XML parse error';
return false;
}
return $obj;
}
function lookupAWS($param) {
$url = signedURL($param);
if(!$url) return false;
for($x=1; $x<3; $x++) {
$contents = @file_get_contents($url);
if ($contents !== False)
break;
sleep(1);
}
if($contents === False)
return false;
return $contents;
}
function signedURL($param) {
if(!$param)
return '';
$server = 'ecs.amazonaws.com';
$uri = '/onca/xml';
$method = 'GET';
// additional parameters
$param["Service"] = "AWSECommerceService";
$param["AssociateTag"] = "itconner-20";
$param["AWSAccessKeyId"] = rawurlencode(ACCESS_KEY_ID);
$param["Timestamp"] = gmdate('Y-m-d\TH:i:s\Z');
$param["Version"] = ECS_VERSION;
ksort($param);
$qa = array();
foreach($param as $key=>$val) {
$key = str_replace("%7E", "~", rawurlencode($key));
$val = str_replace("%7E", "~", rawurlencode($val));
$qa[$key] = rawurlencode($key) . '=' . $val;
}
$qs = implode('&',$qa);
$data = $method."\n".$server."\n".$uri."\n".$qs;
$sig = base64_encode(hash_hmac('sha256', $data, SECRET_ACCESS_KEY, true));
$sig = str_replace("%7E", "~", rawurlencode($sig));
$url = "http://".$server.$uri."?".$qs."&Signature=".$sig;
//echo $url;
return $url;
}
//-------------------------------------------------------------------------------
function startJob(){
$node = '672123011';
$catetoryName = 'Shoes'; //example- Apparel, Beauty, Blended, Books
$keyword = "";
$page = 1;
$xml = ItemSearch($node, $catetoryName, 1,$keyword);
$totalpage = $xml->Items->TotalPages;
$browsenode = $xml->Items->Request->ItemSearchRequest->BrowseNode;
if($totalpage > MAX_AMAZON_PAGE)
$totalpage = MAX_AMAZON_PAGE;
for($page=1;$page<=$totalpage;$page++){
echo "<page: ".$page.">";
foreach ($xml->Items->Item as $item) {
echo $item->ASIN."<br>";
}
sleep(1);
$xml = ItemSearch($node, $catetoryName, $page,$keyword);
}
}
startJob();
?>
|
|
|
|
|
Date :
2011-04-07 21:09:48 |
By :
webmaster |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
Load balance : Server 02
|