Subversion Repositories cheapmusic

Rev

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