Subversion Repositories cheapmusic

Rev

Rev 21 | Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 - 1
<?php
2
include_once('php/constants.php');
3
 
4
error_reporting(E_ALL);
5
 
6
// Get CJ Affiliate listings
7
function get_cjaffiliate($query, $searchType) {
8
    $vendors = Vendors::getInstance();
9
    $config = $vendors->getVendor(Vendors::CJAFFILIATE);
10
 
11
    $flatShipping = array (
12
        "4258829"=>"2.98",  // Barnes & Noble
13
        "129899"=>"3.00",   // Booksamillion.com
14
        "4911961"=>"3.24",  // FYE.com
15
        "4926671"=>"3.99",  // Secondspin.com
16
        "1111879"=>"2.99",  // ArkivMusic
17
        "5153127"=>"0.00",  // OnBuy.com
18
        "1595844"=>"0.00"   // Sheet Music Plus
19
    );
20
 
21
// API request variables
22
    $query = '+' . str_replace(" ", " +", $query); // enforce 'and' logic
23
	$safequery = urlencode($query);
24
	$numResults = $config['numResults'];
25
	$numResultsMid = $config['numResultsMid'];
26
	$resultsMid = [];
27
 
28
// Construct the findItemsByKeywords HTTP GET call
29
    $url = "https://product-search.api.cj.com/v2/product-search";
30
    $url .= "?website-id=" . $config['websiteId'];
31
 
32
    if (in_array($_SESSION["barcode"]["Type"], array("UPC","EAN","ISBN"))) {
33
        $url .= "&upc=";
34
    } else {
35
        $url .= "&keywords=";
36
    }
37
    $url .= $safequery;
38
 
39
    $url .= "&records-per-page=" . $numResults;
40
    $url .= "&page-number=1";
41
    $url .= "&sort-by=price";
42
    $url .= "&sort-order=asc";
43
    $url .= "&advertiser-ids=" . $config['advertiserIds'];
44
 
45
    $header = array(
46
        'Content-Type: application/x-www-form-urlencoded',
47
        'Accept: application/xml',
48
        'Accept-Language: en-US,en;q=0.5',
49
        'Accept-Charset: UTF-8,*;q=0.5',
50
        'Authorization: ' . $config['personalAccessToken']
51
        );
52
 
53
    $ch = curl_init();
54
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
55
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
56
    curl_setopt($ch, CURLOPT_URL, $url);
57
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
58
    curl_setopt($ch, CURLOPT_HEADER, 0);
59
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
60
    $result = curl_exec ($ch);
61
 
62
	$result = utf8_encode($result);
63
	$result = simplexml_load_string($result);
64
 
65
    curl_close($ch);
66
 
67
//echo "<pre>";print_r($result);echo "</pre>";
68
 
69
	$arr = [];
70
 
71
// Check to see if the request found any results
72
	if (isset($result->products['total-matched'])) {
73
    // If the response was loaded, parse it and store in array
74
		foreach ($result->{'products'}->{'product'} as $product) {
75
		    $barcode = (string)$product->{'upc'};
76
		    $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
77
 
78
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(95844)
79
            // pending: FYE(4911961)
80
            // declined: Barnes & Noble, Booksamillion
81
 
82
			$merchantId = strval($product->{'advertiser-id'});
83
			$merchantName = (string)$product->{'advertiser-name'};
84
 
85
			$title = (string)$product->{'name'};
86
			$pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
87
			if (empty($pic)) {
88
			    continue;
89
			}
90
			$url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
91
			if (empty($url)) {
92
			    continue;
93
			}
94
 
95
			// bugbug check categories by merchant
96
			$category = "CD";
97
			if ($category == "CDs") { //bugbug
98
				$category = "CD";
99
			}
100
 
101
			$condition = 'Brand New'; // bugbug
102
			$country = 'US';
103
			$bestOffer = false;
104
 
105
            $price = 0;
106
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
107
    			$price = minNotNull(array($product->{'price'}, $product->{'sale-price'}));
108
    		} else if (!empty($product->{'price'})) {
109
    			$price = $product->{'price'};
110
    		} else if (!empty($product->{'sale-price'})) {
111
    			$price = $product->{'sale-price'};
112
    		}
113
			$price = number_format(floatval($price), 2, '.', '');
114
			if ($price <= "0.00") {
115
			    continue;
116
			}
117
 
118
			$currency = 'USD';
119
			$timeLeft = 0;
120
			$listingType = 'Fixed';
121
			$location = 'US';
122
			$zip = '';
123
			$feedbackScore = -1;
124
			$feedbackPercent = -1;
125
			$sellerName = '';
126
			$handlingTime = -1;
127
			$shippingCost = $flatShipping[$merchantId];
128
			$shippingCurrency = 'USD';
129
 
130
// bugbug
131
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'}));
132
 
133
            // this is last after all checks
134
			$resultsMid[] = $merchantId;
135
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
136
			    continue;
137
			}
138
 
139
			$arr[] = array(
140
				"Merchant" => $merchantName,
141
				"Type" => ($searchType == constant("NEW") ? 'New' : 'Used'), // bugbug
142
				"Title" => "$title",
143
				"Barcode" => "$barcode",
144
				"BarcodeType" => "$barcodeType",
145
				"Image" => "$pic",
146
				"URL" => "$url",
147
				"Category" => "$category",
148
				"Condition" => "$condition",
149
				"Country" => "$country",
150
				"BestOffer" => "$bestOffer",
151
				"TimeLeft" => "$timeLeft",
152
				"Price" => "$price",
153
				"Currency" => "$currency",
154
				"ListingType" => "$listingType",
155
				"Location" => "$location",
156
				"Zip" => "$zip",
157
				"FeedbackScore" => "$feedbackScore",
158
				"FeedbackPercent" => "$feedbackPercent",
159
				"SellerName" => "$sellerName",
160
				"HandlingTime" => "$handlingTime",
161
				"ShippingCost" => "$shippingCost",
162
				"ShippingCurrency" => "$shippingCurrency",
163
				"Show" => true
164
			);
165
		}
166
	}
167
// If the response does not indicate 'Success,' log an error
168
	else {
169
    	error_log($url);
170
    	if (isset($result->{'error-message'})) {
171
    		error_log($result->{'error-message'});
172
    	}
173
	}
174
 
175
	return $arr;
176
}