Subversion Repositories cheapmusic

Rev

Rev 38 | Rev 65 | Go to most recent revision | Details | Compare with Previous | 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);
21 - 10
 
20 - 11
// API request variables
12
    $query = '+' . str_replace(" ", " +", $query); // enforce 'and' logic
13
	$safequery = urlencode($query);
14
	$numResults = $config['numResults'];
15
	$numResultsMid = $config['numResultsMid'];
16
	$resultsMid = [];
17
 
18
// Construct the findItemsByKeywords HTTP GET call
19
    $url = "https://product-search.api.cj.com/v2/product-search";
20
    $url .= "?website-id=" . $config['websiteId'];
21
 
22
    if (in_array($_SESSION["barcode"]["Type"], array("UPC","EAN","ISBN"))) {
23
        $url .= "&upc=";
24
    } else {
25
        $url .= "&keywords=";
26
    }
27
    $url .= $safequery;
28
 
29
    $url .= "&records-per-page=" . $numResults;
30
    $url .= "&page-number=1";
31
    $url .= "&sort-by=price";
32
    $url .= "&sort-order=asc";
33
    $url .= "&advertiser-ids=" . $config['advertiserIds'];
34
 
35
    $header = array(
36
        'Content-Type: application/x-www-form-urlencoded',
37
        'Accept: application/xml',
38
        'Accept-Language: en-US,en;q=0.5',
39
        'Accept-Charset: UTF-8,*;q=0.5',
40
        'Authorization: ' . $config['personalAccessToken']
41
        );
42
 
43
    $ch = curl_init();
44
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
45
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
46
    curl_setopt($ch, CURLOPT_URL, $url);
47
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
48
    curl_setopt($ch, CURLOPT_HEADER, 0);
49
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
50
    $result = curl_exec ($ch);
51
 
38 - 52
    if (empty($result)) {
53
        return [];
54
    }
55
 
20 - 56
	$result = utf8_encode($result);
57
	$result = simplexml_load_string($result);
58
 
59
    curl_close($ch);
60
 
28 - 61
//echo "$url<pre>";print_r($result);echo "</pre>";
20 - 62
 
63
	$arr = [];
64
 
