Subversion Repositories cheapmusic

Rev

Rev 89 | Rev 96 | 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
 
6
// Get CJ Affiliate listings
7
function get_cjaffiliate($query, $searchType) {
8
    $vendors = Vendors::getInstance();
9
    $config = $vendors->getVendor(Vendors::CJAFFILIATE);
21 - 10
 
65 - 11
    // API request variables
20 - 12
    $query = '+' . str_replace(" ", " +", $query); // enforce 'and' logic
65 - 13
    $numResults = $config['numResults'];
14
    $numResultsMid = $config['numResultsMid'];
15
    $resultsMid = [];
20 - 16
 
81 - 17
    $params = [];
18
    $params["website-id"] = $config['websiteId'];
20 - 19
 
65 - 20
    if (in_array($_SESSION["barcode"]["Type"], array(
21
        "UPC",
22
        "EAN",
23
        "ISBN"
24
    ))) {
81 - 25
        $params["upc"] = $query;
65 - 26
    }
27
    else {
81 - 28
        $params["keywords"] = $query;
20 - 29
    }
30
 
81 - 31
    $params["records-per-page"] = $numResults;
32
    $params["page-number"] = "1";
33
    $params["sort-by"] = "price";
34
    $params["sort-order"] = "asc";
35
    $params["advertiser-ids"] = $config['advertiserIds'];
20 - 36
 
81 - 37
    $pairs = array();
38
    foreach ($params as $key => $value) {
39
        array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
40
    }
41
    $canonical_query_string = join("&", $pairs);
42
 
43
    $url = "https://product-search.api.cj.com/v2/product-search?" . $canonical_query_string;
44
 
20 - 45
    $header = array(
46
        'Content-Type: application/x-www-form-urlencoded',
47
        'Accept: application/xml',
48
        'Accept-Language: en-US,en;q=0.5',
49
        'Accept-Charset: UTF-8,*;q=0.5',
50
        'Authorization: ' . $config['personalAccessToken']
65 - 51
    );
20 - 52
 
53
    $ch = curl_init();
54
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
55
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
56
    curl_setopt($ch, CURLOPT_URL, $url);
57
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
58
    curl_setopt($ch, CURLOPT_HEADER, 0);
89 - 59
    curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
60
    curl_setopt($ch, CURLOPT_TIMEOUT, 15);
20 - 61
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 62
    $result = curl_exec($ch);
20 - 63
 
38 - 64
    if (empty($result)) {
65
        return [];
66
    }
67
 
65 - 68
    $result = utf8_encode($result);
69
    $result = simplexml_load_string($result);
20 - 70
 
71
    curl_close($ch);
72
 
65 - 73
    //echo "$url<pre>";print_r($result);echo "</pre>";
74
    $arr = [];
20 - 75
 
65 - 76
    // Check to see if the request found any results
77
    if (isset($result->products['total-matched'])) {
78
        // If the response was loaded, parse it and store in array
79
        foreach ($result->{'products'}->{'product'} as $product) {
80
            $barcode = (string)$product->{'upc'};
81
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
20 - 82
 
21 - 83
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 84
            // pending: FYE(4911961)
21 - 85
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
65 - 86
            $merchantId = strval($product->{'advertiser-id'});
87
            $merchantName = (string)$product->{'advertiser-name'};
21 - 88
 
65 - 89
            $title = (string)$product->{'name'};
90
            $pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
91
            if (empty($pic)) {
92
                switch ($merchantId) {
93
                    case 1111879: // ArkivMusic
94
                        $pic = "images/no-image-available.jpg";
95
                    break;
20 - 96
 
65 - 97
                    default:
94 - 98
                        continue 2;
65 - 99
                }
100
            }
101
            $url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
102
            if (empty($url)) {
103
                continue;
104
            }
20 - 105
 
65 - 106
            switch ($merchantId) {
107
                case 4911961: // FYE.com
66 - 108
                    $merchantName = "FYE";
109
                    $mediaType = "CD";
65 - 110
                    $freeShippingCap = 40.00;
111
                    $handlingTime = 1;
112
                    $shippingCost = 3.24;
81 - 113
                    $shippingEstimated = true;
65 - 114
                    $shippingCurrency = 'USD';
115
                    if (preg_match('/\d\d\d\d$/', $title)) {
66 - 116
                        $condition = 'Used';
117
                        $detailCondition = "Used";
65 - 118
                        $title = rtrim(substr($title, 0, -4));
119
                    }
120
                    else {
66 - 121
                        $condition = 'New';
122
                        $detailCondition = "Brand New";
65 - 123
                    }
124
                break;
125
 
126
                case 4926671: // Secondspin.com
66 - 127
                    $merchantName = "Secondspin";
65 - 128
                    $freeShippingCap = 0.00;
129
                    $handlingTime = 1;
130
                    $shippingCost = 3.99;
81 - 131
                    $shippingEstimated = true;
65 - 132
                    $shippingCurrency = 'USD';
66 - 133
                    $condition = 'Used';
134
                    $detailCondition = "Used";
135
                    $mediaType = "CD";
65 - 136
                    $pic = str_replace("fye.com", "secondspin.com", $pic);
137
                    if (endsWith($pic, "nopic-disc-300.png")) {
138
                        $pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
139
                    }
140
                    $url = str_replace("fye.com", "secondspin.com", $url);
141
                break;
142
 
143
                case 1595844: // Sheet Music Plus
144
                    $freeShippingCap = 35.00;
145
                    $handlingTime = 1;
146
                    $shippingCost = 3.99;
81 - 147
                    $shippingEstimated = true;
65 - 148
                    $shippingCurrency = 'USD';
66 - 149
                    $condition = 'New';
150
                    $detailCondition = "Brand New";
151
                    $mediaType = "Book";
65 - 152
                break;
153
 
154
                case 1111879: // ArkivMusic
155
                    $freeShippingCap = 0.00;
156
                    $handlingTime = 1;
157
                    $shippingCost = 2.99;
81 - 158
                    $shippingEstimated = true;
65 - 159
                    $shippingCurrency = 'USD';
66 - 160
                    $condition = 'New';
161
                    $detailCondition = "Brand New";
162
                    $mediaType = "CD";
65 - 163
                break;
21 - 164
 
65 - 165
                default:
166
                    $freeShippingCap = 0.00;
167
                    $handlingTime = - 1;
168
                    $shippingCost = 0.00;
81 - 169
                    $shippingEstimated = true;
65 - 170
                    $shippingCurrency = 'USD';
66 - 171
                    $condition = 'Used';
172
                    $detailCondition = "Used";
173
                    $mediaType = "CD";
65 - 174
                break;
175
            }
20 - 176
 
65 - 177
            $country = 'US';
178
            $bestOffer = false;
20 - 179
 
180
            $price = 0;
181
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
65 - 182
                $price = minNotNull(array(
183
                    $product->{'price'},
184
                    $product->{'sale-price'}
185
                ));
186
            }
187
            else if (!empty($product->{'price'})) {
188
                $price = $product->{'price'};
189
            }
190
            else if (!empty($product->{'sale-price'})) {
191
                $price = $product->{'sale-price'};
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((string)$product->{'advertiser-name'},(string)$product->{'advertiser-category'},(string)$product->{'name'},(string)$product->{'description'},"'".$barcode,$barcodeType,minNotNull(array($product->{'price'},(string)$product->{'sale-price'})),(string)$product->{'buy-url'}));
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,
238
                    "Show" => true
239
                );
65 - 240
            }
241
        }
242
    }
243
    // If the response does not indicate 'Success,' log an error
244
    else {
245
        error_log($url);
246
        if (isset($result->{'error-message'})) {
247
            error_log($result->{'error-message'});
248
        }
249
    }
21 - 250
 
65 - 251
    return $arr;
20 - 252
}