Subversion Repositories cheapmusic

Rev

Rev 53 | 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
// Get linkshare listings
6
function get_linkshare($query, $searchType) {
7
    $vendors = Vendors::getInstance();
8
    $config = $vendors->getVendor(Vendors::LINKSHARE);
9
 
10
    $token = getLinkshareToken($config);
38 - 11
    if ($token === false) {
12
        return [];
13
    }
20 - 14
 
15
    $arr = [];
16
    foreach($config["advertiser"] as $advertiser) {
17
        $arrTemp = get_linkshareAdvertiser($config, $token, $query, $searchType, $advertiser);
18
        $arr = array_merge($arr, $arrTemp);
19
    }
57 - 20
 
20 - 21
    return($arr);
22
}
23
 
24
// Get linkshare listings by advertiser id
25
function get_linkshareAdvertiser($config, $token, $query, $searchType, $advertiserId) {
26
// API request variables
27
	$safequery = urlencode($query);
28
	$numResults = $config['numResults'];
29
	$numResultsMid = $config['numResultsMid'];
30
	$resultsMid = [];
31
 
32
// Construct the findItemsByKeywords HTTP GET call
33
    $url = "https://api.rakutenmarketing.com/productsearch/1.0";
34
    $url .= "?keyword=" . $safequery;
35
    $url .= "&max=" . $numResults;
36
    $url .= "&pagenumber=1";
37
    $url .= "&sort=retailprice";
38
    $url .= "&sorttype=asc";
39
    $url .= "&mid=" . $advertiserId;
40
 
41
    $header = array(
42
        'Content-Type: application/x-www-form-urlencoded',
43
        'Accept: application/xml',
44
        'Accept-Language: en-US,en;q=0.5',
45
        'Accept-Charset: UTF-8,*;q=0.5',
46
        'Authorization: Bearer ' . $token
47
        );
48
 
49
    $ch = curl_init();
50
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
51
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
52
    curl_setopt($ch, CURLOPT_URL, $url);
53
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
54
    curl_setopt($ch, CURLOPT_HEADER, 0);
55
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
56
    $result = curl_exec ($ch);
57
 
58
	$result = utf8_encode($result);
59
	$result = simplexml_load_string($result);
60
 
61
    curl_close($ch);
62
 
23 - 63
//echo "$url<br><pre>";print_r($result);echo "</pre>";
21 - 64
 
20 - 65
	$arr = [];
66
 
67
// Check to see if the request found any results
68
	if (isset($result->TotalMatches)) {
69
  // If the response was loaded, parse it and store in array
70
		foreach ($result->item as $item) {
71
			$merchantId = strval($item->mid);
72
			$merchantName = (string)$item->merchantname;
73
			$barcode = (string)$item->upccode;
74
			$barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
75
 
76
			$title = (string)$item->productname;
77
			$pic = str_replace('http://', 'https://', (string)$item->imageurl);
78
			if (empty($pic)) {
79
			    continue;
80
			}
81
			$url = str_replace('http://', 'https://', (string)$item->linkurl);
82
			if (empty($url)) {
83
			    continue;
84
			}
85
 
57 - 86
			$country = 'US';
87
 
21 - 88
			switch ($merchantId) {
89
			    case 35126: // Sam Ash Music
90
			        if ($item->category->primary == "Books/Dvds") {
24 - 91
                        $type = 'New';
21 - 92
   	    			    $condition = "Brand New";
93
    	    			$category = "Book";
24 - 94
        			    $freeShippingCap = 0.00; // 9.99 on webpage
95
            			$handlingTime = 1;
96
            			$shippingCost = 0.00;
97
            			$shippingCurrency = 'USD';
21 - 98
    	    		} else {
24 - 99
    	    		    continue 2; // next loop
21 - 100
    	    		}
101
			        break;
102
 
103
			    case 13770: // Music Notes
24 - 104
                    $type = 'New';
21 - 105
   				    $condition = "Brand New";
106
    				$category = "Book";
24 - 107
       			    $freeShippingCap = 0.00;
108
           			$handlingTime = 1;
109
        			$shippingCost = 0.00;
110
        			$shippingCurrency = 'USD';
21 - 111
			        break;
112
 
50 - 113
			    case 2653: // alibris
57 - 114
			    case 24390: // alibris uk
50 - 115
			        if ($item->category->primary == "Media > Music" || $item->category->primary == "Media > Dvds & Movies") {
116
                        $category = "CD";
117
			        } else {
118
                        $category = "Book";
119
                    }
120
                    $type = 'Used';
121
                    $condition = "Used";
24 - 122
           			$handlingTime = 1;
57 - 123
           			if ($merchantId == 2653) {
124
            			$shippingCost = 3.99;
125
           			    $freeShippingCap = 39.00;
126
            		} else {/* 24390 */
127
            			$shippingCost = 4.25;
128
           			    $freeShippingCap = 0.00;
129
            			$country = 'GB';
130
            		}
24 - 131
        			$shippingCurrency = 'USD';
21 - 132
			        break;
133
 
134
   				default:
24 - 135
                    $type = 'Used';
21 - 136
   				    $condition = "Used";
137
    				$category = "CD";
24 - 138
       			    $freeShippingCap = 0.00;
139
        			$handlingTime = -1;
140
        			$shippingCost = 0.00;
141
        			$shippingCurrency = 'USD';
21 - 142
    				break;
20 - 143
			}
144
 
145
			$bestOffer = false;
146
 
147
			$price = 0;
148
			if (!empty($item->price) && !empty($item->saleprice)) {
149
    			$price = minNotNull(array($item->price, $item->saleprice));
150
    		} else if (!empty($item->price)) {
151
    			$price = $item->price;
152
    		} else if (!empty($item->saleprice)) {
153
    			$price = $item->saleprice;
154
    		}
155
			$price = number_format(floatval($price), 2, '.', '');
156
			if ($price <= "0.00") {
157
			    continue;
158
			}
159
 
160
			$currency = 'USD';
161
			$timeLeft = 0;
162
			$listingType = 'Fixed';
163
			$location = 'US';
164
			$zip = '';
165
			$feedbackScore = -1;
166
			$feedbackPercent = -1;
167
			$sellerName = '';
168
 
169
// bugbug
21 - 170
//ls_cj_csv(array($merchantName,(string)$item->category->primary,(string)$item->productname,(string)$item->description->short,"'".$barcode,$barcodeType,minNotNull(array($item->price, (string)$item->saleprice)),(string)$item->linkurl));
20 - 171
 
172
            // this is last after all checks
173
			$resultsMid[] = $merchantId;
174
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
175
			    continue;
176
			}
177
 
178
			$arr[] = array(
179
				"Merchant" => $merchantName,
24 - 180
				"Type" => "$type",
20 - 181
				"Title" => "$title",
182
				"Barcode" => "$barcode",
183
				"BarcodeType" => "$barcodeType",
184
				"Image" => "$pic",
185
				"URL" => "$url",
186
				"Category" => "$category",
187
				"Condition" => "$condition",
188
				"Country" => "$country",
189
				"BestOffer" => "$bestOffer",
190
				"TimeLeft" => "$timeLeft",
191
				"Price" => "$price",
192
				"Currency" => "$currency",
193
				"ListingType" => "$listingType",
194
				"Location" => "$location",
195
				"Zip" => "$zip",
196
				"FeedbackScore" => "$feedbackScore",
197
				"FeedbackPercent" => "$feedbackPercent",
198
				"SellerName" => "$sellerName",
199
				"HandlingTime" => "$handlingTime",
200
				"ShippingCost" => "$shippingCost",
201
				"ShippingCurrency" => "$shippingCurrency",
24 - 202
				"FreeShippingCap" => "$freeShippingCap",
20 - 203
				"Show" => true
204
			);
205
		}
206
	}
207
// If the response does not indicate 'Success,' log the error(s)
208
	else {
209
		foreach ($result->Errors as $error) {
210
		    if ($error->ErrorID != "7186919") { // no product found, not an error
211
    			error_log($url);
212
	    		error_log("$error->ErrorText ($error->ErrorId)");
213
	    	}
214
		}
215
	}
57 - 216
 
20 - 217
	return $arr;
218
}
219
 
220
// Get Linkshare Bearer Token
221
function getLinkshareToken($config)
222
{
223
    static $expiration = 0;
224
    static $accessToken = '';
225
    static $refreshToken = '';
226
 
227
    if ($expiration == 0 || time() > $expiration) {
228
        $url="https://api.rakutenmarketing.com/token";
229
        $postdata = "grant_type=password&username=" . $config['user'] . "&password=" . $config['password'] . "&scope=" . $config['scope'];
230
        $header = array(
231
            'Content-Type: application/x-www-form-urlencoded',
232
            'Authorization: ' . $config['authorizationToken']
233
            );
234
 
235
        $ch = curl_init();
236
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
237
        curl_setopt($ch, CURLOPT_URL, $url);
238
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
239
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
240
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
241
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
242
        curl_setopt($ch, CURLOPT_REFERER, $url);
243
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
244
        curl_setopt($ch, CURLOPT_POST, 1);
53 - 245
        $result = curl_exec ($ch);
246
        $result = json_decode($result);
20 - 247
        curl_close($ch);
248
 
38 - 249
        if (empty($result)) {
250
            return false;
251
        }
252
 
20 - 253
        $expiration = time() + $result->{'expires_in'};
254
        $accessToken = $result->{'access_token'};
255
        $refreshToken = $result->{'refresh_token'};
256
        $tokenType =  $result->{'token_type'};
257
    }
258
 
38 - 259
    return $accessToken;
20 - 260
}
41 - 261
 
262
// get linkshare coupon codes
263
function get_linkshareCoupons($config) {
264
    $vendors = Vendors::getInstance();
265
    $config = $vendors->getVendor(Vendors::LINKSHARE);
266
 
267
    $token = getLinkshareToken($config);
268
    if ($token === false) {
269
        return [];
270
    }
271
 
272
// Construct the findItemsByKeywords HTTP GET call
273
    $url = "https://api.rakutenmarketing.com/coupon/1.0?category=22&network=1&resultsperpage=100&pagenumber=1";
274
 
275
    $header = array(
276
        'Content-Type: application/x-www-form-urlencoded',
277
        'Accept: application/xml',
278
        'Accept-Language: en-US,en;q=0.5',
279
        'Accept-Charset: UTF-8,*;q=0.5',
280
        'Authorization: Bearer ' . $token
281
        );
282
 
283
    $ch = curl_init();
284
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
285
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
286
    curl_setopt($ch, CURLOPT_URL, $url);
287
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
288
    curl_setopt($ch, CURLOPT_HEADER, 0);
289
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
290
    $result = curl_exec ($ch);
291
 
292
	$result = utf8_encode($result);
293
	$result = simplexml_load_string($result);
294
 
295
    curl_close($ch);
296
 
297
//echo "$url<br><pre>";print_r($result);echo "</pre>";
298
 
299
	$arr = [];
300
 
301
// Check to see if the request found any results
302
	if (isset($result->TotalMatches)) {
303
  // If the response was loaded, parse it and store in array
43 - 304
		foreach ($result->link as $link) {
305
		    echo "NULL";
306
		    echo "," . $link->advertisername;
307
		    echo "," . $link->offerstartdate;
308
		    echo "," . $link->offerenddate;
309
		    echo ",\"" . $link->offerdescription . "\"";
310
		    echo "," . $link->couponcode;
311
		    echo "," . str_replace('http://', 'https://', (string)$link->clickurl);
312
		    echo "," . str_replace('http://', 'https://', (string)$link->impressionpixel);
313
		    echo "<br>";
41 - 314
		}
315
	}
316
// If the response does not indicate 'Success,' log the error(s)
317
	else {
318
		foreach ($result->Errors as $error) {
319
		    if ($error->ErrorID != "7186919") { // no product found, not an error
320
    			error_log($url);
321
	    		error_log("$error->ErrorText ($error->ErrorId)");
322
	    	}
323
		}
324
	}
57 - 325
 
41 - 326
	return $arr;
327
}