Rev 143 | 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
$numResults = $config['numResults'];
$numResultsMid = $config['numResultsMid'];
$resultsMid = [];
$url = '[cached]';
$result = getSearchCache("cjaffiliate", $query, $searchType);
if ($result === false) {
$params = [];
$params["website-id"] = $config['websiteId'];
if (!empty($_SESSION["advSearch"]["Barcode"])) {
$params["upc"] = $_SESSION["advSearch"]["Barcode"];
}
else {
$params["keywords"] = $query;
}
$params["records-per-page"] = $numResults;
$params["page-number"] = "1";
$params["sort-by"] = "price";
$params["sort-order"] = "asc";
$params["advertiser-ids"] = $config['advertiserIds'];
$pairs = array();
foreach ($params as $key => $value) {
array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
}
$canonical_query_string = join("&", $pairs);
$url = "https://product-search.api.cj.com/v2/product-search?" . $canonical_query_string;
$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();
if (defined('CURL_HTTP_VERSION_2_0')) {
curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_2_0);
}
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_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$result = curl_exec($ch);
curl_close($ch);
saveSearchCache("cjaffiliate", $query, $searchType, $result);
}
if (empty($result)) {
return [];
}
$result = utf8_encode($result);
$result = simplexml_load_string($result);
//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 2;
}
}
$url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
if (empty($url)) {
continue;
}
switch ($merchantId) {
case 4911961: // FYE.com
$merchantName = "FYE";
$mediaType = "CD";
$freeShippingCap = 40.00;
$handlingTime = 1;
$shippingCost = 3.24;
$shippingEstimated = true;
$shippingCurrency = 'USD';
if (preg_match('/\d\d\d\d$/', $title)) {
$condition = 'Used';
$detailCondition = "Used";
$title = rtrim(substr($title, 0, -4));
}
else {
$condition = 'New';
$detailCondition = "Brand New";
}
break;
case 4926671: // Secondspin.com
$merchantName = "Secondspin";
$freeShippingCap = 0.00;
$handlingTime = 1;
$shippingCost = 3.99;
$shippingEstimated = true;
$shippingCurrency = 'USD';
$condition = 'Used';
$detailCondition = "Used";
$mediaType = "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;
$shippingEstimated = true;
$shippingCurrency = 'USD';
$condition = 'New';
$detailCondition = "Brand New";
$mediaType = "Book";
break;
case 1111879: // ArkivMusic
$freeShippingCap = 0.00;
$handlingTime = 1;
$shippingCost = 2.99;
$shippingEstimated = true;
$shippingCurrency = 'USD';
$condition = 'New';
$detailCondition = "Brand New";
$mediaType = "CD";
break;
default:
$freeShippingCap = 0.00;
$handlingTime = - 1;
$shippingCost = 0.00;
$shippingEstimated = true;
$shippingCurrency = 'USD';
$condition = 'Used';
$detailCondition = "Used";
$mediaType = "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 ($numResultsMid > array_count_values($resultsMid) [$merchantId]) {
$arr[] = array(
"Merchant" => $merchantName,
"Condition" => $condition,
"Title" => $title,
"Barcode" => $barcode,
"BarcodeType" => $barcodeType,
"Image" => $pic,
"URL" => $url,
"MediaType" => $mediaType,
"DetailCondition" => $detailCondition,
"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,
"ShippingEstimated" => $shippingEstimated,
"ShippingCurrency" => $shippingCurrency,
"FreeShippingCap" => $freeShippingCap,
"Show" => true,
"Details" => ""
);
}
}
}
// If the response does not indicate 'Success,' log an error
else {
my_error_log($url);
if (isset($result->{'error-message'})) {
my_error_log($result->{'error-message'});
}
}
return $arr;
}