Subversion Repositories cheapmusic

Rev

Rev 94 | Rev 99 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

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