Subversion Repositories cheapmusic

Rev

Rev 28 | Rev 39 | 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";
121
    				break;
122
 
123
    			case 1595844: // Sheet Music Plus
24 - 124
       			    $freeShippingCap = 35.00;
125
           			$handlingTime = 1;
126
           			$shippingCost = 3.99;
127
           			$shippingCurrency = 'USD';
128
                    $type = 'New';
21 - 129
   				    $condition = "Brand New";
130
    				$category = "Book";
131
    				break;
132
 
133
    			case 1111879: // ArkivMusic
24 - 134
       			    $freeShippingCap = 0.00;
135
           			$handlingTime = 1;
136
           			$shippingCost = 2.99;
137
           			$shippingCurrency = 'USD';
138
                    $type = 'New';
21 - 139
   				    $condition = "Brand New";
140
    				$category = "CD";
141
    				break;
142
 
143
   				default:
24 - 144
       			    $freeShippingCap = 0.00;
145
           			$handlingTime = -1;
146
           			$shippingCost = 0.00;
147
           			$shippingCurrency = 'USD';
148
                    $type = 'Used';
21 - 149
   				    $condition = "Used";
150
    				$category = "CD";
151
    				break;
20 - 152
			}
153
 
154
			$country = 'US';
155
			$bestOffer = false;
156
 
157
            $price = 0;
158
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
159
    			$price = minNotNull(array($product->{'price'}, $product->{'sale-price'}));
160
    		} else if (!empty($product->{'price'})) {
161
    			$price = $product->{'price'};
162
    		} else if (!empty($product->{'sale-price'})) {
163
    			$price = $product->{'sale-price'};
164
    		}
165
			$price = number_format(floatval($price), 2, '.', '');
166
			if ($price <= "0.00") {
167
			    continue;
168
			}
169
 
170
			$currency = 'USD';
171
			$timeLeft = 0;
172
			$listingType = 'Fixed';
173
			$location = 'US';
174
			$zip = '';
175
			$feedbackScore = -1;
176
			$feedbackPercent = -1;
177
			$sellerName = '';
178
 
179
// bugbug
21 - 180
//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 - 181
 
182
            // this is last after all checks
183
			$resultsMid[] = $merchantId;
184
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
185
			    continue;
186
			}
187
 
188
			$arr[] = array(
189
				"Merchant" => $merchantName,
24 - 190
				"Type" => "$type",
20 - 191
				"Title" => "$title",
192
				"Barcode" => "$barcode",
193
				"BarcodeType" => "$barcodeType",
194
				"Image" => "$pic",
195
				"URL" => "$url",
196
				"Category" => "$category",
197
				"Condition" => "$condition",
198
				"Country" => "$country",
199
				"BestOffer" => "$bestOffer",
200
				"TimeLeft" => "$timeLeft",
201
				"Price" => "$price",
202
				"Currency" => "$currency",
203
				"ListingType" => "$listingType",
204
				"Location" => "$location",
205
				"Zip" => "$zip",
206
				"FeedbackScore" => "$feedbackScore",
207
				"FeedbackPercent" => "$feedbackPercent",
208
				"SellerName" => "$sellerName",
209
				"HandlingTime" => "$handlingTime",
210
				"ShippingCost" => "$shippingCost",
211
				"ShippingCurrency" => "$shippingCurrency",
24 - 212
				"FreeShippingCap" => "$freeShippingCap",
20 - 213
				"Show" => true
214
			);
215
		}
216
	}
217
// If the response does not indicate 'Success,' log an error
218
	else {
219
    	error_log($url);
220
    	if (isset($result->{'error-message'})) {
221
    		error_log($result->{'error-message'});
222
    	}
223
	}
21 - 224
 
20 - 225
	return $arr;
226
}