Subversion Repositories cheapmusic

Rev

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