Subversion Repositories cheapmusic

Rev

Rev 27 | Rev 38 | 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
 
52
	$result = utf8_encode($result);
53
	$result = simplexml_load_string($result);
54
 
55
    curl_close($ch);
56
 
28 - 57
//echo "$url<pre>";print_r($result);echo "</pre>";
20 - 58
 
59
	$arr = [];
60
 
61
// Check to see if the request found any results
62
	if (isset($result->products['total-matched'])) {
63
    // If the response was loaded, parse it and store in array
64
		foreach ($result->{'products'}->{'product'} as $product) {
65
		    $barcode = (string)$product->{'upc'};
66
		    $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
67
 
21 - 68
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 69
            // pending: FYE(4911961)
21 - 70
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
71
 
20 - 72
			$merchantId = strval($product->{'advertiser-id'});
73
			$merchantName = (string)$product->{'advertiser-name'};
74
 
75
			$title = (string)$product->{'name'};
76
			$pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
77
			if (empty($pic)) {
21 - 78
			    switch ($merchantId) {
79
        			case 1111879: // ArkivMusic
80
        			    $pic = "images/no-image-available.jpg";
81
			            break;
82
 
83
			        default:
84
        			    continue;
85
			    }
20 - 86
			}
87
			$url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
88
			if (empty($url)) {
89
			    continue;
90
			}
91
 
21 - 92
			switch ($merchantId) {
93
			    case 4911961: // FYE.com
94
    				$category = "CD";
24 - 95
       			    $freeShippingCap = 40.00;
96
           			$handlingTime = 1;
97
           			$shippingCost = 3.24;
98
           			$shippingCurrency = 'USD';
21 - 99
    				if (preg_match('/\d\d\d\d$/', $title)) {
24 - 100
                        $type = 'Used';
21 - 101
    				    $condition = "Used";
102
    				    $title = rtrim(substr($title, 0, -4));
103
    				} else {
24 - 104
                        $type = 'New';
21 - 105
    				    $condition = "Brand New";
106
    				}
107
    				break;
108
 
109
   				case 4926671: // Secondspin.com
24 - 110
       			    $freeShippingCap = 0.00;
111
           			$handlingTime = 1;
112
           			$shippingCost = 3.99;
113
           			$shippingCurrency = 'USD';
114
                    $type = 'Used';
21 - 115
   				    $condition = "Used";
116
    				$category = "CD";
117
    				break;
118
 
119
    			case 1595844: // Sheet Music Plus
24 - 120
       			    $freeShippingCap = 35.00;
121
           			$handlingTime = 1;
122
           			$shippingCost = 3.99;
123
           			$shippingCurrency = 'USD';
124
                    $type = 'New';
21 - 125
   				    $condition = "Brand New";
126
    				$category = "Book";
127
    				break;
128
 
129
    			case 1111879: // ArkivMusic
24 - 130
       			    $freeShippingCap = 0.00;
131
           			$handlingTime = 1;
132
           			$shippingCost = 2.99;
133
           			$shippingCurrency = 'USD';
134
                    $type = 'New';
21 - 135
   				    $condition = "Brand New";
136
    				$category = "CD";
137
    				break;
138
 
139
   				default:
24 - 140
       			    $freeShippingCap = 0.00;
141
           			$handlingTime = -1;
142
           			$shippingCost = 0.00;
143
           			$shippingCurrency = 'USD';
144
                    $type = 'Used';
21 - 145
   				    $condition = "Used";
146
    				$category = "CD";
147
    				break;
20 - 148
			}
149
 
150
			$country = 'US';
151
			$bestOffer = false;
152
 
153
            $price = 0;
154
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
155
    			$price = minNotNull(array($product->{'price'}, $product->{'sale-price'}));
156
    		} else if (!empty($product->{'price'})) {
157
    			$price = $product->{'price'};
158
    		} else if (!empty($product->{'sale-price'})) {
159
    			$price = $product->{'sale-price'};
160
    		}
161
			$price = number_format(floatval($price), 2, '.', '');
162
			if ($price <= "0.00") {
163
			    continue;
164
			}
165
 
166
			$currency = 'USD';
167
			$timeLeft = 0;
168
			$listingType = 'Fixed';
169
			$location = 'US';
170
			$zip = '';
171
			$feedbackScore = -1;
172
			$feedbackPercent = -1;
173
			$sellerName = '';
174
 
175
// bugbug
21 - 176
//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 - 177
 
178
            // this is last after all checks
179
			$resultsMid[] = $merchantId;
180
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
181
			    continue;
182
			}
183
 
184
			$arr[] = array(
185
				"Merchant" => $merchantName,
24 - 186
				"Type" => "$type",
20 - 187
				"Title" => "$title",
188
				"Barcode" => "$barcode",
189
				"BarcodeType" => "$barcodeType",
190
				"Image" => "$pic",
191
				"URL" => "$url",
192
				"Category" => "$category",
193
				"Condition" => "$condition",
194
				"Country" => "$country",
195
				"BestOffer" => "$bestOffer",
196
				"TimeLeft" => "$timeLeft",
197
				"Price" => "$price",
198
				"Currency" => "$currency",
199
				"ListingType" => "$listingType",
200
				"Location" => "$location",
201
				"Zip" => "$zip",
202
				"FeedbackScore" => "$feedbackScore",
203
				"FeedbackPercent" => "$feedbackPercent",
204
				"SellerName" => "$sellerName",
205
				"HandlingTime" => "$handlingTime",
206
				"ShippingCost" => "$shippingCost",
207
				"ShippingCurrency" => "$shippingCurrency",
24 - 208
				"FreeShippingCap" => "$freeShippingCap",
20 - 209
				"Show" => true
210
			);
211
		}
212
	}
213
// If the response does not indicate 'Success,' log an error
214
	else {
215
    	error_log($url);
216
    	if (isset($result->{'error-message'})) {
217
    		error_log($result->{'error-message'});
218
    	}
219
	}
21 - 220
 
20 - 221
	return $arr;
222
}