Subversion Repositories cheapmusic

Rev

Rev 81 | Rev 85 | 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);
61
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 62
    $result = curl_exec($ch);
20 - 63
 
65 - 64
    $result = utf8_encode($result);
65
    $result = simplexml_load_string($result);
20 - 66
 
67
    curl_close($ch);
68
 
65 - 69
    //echo "$url<br><pre>";print_r($result);echo "</pre>";
70
    $arr = [];
21 - 71
 
65 - 72
    // Check to see if the request found any results
73
    if (isset($result->TotalMatches)) {
74
        // If the response was loaded, parse it and store in array
75
        foreach ($result->item as $item) {
76
            $merchantId = strval($item->mid);
77
            $merchantName = (string)$item->merchantname;
78
            $barcode = (string)$item->upccode;
79
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
20 - 80
 
65 - 81
            $title = (string)$item->productname;
82
            $pic = str_replace('http://', 'https://', (string)$item->imageurl);
83
            if (empty($pic)) {
84
                continue;
85
            }
86
            $url = str_replace('http://', 'https://', (string)$item->linkurl);
87
            if (empty($url)) {
88
                continue;
89
            }
20 - 90
 
65 - 91
            $country = 'US';
20 - 92
 
65 - 93
            switch ($merchantId) {
94
                case 35126: // Sam Ash Music
66 - 95
                    $merchantName = "Sam Ash";
96
                    if ($item->category->primary == "Books/Dvds") {
97
                        $condition = 'New';
98
                        $detailCondition = "Brand New";
99
                        $mediaType = "Book";
65 - 100
                        $freeShippingCap = 0.00; // 9.99 on webpage
101
                        $handlingTime = 1;
102
                        $shippingCost = 0.00;
81 - 103
                        $shippingEstimated = false;
65 - 104
                        $shippingCurrency = 'USD';
105
                    }
106
                    else {
107
                        continue 2; // next loop
108
 
109
                    }
110
                break;
21 - 111
 
65 - 112
                case 13770: // Music Notes
66 - 113
                    $condition = 'New';
114
                    $detailCondition = "Brand New";
115
                    $mediaType = "Book";
65 - 116
                    $freeShippingCap = 0.00;
117
                    $handlingTime = 1;
118
                    $shippingCost = 0.00;
81 - 119
                    $shippingEstimated = false;
65 - 120
                    $shippingCurrency = 'USD';
121
                break;
21 - 122
 
65 - 123
                case 2653: // alibris
66 - 124
                    /* fall through */
65 - 125
                case 24390: // alibris uk
66 - 126
                    $merchantName = "Alibris";
127
                    if ($merchantId == 24390) {
128
                        $merchantName .= " UK";
65 - 129
                    }
66 - 130
                    if ($item->category->primary == "Media > Music" || $item->category->primary == "Media > Dvds & Movies") {
131
                        $mediaType = "CD";
132
                    }
65 - 133
                    else {
66 - 134
                        $mediaType = "Book";
50 - 135
                    }
66 - 136
                    $condition = 'Used';
137
                    $detailCondition = "Used";
65 - 138
                    $handlingTime = 1;
139
                    if ($merchantId == 2653) {
140
                        $shippingCost = 3.99;
141
                        $freeShippingCap = 39.00;
142
                    }
143
                    else { /* 24390 */
144
                        $shippingCost = 4.25;
145
                        $freeShippingCap = 0.00;
146
                        $country = 'GB';
147
                    }
81 - 148
                    $shippingEstimated = true;
65 - 149
                    $shippingCurrency = 'USD';
150
                break;
21 - 151
 
83 - 152
                case 2149: // Walmart
153
                    continue;
154
                break;
155
 
65 - 156
                default:
66 - 157
                    $condition = 'Used';
158
                    $detailCondition = "Used";
159
                    $mediaType = "CD";
65 - 160
                    $freeShippingCap = 0.00;
161
                    $handlingTime = - 1;
81 - 162
                    $shippingEstimated = true;
65 - 163
                    $shippingCost = 0.00;
164
                    $shippingCurrency = 'USD';
165
                break;
166
            }
20 - 167
 
65 - 168
            $bestOffer = false;
20 - 169
 
65 - 170
            $price = 0;
171
            if (!empty($item->price) && !empty($item->saleprice)) {
172
                $price = minNotNull(array(
173
                    $item->price,
174
                    $item->saleprice
175
                ));
176
            }
177
            else if (!empty($item->price)) {
178
                $price = $item->price;
179
            }
180
            else if (!empty($item->saleprice)) {
181
                $price = $item->saleprice;
182
            }
183
            $price = number_format(floatval($price) , 2, '.', '');
184
            if ($price <= "0.00") {
185
                continue;
186
            }
20 - 187
 
65 - 188
            $currency = 'USD';
189
            $timeLeft = 0;
190
            $listingType = 'Fixed';
191
            $location = 'US';
192
            $zip = '';
193
            $feedbackScore = - 1;
194
            $feedbackPercent = - 1;
195
            $sellerName = '';
20 - 196
 
65 - 197
            // bugbug
198
            //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 - 199
            // this is last after all checks
65 - 200
            $resultsMid[] = $merchantId;
201
            if (array_count_values($resultsMid) [$merchantId] > $numResultsMid) {
202
                continue;
203
            }
20 - 204
 
65 - 205
            $arr[] = array(
206
                "Merchant" => $merchantName,
81 - 207
                "Condition" => $condition,
208
                "Title" => $title,
209
                "Barcode" => $barcode,
210
                "BarcodeType" => $barcodeType,
211
                "Image" => $pic,
212
                "URL" => $url,
213
                "MediaType" => $mediaType,
214
                "DetailCondition" => $detailCondition,
215
                "Country" => $country,
216
                "BestOffer" => $bestOffer,
217
                "TimeLeft" => $timeLeft,
218
                "Price" => $price,
219
                "Currency" => $currency,
220
                "ListingType" => $listingType,
221
                "Location" => $location,
222
                "Zip" => $zip,
223
                "FeedbackScore" => $feedbackScore,
224
                "FeedbackPercent" => $feedbackPercent,
225
                "SellerName" => $sellerName,
226
                "HandlingTime" => $handlingTime,
227
                "ShippingCost" => $shippingCost,
228
                "ShippingEstimated" => $shippingEstimated,
229
                "ShippingCurrency" => $shippingCurrency,
230
                "FreeShippingCap" => $freeShippingCap,
65 - 231
                "Show" => true
232
            );
233
        }
234
    }
