Subversion Repositories cheapmusic

Rev

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