Subversion Repositories cheapmusic

Rev

Rev 65 | Rev 81 | 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
66 - 101
                    $merchantName = "FYE";
102
                    $mediaType = "CD";
65 - 103
                    $freeShippingCap = 40.00;
104
                    $handlingTime = 1;
105
                    $shippingCost = 3.24;
106
                    $shippingCurrency = 'USD';
107
                    if (preg_match('/\d\d\d\d$/', $title)) {
66 - 108
                        $condition = 'Used';
109
                        $detailCondition = "Used";
65 - 110
                        $title = rtrim(substr($title, 0, -4));
111
                    }
112
                    else {
66 - 113
                        $condition = 'New';
114
                        $detailCondition = "Brand New";
65 - 115
                    }
116
                break;
117
 
118
                case 4926671: // Secondspin.com
66 - 119
                    $merchantName = "Secondspin";
65 - 120
                    $freeShippingCap = 0.00;
121
                    $handlingTime = 1;
122
                    $shippingCost = 3.99;
123
                    $shippingCurrency = 'USD';
66 - 124
                    $condition = 'Used';
125
                    $detailCondition = "Used";
126
                    $mediaType = "CD";
65 - 127
                    $pic = str_replace("fye.com", "secondspin.com", $pic);
128
                    if (endsWith($pic, "nopic-disc-300.png")) {
129
                        $pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
130
                    }
131
                    $url = str_replace("fye.com", "secondspin.com", $url);
132
                break;
133
 
134
                case 1595844: // Sheet Music Plus
135
                    $freeShippingCap = 35.00;
136
                    $handlingTime = 1;
137
                    $shippingCost = 3.99;
138
                    $shippingCurrency = 'USD';
66 - 139
                    $condition = 'New';
140
                    $detailCondition = "Brand New";
141
                    $mediaType = "Book";
65 - 142
                break;
143
 
144
                case 1111879: // ArkivMusic
145
                    $freeShippingCap = 0.00;
146
                    $handlingTime = 1;
147
                    $shippingCost = 2.99;
148
                    $shippingCurrency = 'USD';
66 - 149
                    $condition = 'New';
150
                    $detailCondition = "Brand New";
151
                    $mediaType = "CD";
65 - 152
                break;
21 - 153
 
65 - 154
                default:
155
                    $freeShippingCap = 0.00;
156
                    $handlingTime = - 1;
157
                    $shippingCost = 0.00;
158
                    $shippingCurrency = 'USD';
66 - 159
                    $condition = 'Used';
160
                    $detailCondition = "Used";
161
                    $mediaType = "CD";
65 - 162
                break;
163
            }
20 - 164
 
65 - 165
            $country = 'US';
166
            $bestOffer = false;
20 - 167
 
168
            $price = 0;
169
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
65 - 170
                $price = minNotNull(array(
171
                    $product->{'price'},
172
                    $product->{'sale-price'}
173
                ));
174
            }
175
            else if (!empty($product->{'price'})) {
176
                $price = $product->{'price'};
177
            }
178
            else if (!empty($product->{'sale-price'})) {
179
                $price = $product->{'sale-price'};
180
            }
181
            $price = number_format(floatval($price) , 2, '.', '');
182
            if ($price <= "0.00") {
183
                continue;
184
            }
20 - 185
 
65 - 186
            $currency = 'USD';
187
            $timeLeft = 0;
188
            $listingType = 'Fixed';
189
            $location = 'US';
190
            $zip = '';
191
            $feedbackScore = - 1;
192
            $feedbackPercent = - 1;
193
            $sellerName = '';
20 - 194
 
65 - 195
            // bugbug
196
            //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 - 197
            // this is last after all checks
65 - 198
            $resultsMid[] = $merchantId;
199
            if (array_count_values($resultsMid) [$merchantId] > $numResultsMid) {
200
                continue;
201
            }
20 - 202
 
65 - 203
            $arr[] = array(
204
                "Merchant" => $merchantName,
66 - 205
                "Condition" => "$condition",
65 - 206
                "Title" => "$title",
207
                "Barcode" => "$barcode",
208
                "BarcodeType" => "$barcodeType",
209
                "Image" => "$pic",
210
                "URL" => "$url",
66 - 211
                "MediaType" => "$mediaType",
212
                "DetailCondition" => "$detailCondition",
65 - 213
                "Country" => "$country",
214
                "BestOffer" => "$bestOffer",
215
                "TimeLeft" => "$timeLeft",
216
                "Price" => "$price",
217
                "Currency" => "$currency",
218
                "ListingType" => "$listingType",
219
                "Location" => "$location",
220
                "Zip" => "$zip",
221
                "FeedbackScore" => "$feedbackScore",
222
                "FeedbackPercent" => "$feedbackPercent",
223
                "SellerName" => "$sellerName",
224
                "HandlingTime" => "$handlingTime",
225
                "ShippingCost" => "$shippingCost",
226
                "ShippingCurrency" => "$shippingCurrency",
227
                "FreeShippingCap" => "$freeShippingCap",
228
                "Show" => true
229
            );
230
        }
231
    }
232
    // If the response does not indicate 'Success,' log an error
233
    else {
234
        error_log($url);
235
        if (isset($result->{'error-message'})) {
236
            error_log($result->{'error-message'});
237
        }
238
    }
21 - 239
 
65 - 240
    return $arr;
20 - 241
}