235
    // If the response does not indicate 'Success,' log the error(s)
236
    else {
73 - 237
        if (!empty($result->Errors)) {
238
            foreach ($result->Errors as $error) {
239
                if ($error->ErrorID != "7186919") { // no product found, not an error
240
                    error_log($url);
241
                    error_log("$error->ErrorText ($error->ErrorId)");
242
                }
65 - 243
            }
73 - 244
        } else {
245
            error_log($url);
246
            error_log("No result or error message.");
65 - 247
        }
248
    }
57 - 249
 
65 - 250
    return $arr;
20 - 251
}
252
 
253
// Get Linkshare Bearer Token
65 - 254
function getLinkshareToken($config) {
20 - 255
    static $expiration = 0;
256
    static $accessToken = '';
257
    static $refreshToken = '';
258
 
259
    if ($expiration == 0 || time() > $expiration) {
65 - 260
        $url = "https://api.rakutenmarketing.com/token";
20 - 261
        $postdata = "grant_type=password&username=" . $config['user'] . "&password=" . $config['password'] . "&scope=" . $config['scope'];
262
        $header = array(
263
            'Content-Type: application/x-www-form-urlencoded',
264
            'Authorization: ' . $config['authorizationToken']
65 - 265
        );
20 - 266
 
267
        $ch = curl_init();
268
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
269
        curl_setopt($ch, CURLOPT_URL, $url);
65 - 270
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
20 - 271
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
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);
321
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 322
    $result = curl_exec($ch);
41 - 323
 
65 - 324
    $result = utf8_encode($result);
325
    $result = simplexml_load_string($result);
41 - 326
 
327
    curl_close($ch);
328
 
65 - 329
    //echo "$url<br><pre>";print_r($result);echo "</pre>";
330
    $arr = [];
41 - 331
 
65 - 332
    // Check to see if the request found any results
333
    if (isset($result->TotalMatches)) {
334
        // If the response was loaded, parse it and store in array
335
        foreach ($result->link as $link) {
336
            echo "NULL";
337
            echo "," . $link->advertisername;
338
            echo "," . $link->offerstartdate;
339
            echo "," . $link->offerenddate;
340
            echo ",\"" . $link->offerdescription . "\"";
341
            echo "," . $link->couponcode;
342
            echo "," . str_replace('http://', 'https://', (string)$link->clickurl);
343
            echo "," . str_replace('http://', 'https://', (string)$link->impressionpixel);
344
            echo "<br>";
345
        }
346
    }
347
    // If the response does not indicate 'Success,' log the error(s)
348
    else {
349
        foreach ($result->Errors as $error) {
350
            if ($error->ErrorID != "7186919") { // no product found, not an error
351
                error_log($url);
352
                error_log("$error->ErrorText ($error->ErrorId)");
353
            }
354
        }
355
    }
41 - 356
 
65 - 357
    return $arr;
83 - 358
}