Subversion Repositories cheapmusic

Rev

Rev 39 | Rev 66 | 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
    $safequery = urlencode($query);
14
    $numResults = $config['numResults'];
15
    $numResultsMid = $config['numResultsMid'];
16
    $resultsMid = [];
20 - 17
 
65 - 18
    // Construct the findItemsByKeywords HTTP GET call
20 - 19
    $url = "https://product-search.api.cj.com/v2/product-search";
20
    $url .= "?website-id=" . $config['websiteId'];
21
 
65 - 22
    if (in_array($_SESSION["barcode"]["Type"], array(
23
        "UPC",
24
        "EAN",
25
        "ISBN"
26
    ))) {
20 - 27
        $url .= "&upc=";
65 - 28
    }
29
    else {
20 - 30
        $url .= "&keywords=";
31
    }
32
    $url .= $safequery;
33
 
34
    $url .= "&records-per-page=" . $numResults;
35
    $url .= "&page-number=1";
36
    $url .= "&sort-by=price";
37
    $url .= "&sort-order=asc";
38
    $url .= "&advertiser-ids=" . $config['advertiserIds'];
39
 
40
    $header = array(
41
        'Content-Type: application/x-www-form-urlencoded',
42
        'Accept: application/xml',
43
        'Accept-Language: en-US,en;q=0.5',
44
        'Accept-Charset: UTF-8,*;q=0.5',
45
        'Authorization: ' . $config['personalAccessToken']
65 - 46
    );
20 - 47
 
48
    $ch = curl_init();
49
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
50
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
51
    curl_setopt($ch, CURLOPT_URL, $url);
52
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
53
    curl_setopt($ch, CURLOPT_HEADER, 0);
54
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
65 - 55
    $result = curl_exec($ch);
20 - 56
 
38 - 57
    if (empty($result)) {
58
        return [];
59
    }
60
 
65 - 61
    $result = utf8_encode($result);
62
    $result = simplexml_load_string($result);
20 - 63
 
64
    curl_close($ch);
65
 
65 - 66
    //echo "$url<pre>";print_r($result);echo "</pre>";
67
    $arr = [];
20 - 68
 
65 - 69
    // Check to see if the request found any results
