Rev 38 | Rev 66 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed
<?php
include_once('php/constants.php');
error_reporting(E_ALL);
// Get CJ Affiliate listings
function get_cjaffiliate($query, $searchType) {
$vendors = Vendors::getInstance();
$config = $vendors->getVendor(Vendors::CJAFFILIATE);
// API request variables
$query = '+' . str_replace(" ", " +", $query); // enforce 'and' logic
$safequery = urlencode($query);
$numResults = $config['numResults'];
$numResultsMid = $config['numResultsMid'];
$resultsMid = [];
// Construct the findItemsByKeywords HTTP GET call
$url = "https://product-search.api.cj.com/v2/product-search";
$url .= "?website-id=" . $config['websiteId'];
if (in_array($_SESSION["barcode"]["Type"], array("UPC","EAN","ISBN"))) {
$url .= "&upc=";
} else {
$url .= "&keywords=";
}
$url .= $safequery;
$url .= "&records-per-page=" . $numResults;
$url .= "&page-number=1";
$url .= "&sort-by=price";
$url .= "&sort-order=asc";
$url .= "&advertiser-ids=" . $config['advertiserIds'];
$header = array(
'Content-Type: application/x-www-form-urlencoded',
'Accept: application/xml',
'Accept-Language: en-US,en;q=0.5',
'Accept-Charset: UTF-8,*;q=0.5',
'Authorization: ' . $config['personalAccessToken']
);
$ch = curl_init();
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_AUTOREFERER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec ($ch);
if (empty($result)) {
return [];
}
$result = utf8_encode($result);
$result = simplexml_load_string($result);
curl_close($ch);
//echo "$url<pre>";print_r($result);echo "</pre>";
$arr = [];
// Check to see if the request found any results
if (isset($result->products['total-matched'])) {
// If the response was loaded, parse it and store in array
foreach ($result->{'products'}->{'product'} as $product) {
$barcode = (string)$product->{'upc'};
$barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
// approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
// pending: FYE(4911961)
// declined: Barnes & Noble, Booksamillion, OnBuy.com
$merchantId = strval($product->{'advertiser-id'});
$merchantName = (string)$product->{'advertiser-name'};
$title = (string)$product->{'name'};
$pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
if (empty($pic)) {
switch ($merchantId) {
case 1111879: // ArkivMusic
$pic = "images/no-image-available.jpg";
break;
default:
continue;
}
}
$url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
if (empty($url)) {
continue;
}
switch ($merchantId) {
case 4911961: // FYE.com
$category = "CD";
$freeShippingCap = 40.00;
$handlingTime = 1;
$shippingCost = 3.24;
$shippingCurrency = 'USD';
if (preg_match('/\d\d\d\d$/', $title)) {
$type = 'Used';
$condition = "Used";
$title = rtrim(substr($title, 0, -4));
} else {
$type = 'New';
$condition = "Brand New";
}
break;
case 4926671: // Secondspin.com
$freeShippingCap = 0.00;
$handlingTime = 1;
$shippingCost = 3.99;
$shippingCurrency = 'USD';
$type = 'Used';
$condition = "Used";
$category = "CD";
$pic = str_replace("fye.com", "secondspin.com", $pic);
if (endsWith($pic, "nopic-disc-300.png")) {
$pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
}
$url = str_replace("fye.com", "secondspin.com", $url);
break;
case 1595844: // Sheet Music Plus
$freeShippingCap = 35.00;
$handlingTime = 1;
$shippingCost = 3.99;
$shippingCurrency = 'USD';
$type = 'New';
$condition = "Brand New";
$category = "Book";
break;
case 1111879: // ArkivMusic
$freeShippingCap = 0.00;
$handlingTime = 1;
$shippingCost = 2.99;
$shippingCurrency = 'USD';
$type = 'New';
$condition = "Brand New";
$category = "CD";
break;
default:
$freeShippingCap = 0.00;
$handlingTime = -1;
$shippingCost = 0.00;
$shippingCurrency = 'USD';
$type = 'Used';
$condition = "Used";
$category = "CD";
break;
}
$country = 'US';
$bestOffer = false;
$price = 0;
if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
$price = minNotNull(array($product->{'price'}, $product->{'sale-price'}));
} else if (!empty($product->{'price'})) {
$price = $product->{'price'};
} else if (!empty($product->{'sale-price'})) {
$price = $product->{'sale-price'};
}
$price = number_format(floatval($price), 2, '.', '');
if ($price <= "0.00") {
continue;
}
$currency = 'USD';
$timeLeft = 0;
$listingType = 'Fixed';
$location = 'US';
$zip = '';
$feedbackScore = -1;
$feedbackPercent = -1;
$sellerName = '';
// bugbug
//ls_cj_csv(array((string)$product->{'advertiser-name'},(string)$product->{'advertiser-category'},(string)$product->{'name'},(string)$product->{'description'},"'".$barcode,$barcodeType,minNotNull(array($product->{'price'},(string)$product->{'sale-price'})),(string)$product->{'buy-url'}));
// this is last after all checks
$resultsMid[] = $merchantId;
if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
continue;
}
$arr[] = array(
"Merchant" => $merchantName,
"Type" => "$type",
"Title" => "$title",
"Barcode" => "$barcode",
"BarcodeType" => "$barcodeType",
"Image" => "$pic",
"URL" => "$url",
"Category" => "$category",
"Condition" => "$condition",
"Country" => "$country",
"BestOffer" => "$bestOffer",
"TimeLeft" => "$timeLeft",
"Price" => "$price",
"Currency" => "$currency",
"ListingType" => "$listingType",
"Location" => "$location",
"Zip" => "$zip",
"FeedbackScore" => "$feedbackScore",
"FeedbackPercent" => "$feedbackPercent",
"SellerName" => "$sellerName",
"HandlingTime" => "$handlingTime",
"ShippingCost" => "$shippingCost",
"ShippingCurrency" => "$shippingCurrency",
"FreeShippingCap" => "$freeShippingCap",
"Show" => true
);
}
}
// If the response does not indicate 'Success,' log an error
else {
error_log($url);
if (isset($result->{'error-message'})) {
error_log($result->{'error-message'});
}
}
return $arr;
}