Subversion Repositories cheapmusic

Rev

Rev 85 | Rev 94 | 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";
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;
202
            if (array_count_values($resultsMid) [$merchantId] > $numResultsMid) {
203
                continue;
204
            }
20 - 205
 
65 - 206
            $arr[] = array(
207
                "Merchant" => $merchantName,
81 - 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,
65 - 232
                "Show" => true
233
            );
234
        }
235
    }
236
    // If the response does not indicate 'Success,' log the error(s)
237
    else {
73 - 238
        if (!empty($result->Errors)) {
239
            foreach ($result->Errors as $error) {
240
                if ($error->ErrorID != "7186919") { // no product found, not an error
241
                    error_log($url);
242
                    error_log("$error->ErrorText ($error->ErrorId)");
243
                }
65 - 244
            }
73 - 245
        } else {
246
            error_log($url);
247
            error_log("No result or error message.");
65 - 248
        }
249
    }
57 - 250
 
65 - 251
    return $arr;
20 - 252
}
253
 
254
// Get Linkshare Bearer Token
65 - 255
function getLinkshareToken($config) {
20 - 256
    static $expiration = 0;
257
    static $accessToken = '';
258
    static $refreshToken = '';
259
 
260
    if ($expiration == 0 || time() > $expiration) {
65 - 261
        $url = "https://api.rakutenmarketing.com/token";
20 - 262
        $postdata = "grant_type=password&username=" . $config['user'] . "&password=" . $config['password'] . "&scope=" . $config['scope'];
263
        $header = array(
264
            'Content-Type: application/x-www-form-urlencoded',
265
            'Authorization: ' . $config['authorizationToken']
65 - 266
        );
20 - 267
 
268
        $ch = curl_init();
269
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
270
        curl_setopt($ch, CURLOPT_URL, $url);
65 - 271
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
89 - 272
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
273
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
20 - 274
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
275
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
276
        curl_setopt($ch, CURLOPT_REFERER, $url);
277
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
278
        curl_setopt($ch, CURLOPT_POST, 1);
65 - 279
        $result = curl_exec($ch);
53 - 280
        $result = json_decode($result);
20 - 281
        curl_close($ch);
282
 
38 - 283
        if (empty($result)) {
284
            return false;
285
        }
286
 
20 - 287
        $expiration = time() + $result->{'expires_in'};
288
        $accessToken = $result->{'access_token'};
289
        $refreshToken = $result->{'refresh_token'};
65 - 290
        $tokenType = $result->{'token_type'};
20 - 291
    }
292
 
38 - 293
    return $accessToken;
20 - 294
}
41 - 295
 
296
// get linkshare coupon codes
65 - 297
function get_linkshareCoupons() {
41 - 298
    $vendors = Vendors::getInstance();
299
    $config = $vendors->getVendor(Vendors::LINKSHARE);
300
 
301
    $token = getLinkshareToken($config);
302
    if ($token === false) {
303
        return [];
304
    }
305
 
65 - 306
    // Construct the findItemsByKeywords HTTP GET call
41 - 307
    $url = "https://api.rakutenmarketing.com/coupon/1.0?category=22&network=1&resultsperpage=100&pagenumber=1";
308
 
309
    $header = array(
310
        'Content-Type: application/x-www-form-urlencoded',
311
        'Accept: application/xml',
312
        'Accept-Language: en-US,en;q=0.5',
313
        'Accept-Charset: UTF-8,*;q=0.5',
314
        'Authorization: Bearer ' . $token
65 - 315
    );
41 - 316
 
317
    $ch = curl_init();
318
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
319
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
320
    curl_setopt($ch, CURLOPT_URL, $url);
321
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
322
    curl_setopt($ch, CURLOPT_HEADER, 0);
89 - 323
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
324
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
41 - 325
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 326
    $result = curl_exec($ch);
41 - 327
 
65 - 328
    $result = utf8_encode($result);
329
    $result = simplexml_load_string($result);
41 - 330
 
331
    curl_close($ch);
332
 
65 - 333
    //echo "$url<br><pre>";print_r($result);echo "</pre>";
334
    $arr = [];
41 - 335
 
65 - 336
    // Check to see if the request found any results
337
    if (isset($result->TotalMatches)) {
338
        // If the response was loaded, parse it and store in array
339
        foreach ($result->link as $link) {
340
            echo "NULL";
341
            echo "," . $link->advertisername;
342
            echo "," . $link->offerstartdate;
343
            echo "," . $link->offerenddate;
344
            echo ",\"" . $link->offerdescription . "\"";
345
            echo "," . $link->couponcode;
346
            echo "," . str_replace('http://', 'https://', (string)$link->clickurl);
347
            echo "," . str_replace('http://', 'https://', (string)$link->impressionpixel);
348
            echo "<br>";
349
        }
350
    }
351
    // If the response does not indicate 'Success,' log the error(s)
352
    else {
353
        foreach ($result->Errors as $error) {
354
            if ($error->ErrorID != "7186919") { // no product found, not an error
355
                error_log($url);
356
                error_log("$error->ErrorText ($error->ErrorId)");
357
            }
358
        }
359
    }
41 - 360
 
65 - 361
    return $arr;
83 - 362
}