Subversion Repositories cheapmusic

Rev

Rev 66 | Rev 89 | 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);
59
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 60
    $result = curl_exec($ch);
20 - 61
 
38 - 62
    if (empty($result)) {
63
        return [];
64
    }
65
 
65 - 66
    $result = utf8_encode($result);
67
    $result = simplexml_load_string($result);
20 - 68
 
69
    curl_close($ch);
70
 
65 - 71
    //echo "$url<pre>";print_r($result);echo "</pre>";
72
    $arr = [];
20 - 73
 
65 - 74
    // Check to see if the request found any results
75
    if (isset($result->products['total-matched'])) {
76
        // If the response was loaded, parse it and store in array
77
        foreach ($result->{'products'}->{'product'} as $product) {
78
            $barcode = (string)$product->{'upc'};
79
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
20 - 80
 
21 - 81
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 82
            // pending: FYE(4911961)
21 - 83
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
65 - 84
            $merchantId = strval($product->{'advertiser-id'});
85
            $merchantName = (string)$product->{'advertiser-name'};
21 - 86
 
65 - 87
            $title = (string)$product->{'name'};
88
            $pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
89
            if (empty($pic)) {
90
                switch ($merchantId) {
91
                    case 1111879: // ArkivMusic
92
                        $pic = "images/no-image-available.jpg";
93
                    break;
20 - 94
 
65 - 95
                    default:
96
                        continue;
97
                }
98
            }
99
            $url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
100
            if (empty($url)) {
101
                continue;
102
            }
20 - 103
 
65 - 104
            switch ($merchantId) {
105
                case 4911961: // FYE.com
66 - 106
                    $merchantName = "FYE";
107
                    $mediaType = "CD";
65 - 108
                    $freeShippingCap = 40.00;
109
                    $handlingTime = 1;
110
                    $shippingCost = 3.24;
81 - 111
                    $shippingEstimated = true;
65 - 112
                    $shippingCurrency = 'USD';
113
                    if (preg_match('/\d\d\d\d$/', $title)) {
66 - 114
                        $condition = 'Used';
115
                        $detailCondition = "Used";
65 - 116
                        $title = rtrim(substr($title, 0, -4));
117
                    }
118
                    else {
66 - 119
                        $condition = 'New';
120
                        $detailCondition = "Brand New";
65 - 121
                    }
122
                break;
123
 
124
                case 4926671: // Secondspin.com
66 - 125
                    $merchantName = "Secondspin";
65 - 126
                    $freeShippingCap = 0.00;
127
                    $handlingTime = 1;
128
                    $shippingCost = 3.99;
81 - 129
                    $shippingEstimated = true;
65 - 130
                    $shippingCurrency = 'USD';
66 - 131
                    $condition = 'Used';
132
                    $detailCondition = "Used";
133
                    $mediaType = "CD";
65 - 134
                    $pic = str_replace("fye.com", "secondspin.com", $pic);
135
                    if (endsWith($pic, "nopic-disc-300.png")) {
136
                        $pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
137
                    }
138
                    $url = str_replace("fye.com", "secondspin.com", $url);
139
                break;
140
 
141
                case 1595844: // Sheet Music Plus
142
                    $freeShippingCap = 35.00;
143
                    $handlingTime = 1;
144
                    $shippingCost = 3.99;
81 - 145
                    $shippingEstimated = true;
65 - 146
                    $shippingCurrency = 'USD';
66 - 147
                    $condition = 'New';
148
                    $detailCondition = "Brand New";
149
                    $mediaType = "Book";
65 - 150
                break;
151
 
152
                case 1111879: // ArkivMusic
153
                    $freeShippingCap = 0.00;
154
                    $handlingTime = 1;
155
                    $shippingCost = 2.99;
81 - 156
                    $shippingEstimated = true;
65 - 157
                    $shippingCurrency = 'USD';
66 - 158
                    $condition = 'New';
159
                    $detailCondition = "Brand New";
160
                    $mediaType = "CD";
65 - 161
                break;
21 - 162
 
65 - 163
                default:
164
                    $freeShippingCap = 0.00;
165
                    $handlingTime = - 1;
166
                    $shippingCost = 0.00;
81 - 167
                    $shippingEstimated = true;
65 - 168
                    $shippingCurrency = 'USD';
66 - 169
                    $condition = 'Used';
170
                    $detailCondition = "Used";
171
                    $mediaType = "CD";
65 - 172
                break;
173
            }
20 - 174
 
65 - 175
            $country = 'US';
176
            $bestOffer = false;
20 - 177
 
178
            $price = 0;
179
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
65 - 180
                $price = minNotNull(array(
181
                    $product->{'price'},
182
                    $product->{'sale-price'}
183
                ));
184
            }
185
            else if (!empty($product->{'price'})) {
186
                $price = $product->{'price'};
187
            }
188
            else if (!empty($product->{'sale-price'})) {
189
                $price = $product->{'sale-price'};
190
            }
191
            $price = number_format(floatval($price) , 2, '.', '');
192
            if ($price <= "0.00") {
193
                continue;
194
            }
20 - 195
 
65 - 196
            $currency = 'USD';
197
            $timeLeft = 0;
198
            $listingType = 'Fixed';
199
            $location = 'US';
200
            $zip = '';
201
            $feedbackScore = - 1;
202
            $feedbackPercent = - 1;
203
            $sellerName = '';
20 - 204
 
65 - 205
            // bugbug
206
            //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 - 207
            // this is last after all checks
65 - 208
            $resultsMid[] = $merchantId;
209
            if (array_count_values($resultsMid) [$merchantId] > $numResultsMid) {
210
                continue;
211
            }
20 - 212
 
65 - 213
            $arr[] = array(
214
                "Merchant" => $merchantName,
81 - 215
                "Condition" => $condition,
216
                "Title" => $title,
217
                "Barcode" => $barcode,
218
                "BarcodeType" => $barcodeType,
219
                "Image" => $pic,
220
                "URL" => $url,
221
                "MediaType" => $mediaType,
222
                "DetailCondition" => $detailCondition,
223
                "Country" => $country,
224
                "BestOffer" => $bestOffer,
225
                "TimeLeft" => $timeLeft,
226
                "Price" => $price,
227
                "Currency" => $currency,
228
                "ListingType" => $listingType,
229
                "Location" => $location,
230
                "Zip" => $zip,
231
                "FeedbackScore" => $feedbackScore,
232
                "FeedbackPercent" => $feedbackPercent,
233
                "SellerName" => $sellerName,
234
                "HandlingTime" => $handlingTime,
235
                "ShippingCost" => $shippingCost,
236
                "ShippingEstimated" => $shippingEstimated,
237
                "ShippingCurrency" => $shippingCurrency,
238
                "FreeShippingCap" => $freeShippingCap,
65 - 239
                "Show" => true
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
}