Subversion Repositories cheapmusic

Rev

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