Subversion Repositories cheapmusic

Rev

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