Subversion Repositories cheapmusic

Rev

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