65
// Check to see if the request found any results
66
	if (isset($result->products['total-matched'])) {
67
    // If the response was loaded, parse it and store in array
68
		foreach ($result->{'products'}->{'product'} as $product) {
69
		    $barcode = (string)$product->{'upc'};
70
		    $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
71
 
21 - 72
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 73
            // pending: FYE(4911961)
21 - 74
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
75
 
20 - 76
			$merchantId = strval($product->{'advertiser-id'});
77
			$merchantName = (string)$product->{'advertiser-name'};
78
 
79
			$title = (string)$product->{'name'};
80
			$pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
81
			if (empty($pic)) {
21 - 82
			    switch ($merchantId) {
83
        			case 1111879: // ArkivMusic
84
        			    $pic = "images/no-image-available.jpg";
85
			            break;
86
 
87
			        default:
88
        			    continue;
89
			    }
20 - 90
			}
91
			$url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
92
			if (empty($url)) {
93
			    continue;
94
			}
95
 
21 - 96
			switch ($merchantId) {
97
			    case 4911961: // FYE.com
98
    				$category = "CD";
24 - 99
       			    $freeShippingCap = 40.00;
100
           			$handlingTime = 1;
101
           			$shippingCost = 3.24;
102
           			$shippingCurrency = 'USD';
21 - 103
    				if (preg_match('/\d\d\d\d$/', $title)) {
24 - 104
                        $type = 'Used';
21 - 105
    				    $condition = "Used";
106
    				    $title = rtrim(substr($title, 0, -4));
107
    				} else {
24 - 108
                        $type = 'New';
21 - 109
    				    $condition = "Brand New";
110
    				}
111
    				break;
112
 
113
   				case 4926671: // Secondspin.com
24 - 114
       			    $freeShippingCap = 0.00;
115
           			$handlingTime = 1;
116
           			$shippingCost = 3.99;
117
           			$shippingCurrency = 'USD';
118
                    $type = 'Used';
21 - 119
   				    $condition = "Used";
120
    				$category = "CD";
39 - 121
    				$pic = str_replace("fye.com", "secondspin.com", $pic);
122
    				if (endsWith($pic, "nopic-disc-300.png")) {
123
    				    $pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
124
    				}
125
    				$url = str_replace("fye.com", "secondspin.com", $url);
21 - 126
    				break;
127
 
128
    			case 1595844: // Sheet Music Plus
24 - 129
       			    $freeShippingCap = 35.00;
130
           			$handlingTime = 1;
131
           			$shippingCost = 3.99;
132
           			$shippingCurrency = 'USD';
133
                    $type = 'New';
21 - 134
   				    $condition = "Brand New";
135
    				$category = "Book";
136
    				break;
137
 
138
    			case 1111879: // ArkivMusic
24 - 139
       			    $freeShippingCap = 0.00;
140
           			$handlingTime = 1;
141
           			$shippingCost = 2.99;
142
           			$shippingCurrency = 'USD';
143
                    $type = 'New';
21 - 144
   				    $condition = "Brand New";
145
    				$category = "CD";
146
    				break;
147
 
148
   				default:
24 - 149
       			    $freeShippingCap = 0.00;
150
           			$handlingTime = -1;
151
           			$shippingCost = 0.00;
152
           			$shippingCurrency = 'USD';
153
                    $type = 'Used';
21 - 154
   				    $condition = "Used";
155
    				$category = "CD";
156
    				break;
20 - 157
			}
158
 
159
			$country = 'US';
160
			$bestOffer = false;
161
 
162
            $price = 0;
163
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
164
    			$price = minNotNull(array($product->{'price'}, $product->{'sale-price'}));
165
    		} else if (!empty($product->{'price'})) {
166
    			$price = $product->{'price'};
167
    		} else if (!empty($product->{'sale-price'})) {
168
    			$price = $product->{'sale-price'};
169
    		}
170
			$price = number_format(floatval($price), 2, '.', '');
171
			if ($price <= "0.00") {
172
			    continue;
173
			}
174
 
175
			$currency = 'USD';
176
			$timeLeft = 0;
177
			$listingType = 'Fixed';
178
			$location = 'US';
179
			$zip = '';
180
			$feedbackScore = -1;
181
			$feedbackPercent = -1;
182
			$sellerName = '';
183
 
184
// bugbug
21 - 185
//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'}));
20 - 186
 
187
            // this is last after all checks
188
			$resultsMid[] = $merchantId;
189
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
190
			    continue;
191
			}
192
 
193
			$arr[] = array(
194
				"Merchant" => $merchantName,
24 - 195
				"Type" => "$type",
20 - 196
				"Title" => "$title",
197
				"Barcode" => "$barcode",
198
				"BarcodeType" => "$barcodeType",
199
				"Image" => "$pic",
200
				"URL" => "$url",
201
				"Category" => "$category",
202
				"Condition" => "$condition",
203
				"Country" => "$country",
204
				"BestOffer" => "$bestOffer",
205
				"TimeLeft" => "$timeLeft",
206
				"Price" => "$price",
207
				"Currency" => "$currency",
208
				"ListingType" => "$listingType",
209
				"Location" => "$location",
210
				"Zip" => "$zip",
211
				"FeedbackScore" => "$feedbackScore",
212
				"FeedbackPercent" => "$feedbackPercent",
213
				"SellerName" => "$sellerName",
214
				"HandlingTime" => "$handlingTime",
215
				"ShippingCost" => "$shippingCost",
216
				"ShippingCurrency" => "$shippingCurrency",
24 - 217
				"FreeShippingCap" => "$freeShippingCap",
20 - 218
				"Show" => true
219
			);
220
		}
221
	}
222
// If the response does not indicate 'Success,' log an error
223
	else {
224
    	error_log($url);
225
    	if (isset($result->{'error-message'})) {
226
    		error_log($result->{'error-message'});
227
    	}
228
	}
21 - 229
 
20 - 230
	return $arr;
231
}