Subversion Repositories cheapmusic

Rev

Rev 83 | Rev 89 | 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
85 - 153
                    continue 2;
83 - 154
 
65 - 155
                default:
66 - 156
                    $condition = 'Used';
157
                    $detailCondition = "Used";
158
                    $mediaType = "CD";
65 - 159
                    $freeShippingCap = 0.00;
160
                    $handlingTime = - 1;
81 - 161
                    $shippingEstimated = true;
65 - 162
                    $shippingCost = 0.00;
163
                    $shippingCurrency = 'USD';
164
                break;
165
            }
20 - 166
 
65 - 167
            $bestOffer = false;
20 - 168
 
65 - 169
            $price = 0;
170
            if (!empty($item->price) && !empty($item->saleprice)) {
171
                $price = minNotNull(array(
172
                    $item->price,
173
                    $item->saleprice
174
                ));
175
            }
176
            else if (!empty($item->price)) {
177
                $price = $item->price;
178
            }
179
            else if (!empty($item->saleprice)) {
180
                $price = $item->saleprice;
181
            }
182
            $price = number_format(floatval($price) , 2, '.', '');
183
            if ($price <= "0.00") {
184
                continue;
185
            }
20 - 186
 
65 - 187
            $currency = 'USD';
188
            $timeLeft = 0;
189
            $listingType = 'Fixed';
190
            $location = 'US';
191
            $zip = '';
192
            $feedbackScore = - 1;
193
            $feedbackPercent = - 1;
194
            $sellerName = '';
20 - 195
 
65 - 196
            // bugbug
197
            //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 - 198
            // this is last after all checks
65 - 199
            $resultsMid[] = $merchantId;
200
            if (array_count_values($resultsMid) [$merchantId] > $numResultsMid) {
201
                continue;
202
            }
20 - 203
 
65 - 204
            $arr[] = array(
205
                "Merchant" => $merchantName,
81 - 206
                "Condition" => $condition,
207
                "Title" => $title,
208
                "Barcode" => $barcode,
209
                "BarcodeType" => $barcodeType,
210
                "Image" => $pic,
211
                "URL" => $url,
212
                "MediaType" => $mediaType,
213
                "DetailCondition" => $detailCondition,
214
                "Country" => $country,
215
                "BestOffer" => $bestOffer,
216
                "TimeLeft" => $timeLeft,
217
                "Price" => $price,
218
                "Currency" => $currency,
219
                "ListingType" => $listingType,
220
                "Location" => $location,
221
                "Zip" => $zip,
222
                "FeedbackScore" => $feedbackScore,
223
                "FeedbackPercent" => $feedbackPercent,
224
                "SellerName" => $sellerName,
225
                "HandlingTime" => $handlingTime,
226
                "ShippingCost" => $shippingCost,
227
                "ShippingEstimated" => $shippingEstimated,
228
                "ShippingCurrency" => $shippingCurrency,
229
                "FreeShippingCap" => $freeShippingCap,
65 - 230
                "Show" => true
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
239
                    error_log($url);
240
                    error_log("$error->ErrorText ($error->ErrorId)");
241
                }
65 - 242
            }
73 - 243
        } else {
244
            error_log($url);
245
            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);
20 - 270
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
271
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
272
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
273
        curl_setopt($ch, CURLOPT_REFERER, $url);
274
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
275
        curl_setopt($ch, CURLOPT_POST, 1);
65 - 276
        $result = curl_exec($ch);
53 - 277
        $result = json_decode($result);
20 - 278
        curl_close($ch);
279
 
38 - 280
        if (empty($result)) {
281
            return false;
282
        }
283
 
20 - 284
        $expiration = time() + $result->{'expires_in'};
285
        $accessToken = $result->{'access_token'};
286
        $refreshToken = $result->{'refresh_token'};
65 - 287
        $tokenType = $result->{'token_type'};
20 - 288
    }
289
 
38 - 290
    return $accessToken;
20 - 291
}
41 - 292
 
293
// get linkshare coupon codes
65 - 294
function get_linkshareCoupons() {
41 - 295
    $vendors = Vendors::getInstance();
296
    $config = $vendors->getVendor(Vendors::LINKSHARE);
297
 
298
    $token = getLinkshareToken($config);
299
    if ($token === false) {
300
        return [];
301
    }
302
 
65 - 303
    // Construct the findItemsByKeywords HTTP GET call
41 - 304
    $url = "https://api.rakutenmarketing.com/coupon/1.0?category=22&network=1&resultsperpage=100&pagenumber=1";
305
 
306
    $header = array(
307
        'Content-Type: application/x-www-form-urlencoded',
308
        'Accept: application/xml',
309
        'Accept-Language: en-US,en;q=0.5',
310
        'Accept-Charset: UTF-8,*;q=0.5',
311
        'Authorization: Bearer ' . $token
65 - 312
    );
41 - 313
 
314
    $ch = curl_init();
315
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
316
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
317
    curl_setopt($ch, CURLOPT_URL, $url);
318
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
319
    curl_setopt($ch, CURLOPT_HEADER, 0);
320
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 321
    $result = curl_exec($ch);
41 - 322
 
65 - 323
    $result = utf8_encode($result);
324
    $result = simplexml_load_string($result);
41 - 325
 
326
    curl_close($ch);
327
 
65 - 328
    //echo "$url<br><pre>";print_r($result);echo "</pre>";
329
    $arr = [];
41 - 330
 
65 - 331
    // Check to see if the request found any results
332
    if (isset($result->TotalMatches)) {
333
        // If the response was loaded, parse it and store in array
334
        foreach ($result->link as $link) {
335
            echo "NULL";
336
            echo "," . $link->advertisername;
337
            echo "," . $link->offerstartdate;
338
            echo "," . $link->offerenddate;
339
            echo ",\"" . $link->offerdescription . "\"";
340
            echo "," . $link->couponcode;
341
            echo "," . str_replace('http://', 'https://', (string)$link->clickurl);
342
            echo "," . str_replace('http://', 'https://', (string)$link->impressionpixel);
343
            echo "<br>";
344
        }
345
    }
346
    // If the response does not indicate 'Success,' log the error(s)
347
    else {
348
        foreach ($result->Errors as $error) {
349
            if ($error->ErrorID != "7186919") { // no product found, not an error
350
                error_log($url);
351
                error_log("$error->ErrorText ($error->ErrorId)");
352
            }
353
        }
354
    }
41 - 355
 
65 - 356
    return $arr;
83 - 357
}