Skip to content

Instantly share code, notes, and snippets.

@shofetim
Created June 22, 2017 23:59
Show Gist options
  • Save shofetim/d6d90196a19acc15588af4e5545aab6f to your computer and use it in GitHub Desktop.
Save shofetim/d6d90196a19acc15588af4e5545aab6f to your computer and use it in GitHub Desktop.
<?php
#exit;
#$ua=$_SERVER['HTTP_USER_AGENT'];
#if ($ua == 'GuzzleHttp/6.2.0 curl/7.35.0 PHP/5.5.9-1ubuntu4.17') {
#header($_SERVER["SERVER_PROTOCOL"]." 404 Not Found");
#header("Status: 404 Not Found");
#$_SERVER['REDIRECT_STATUS'] = 404;
#exit;
#}
#$randResult=rand(1,10);
#if ($randResult>8) {
#exit;
#}
#exit;
# Rate limiting
$server = getenv('DATABASE_HOST');
$user = getenv('DATABASE_USERNAME');
$pass = getenv('DATABASE_PASSWORD');
$database = getenv('DATABASE_NAME');
//die(var_dump([$server, $user, $pass]));
mysql_connect($server, $user, $pass) or die(mysql_error());
mysql_select_db($database) or die(mysql_error());
$row = mysql_fetch_row(mysql_query('select count(id) from api_slots'));
if ($row[0] > 23) { # set the max request rate here
exit;
} else {
mysql_query('insert into api_slots');
$lockId = mysql_insert_id();
}
ob_start();
header('Content-type: text/xml');
echo '<?xml version="1.0" ?><pricing>' ;
$documentPath=__DIR__.'/../';
require_once($documentPath.'/wp-load.php');
#error_reporting(E_ALL | E_WARNING | E_NOTICE);
error_reporting(0);
ini_set('display_errors', false);
$product=isset($_GET['isbn']) ? $_GET['isbn']:'9781119056393';
if(!isset($_SESSION['api_log'])){
$_SESSION['api_log']=session_id();
}else{
$_SESSION['api_log']='';
}
$hash= isset($_GET['hash']) ? $_GET['hash'] :'null';
if(empty($_SESSION['api_logs']) || session_id() != $_SESSION['api_logs']['session_id']) {
setApiUser($hash);
}
$product=isset($_GET['isbn']) ? $_GET['isbn']:'9781119056393';
if (isset($_SESSION['api_logs']['api_user_id']) && $_SESSION['api_logs']['api_user_id'] != '') {
global $wpdb;
$api_user_id = $_SESSION['api_logs']['api_user_id'];
$ip = userIpAddress();
$request_date = $date_created = date('Y-m-d h:i:s',time());
$wpdb->insert("api_log", array(
'api_user_id' => $api_user_id,
'ip' => $ip,
'query' => $product,
'request_date' => $request_date,
),array('%s','%s','%s','%s'));
$wpdb->update("api_user", array(
'date_updated' => $request_date,
),
array('id' => $api_user_id),
array('%s'),
array('%s')
);
}
$isbn10='';
$isbn12='';
$isbn13='';
$product = checkisbnformate($product);
if(isset($product['isbn13']) && !empty($product['isbn13']))
{
$isbn10=$product['isbn10'];
$isbn13=$product['isbn13'];
}else{
$isbn12=$product['isbn12'];
}
if ($product['invalid_product'] != 1) {
if ($product['isbn12']) {
$productAsin = mwsLookup('upc', $product['isbn12']);
}
if ($product['isbn10']) {
$productAsin = mwsLookup('asin', $product['isbn10']);
}
if(!isset($productAsin['GetMatchingProductForIdResponse'])){
#sleep(1);
if($product['isbn12']) {
$productAsin = mwsLookup('upc', $product['isbn12']);
}
if ($product['isbn10']) {
$productAsin = mwsLookup('asin', $product['isbn10']);
}
}
$productBinding = isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Binding']) ? ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Binding']) : ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:Binding']);;
$productGroup = isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:ProductGroup']) ? ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:ProductGroup']) : ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:ProductGroup']);
if($productBinding=='Audio CD' || $productGroup=='Audio CD'){
$author=isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Artist']) ? implode(',',$productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Artist']) : '';
}else{
if (isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Author'])) {
$author = $productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Author'];
} elseif (isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Publisher'])) {
$author = $productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Publisher'];
} elseif (isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:Author'])) {
$author = $productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:Author'];
} else if (isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:Publisher'])) {
$author = $productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:Publisher'];
} else {
$author = '';
}
}
if (is_array($author)) {
$author = implode(' & ', $author);
}
$title = isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Title']) ? ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['AttributeSets']['ns2:ItemAttributes']['ns2:Title'] ) : ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['AttributeSets']['ns2:ItemAttributes']['ns2:Title']);
$asin_no = isset($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['Identifiers']['MarketplaceASIN']['ASIN']) ? ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product']['Identifiers']['MarketplaceASIN']['ASIN'] ) : ($productAsin['GetMatchingProductForIdResponse']['GetMatchingProductForIdResult']['Products']['Product'][0]['Identifiers']['MarketplaceASIN']['ASIN']);
$isbn = isset($isbn12) ? $asin_no : $isbn10;
$productData = mwsLookupWithOfferPrice($asin_no);
$xml = inventory_evaluator_mws($productData, $productGroup);
$details = pricingDetailsMWS($isbn, $productData, $xml, $productGroup);
$_qty = $details['qty'];
$_price = $details['price'];
$_rank = $details['rank'];
$_type = $details['type'];
$abs_min = $details['abs_min'];
$max_rank = $details['pricing']['max_rank'];
$on_the_way_multi = $details['pricing']['on_the_way_multi'];
$data_qty = checkout_quote(0,$isbn10,$isbn13,$asin_no);
#$onTheWay = checkout_onTheWay($max_rank,$on_the_way_multi,$isbn10,$isbn13,$asin_no);
$onTheWay=0;
$onTheWay += $data_qty;
$total_stock = $onTheWay;
$azPriceByQty = azPriceByQty($xml, $_qty, $_price, $_rank, $_type, $abs_min, $total_stock);
$price=0.00;
if($azPriceByQty[0]) {
$price = $azPriceByQty[0];
}
if($isbn12 && empty($isbn10) && empty($isbn13)){
$isbn= $isbn12;
}else if(empty($isbn12) && empty($isbn10) && empty($isbn13)){
$isbn= $asin_no;
}else if(empty($isbn12) && (!empty($isbn10) || !empty($isbn13))) {
$isbn= $isbn10;
}else{
$isbn= $isbn12;
}
echo " <price isbn='".$isbn."'>".number_format((float)$price, 2, '.', '')."</price>";
}else{
echo "A valid ISBN parameter is required or invalid ISBN / UPC code";
}
echo "</pricing>\n";
#mysql_close();
mysql_query('delete from from api_slots where id = ' . $lockId); # cleanup of rate limiting
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment