Subversion Repositories cheapmusic

Rev

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