Subversion Repositories cheapmusic

Rev

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