70
    if (isset($result->products['total-matched'])) {
71
        // If the response was loaded, parse it and store in array
72
        foreach ($result->{'products'}->{'product'} as $product) {
73
            $barcode = (string)$product->{'upc'};
74
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
20 - 75
 
21 - 76
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 77
            // pending: FYE(4911961)
21 - 78
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
65 - 79
            $merchantId = strval($product->{'advertiser-id'});
80
            $merchantName = (string)$product->{'advertiser-name'};
21 - 81
 
65 - 82
            $title = (string)$product->{'name'};
83
            $pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
84
            if (empty($pic)) {
85
                switch ($merchantId) {
86
                    case 1111879: // ArkivMusic
87
                        $pic = "images/no-image-available.jpg";
88
                    break;
20 - 89
 
65 - 90
                    default:
91
                        continue;
92
                }
93
            }
94
            $url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
95
            if (empty($url)) {
96
                continue;
97
            }
20 - 98
 
65 - 99
            switch ($merchantId) {
100
                case 4911961: // FYE.com
101
                    $category = "CD";
102
                    $freeShippingCap = 40.00;
103
                    $handlingTime = 1;
104
                    $shippingCost = 3.24;
105
                    $shippingCurrency = 'USD';
106
                    if (preg_match('/\d\d\d\d$/', $title)) {
24 - 107
                        $type = 'Used';
65 - 108
                        $condition = "Used";
109
                        $title = rtrim(substr($title, 0, -4));
110
                    }
111
                    else {
24 - 112
                        $type = 'New';
65 - 113
                        $condition = "Brand New";
114
                    }
115
                break;
116
 
117
                case 4926671: // Secondspin.com
118
                    $freeShippingCap = 0.00;
119
                    $handlingTime = 1;
120
                    $shippingCost = 3.99;
121
                    $shippingCurrency = 'USD';
24 - 122
                    $type = 'Used';
65 - 123
                    $condition = "Used";
124
                    $category = "CD";
125
                    $pic = str_replace("fye.com", "secondspin.com", $pic);
126
                    if (endsWith($pic, "nopic-disc-300.png")) {
127
                        $pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
128
                    }
129
                    $url = str_replace("fye.com", "secondspin.com", $url);
130
                break;
131
 
132
                case 1595844: // Sheet Music Plus
133
                    $freeShippingCap = 35.00;
134
                    $handlingTime = 1;
135
                    $shippingCost = 3.99;
136
                    $shippingCurrency = 'USD';
24 - 137
                    $type = 'New';
65 - 138
                    $condition = "Brand New";
139
                    $category = "Book";
140
                break;
141
 
142
                case 1111879: // ArkivMusic
143
                    $freeShippingCap = 0.00;
144
                    $handlingTime = 1;
145
                    $shippingCost = 2.99;
146
                    $shippingCurrency = 'USD';
24 - 147
                    $type = 'New';
65 - 148
                    $condition = "Brand New";
149
                    $category = "CD";
150
                break;
21 - 151
 
65 - 152
                default:
153
                    $freeShippingCap = 0.00;
154
                    $handlingTime = - 1;
155
                    $shippingCost = 0.00;
156
                    $shippingCurrency = 'USD';
24 - 157
                    $type = 'Used';
65 - 158
                    $condition = "Used";
159
                    $category = "CD";
160
                break;
161
            }
20 - 162
 
65 - 163
            $country = 'US';
164
            $bestOffer = false;
20 - 165
 
166
            $price = 0;
167
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
65 - 168
                $price = minNotNull(array(
169
                    $product->{'price'},
170
                    $product->{'sale-price'}
171
                ));
172
            }
173
            else if (!empty($product->{'price'})) {
174
                $price = $product->{'price'};
175
            }
176
            else if (!empty($product->{'sale-price'})) {
177
                $price = $product->{'sale-price'};
178
            }
179
            $price = number_format(floatval($price) , 2, '.', '');
180
            if ($price <= "0.00") {
181
                continue;
182
            }
20 - 183
 
65 - 184
            $currency = 'USD';
185
            $timeLeft = 0;
186
            $listingType = 'Fixed';
187
            $location = 'US';
188
            $zip = '';
189
            $feedbackScore = - 1;
190
            $feedbackPercent = - 1;
191
            $sellerName = '';
20 - 192
 
65 - 193
            // bugbug
194
            //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 - 195
            // this is last after all checks
65 - 196
            $resultsMid[] = $merchantId;
197
            if (array_count_values($resultsMid) [$merchantId] > $numResultsMid) {
198
                continue;
199
            }
20 - 200
 
65 - 201
            $arr[] = array(
202
                "Merchant" => $merchantName,
203
                "Type" => "$type",
204
                "Title" => "$title",
205
                "Barcode" => "$barcode",
206
                "BarcodeType" => "$barcodeType",
207
                "Image" => "$pic",
208
                "URL" => "$url",
209
                "Category" => "$category",
210
                "Condition" => "$condition",
211
                "Country" => "$country",
212
                "BestOffer" => "$bestOffer",
213
                "TimeLeft" => "$timeLeft",
214
                "Price" => "$price",
215
                "Currency" => "$currency",
216
                "ListingType" => "$listingType",
217
                "Location" => "$location",
218
                "Zip" => "$zip",
219
                "FeedbackScore" => "$feedbackScore",
220
                "FeedbackPercent" => "$feedbackPercent",
221
                "SellerName" => "$sellerName",
222
                "HandlingTime" => "$handlingTime",
223
                "ShippingCost" => "$shippingCost",
224
                "ShippingCurrency" => "$shippingCurrency",
225
                "FreeShippingCap" => "$freeShippingCap",
226
                "Show" => true
227
            );
228
        }
229
    }
230
    // If the response does not indicate 'Success,' log an error
231
    else {
232
        error_log($url);
233
        if (isset($result->{'error-message'})) {
234
            error_log($result->{'error-message'});
235
        }
236
    }
21 - 237
 
65 - 238
    return $arr;
20 - 239
}