Subversion Repositories cheapmusic

Rev

Rev 129 | Rev 143 | 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,
235
                    "Show" => true
236
                );
65 - 237
            }
238
        }
239
    }
240
    // If the response does not indicate 'Success,' log the error(s)
241
    else {
73 - 242
        if (!empty($result->Errors)) {
243
            foreach ($result->Errors as $error) {
244
                if ($error->ErrorID != "7186919") { // no product found, not an error
96 - 245
                    my_error_log($url);
246
                    my_error_log("$error->ErrorText ($error->ErrorId)");
73 - 247
                }
65 - 248
            }
73 - 249
        } else {
96 - 250
            my_error_log($url);
251
            my_error_log("No result or error message.");
65 - 252
        }
253
    }
57 - 254
 
65 - 255
    return $arr;
20 - 256
}
257
 
258
// Get Linkshare Bearer Token
65 - 259
function getLinkshareToken($config) {
20 - 260
    static $expiration = 0;
261
    static $accessToken = '';
262
    static $refreshToken = '';
263
 
264
    if ($expiration == 0 || time() > $expiration) {
65 - 265
        $url = "https://api.rakutenmarketing.com/token";
20 - 266
        $postdata = "grant_type=password&username=" . $config['user'] . "&password=" . $config['password'] . "&scope=" . $config['scope'];
267
        $header = array(
268
            'Content-Type: application/x-www-form-urlencoded',
269
            'Authorization: ' . $config['authorizationToken']
65 - 270
        );
20 - 271
 
272
        $ch = curl_init();
273
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
274
        curl_setopt($ch, CURLOPT_URL, $url);
65 - 275
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
89 - 276
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
277
	curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
20 - 278
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
279
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
280
        curl_setopt($ch, CURLOPT_REFERER, $url);
281
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
282
        curl_setopt($ch, CURLOPT_POST, 1);
65 - 283
        $result = curl_exec($ch);
53 - 284
        $result = json_decode($result);
20 - 285
        curl_close($ch);
286
 
38 - 287
        if (empty($result)) {
288
            return false;
289
        }
290
 
20 - 291
        $expiration = time() + $result->{'expires_in'};
292
        $accessToken = $result->{'access_token'};
293
        $refreshToken = $result->{'refresh_token'};
65 - 294
        $tokenType = $result->{'token_type'};
20 - 295
    }
296
 
38 - 297
    return $accessToken;
20 - 298
}
41 - 299
 
300
// get linkshare coupon codes
65 - 301
function get_linkshareCoupons() {
41 - 302
    $vendors = Vendors::getInstance();
303
    $config = $vendors->getVendor(Vendors::LINKSHARE);
304
 
305
    $token = getLinkshareToken($config);
306
    if ($token === false) {
307
        return [];
308
    }
309
 
65 - 310
    // Construct the findItemsByKeywords HTTP GET call
41 - 311
    $url = "https://api.rakutenmarketing.com/coupon/1.0?category=22&network=1&resultsperpage=100&pagenumber=1";
312
 
313
    $header = array(
314
        'Content-Type: application/x-www-form-urlencoded',
315
        'Accept: application/xml',
316
        'Accept-Language: en-US,en;q=0.5',
317
        'Accept-Charset: UTF-8,*;q=0.5',
318
        'Authorization: Bearer ' . $token
65 - 319
    );
41 - 320
 
321
    $ch = curl_init();
322
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
323
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
324
    curl_setopt($ch, CURLOPT_URL, $url);
325
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
326
    curl_setopt($ch, CURLOPT_HEADER, 0);
89 - 327
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
328
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
41 - 329
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 330
    $result = curl_exec($ch);
41 - 331
 
65 - 332
    $result = utf8_encode($result);
333
    $result = simplexml_load_string($result);
41 - 334
 
335
    curl_close($ch);
336
 
65 - 337
    //echo "$url<br><pre>";print_r($result);echo "</pre>";
338
    $arr = [];
41 - 339
 
65 - 340
    // Check to see if the request found any results
341
    if (isset($result->TotalMatches)) {
342
        // If the response was loaded, parse it and store in array
343
        foreach ($result->link as $link) {
344
            echo "NULL";
345
            echo "," . $link->advertisername;
346
            echo "," . $link->offerstartdate;
347
            echo "," . $link->offerenddate;
348
            echo ",\"" . $link->offerdescription . "\"";
349
            echo "," . $link->couponcode;
350
            echo "," . str_replace('http://', 'https://', (string)$link->clickurl);
351
            echo "," . str_replace('http://', 'https://', (string)$link->impressionpixel);
352
            echo "<br>";
353
        }
354
    }
355
    // If the response does not indicate 'Success,' log the error(s)
356
    else {
357
        foreach ($result->Errors as $error) {
358
            if ($error->ErrorID != "7186919") { // no product found, not an error
96 - 359
                my_error_log($url);
360
                my_error_log("$error->ErrorText ($error->ErrorId)");
65 - 361
            }
362
        }
363
    }
41 - 364
 
65 - 365
    return $arr;
83 - 366
}