Subversion Repositories cheapmusic

Rev

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