Skip to content

Instantly share code, notes, and snippets.

@theskillwithin
Created April 17, 2016 00:08
Show Gist options
  • Save theskillwithin/7214bafa813772dd2ed7cf35af13096b to your computer and use it in GitHub Desktop.
Save theskillwithin/7214bafa813772dd2ed7cf35af13096b to your computer and use it in GitHub Desktop.
<?php
include('aws_signed_request.php');
function init() { //get the amazon process output
$row = 1;
if (($handle = fopen("proccess.csv", "r")) !== FALSE) {
while (($data = fgetcsv($handle, 1000, "\t")) !== FALSE) {
$num = count($data);
//echo "<p> $num fields in line $row: <br /></p>\n";
$row++;
$dataout = [];
if ( isset($data[4]) ) {
preg_match_all("@\((.+?)\)@", $data[4], $dataout);
unset($dataout[1][1]);
unset($dataout[0]);
//$dataout[0] = array_values($dataout[0]);
$dataout[1] = array_values($dataout[1]);
$dataout = $dataout[1];
$upcs[] = $dataout;
//var_dump($dataout);
//echo $data[4] . "\n";
}
}
fclose($handle);
}
return($upcs);
}
function domdom($xml) {
$salesRanks = [];
$doc = new DomDocument("1.0", "ISO-8859-15" );
$doc->preserveWhiteSpace = false;
$doc->loadXML($xml);
$xpath = new DOMXpath($doc);
$xpath->registerNamespace('aw', "http://webservices.amazon.com/AWSECommerceService/2011-08-01");
//$salesRankId = $xpath->query("//Items/Item/SalesRank");
foreach ($xpath->query('//aw:ItemLookupResponse/aw:Items/aw:Item/aw:SalesRank') as $x) {
$salesRanks[] = $x->nodeValue;
}
//var_dump($salesRanks);
//return $salesRanks[0];
return isset( $salesRanks[0] )?
$salesRanks[0]:
null;
}
function getAmazonSalesRank($ASINN) {
$public_key = 'SECRET';
$private_key = 'SECRET';
$associate_tag = 'SECRET';
usleep(300000);
// generate signed URL
$request = aws_signed_request('com', array(
'Operation' => 'ItemLookup',
'ItemId' => $ASINN,
'ResponseGroup' => 'SalesRank'), $public_key, $private_key, $associate_tag);
// do request (you could also use curl etc.)
$url = $request;
$ch = curl_init();
// set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
// grab URL and pass it to the browser
$request = curl_exec ($ch);
curl_close($ch);
//echo $request;
$response = $request;
$sales_rank = domdom($response);
// parse XML
/*$pxml = simplexml_load_string($response);
if ( !isset( $pxml->Items->Item->SalesRank ) ) {
$sales_rank = "notset";
var_dump($pxml->Items);
echo "ERROR: $ASINN \n \n";
} else {
$sales_rank = (int) $pxml->Items->Item->SalesRank;
echo "SUCCESS $ASINN";
} */
return [$ASINN=>$sales_rank];
}
//$output = getAmazonSalesRank('B0001EL4QO'); var_dump($output);
$check = init();
echo "-----CHECK INIT----CHECK--INIT----- \n";
var_dump($check);
echo "-----END CHECK INIT---- END CHECK--INIT----- \n";
$resultArray = [];
foreach($check as $key2 => $val2)
{
if ( isset($val2[1]) ) {
$ex2 = explode(',',$val2[1]);
foreach( $ex2 as $asinnn ){ $resultArray = $resultArray + getAmazonSalesRank( $asinnn ); }
}
}
echo "----------result array------------------- \n";
var_dump($resultArray);
echo "----------end result array------------------- \n";
function gotAmazonSalesRank($ASIN, array $resultArray) {
return isset( $resultArray[$ASIN] )?
$resultArray[$ASIN]:
null;
}
$sort_by_rank = function($a, $b) use ($resultArray)
{
$a = gotAmazonSalesRank($a, $resultArray);
$b = gotAmazonSalesRank($b, $resultArray);
if ($a === 0 or $a === NULL) {
return -1;
}
if ($b === 0 or $b === NULL) {
return -1;
}
if ($a == $b) {
return 0;
} else {
return ($a < $b) ? -1 : 1;
}
};
echo "----======== UPC: Best ASIN Match ========---- \n";
foreach($check as $key => $val)
{
if ( isset($val[0]) ) {
if (isset($val[1])) {
$ex = explode(',',$val[1]);
usort($ex,$sort_by_rank);
echo "$val[0]: $ex[0] \n";
} else {
echo "$val[0]: not set \n";
}
}
}
?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment