Subversion Repositories cheapmusic

Rev

Rev 137 | 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 = [];
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();
53
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
54
        curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
55
        curl_setopt($ch, CURLOPT_URL, $url);
56
        curl_setopt($ch, CURLOPT_AUTOREFERER, true);
57
        curl_setopt($ch, CURLOPT_HEADER, 0);
58
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
59
        curl_setopt($ch, CURLOPT_TIMEOUT, 15);
60
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
61
        $result = curl_exec($ch);
62
        curl_close($ch);
63
 
64
        saveSearchCache("cjaffiliate", $query, $searchType, $result);
65
    }
20 - 66
 
38 - 67
    if (empty($result)) {
68
        return [];
69
    }
70
 
65 - 71
    $result = utf8_encode($result);
72
    $result = simplexml_load_string($result);
20 - 73
 
65 - 74
    //echo "$url<pre>";print_r($result);echo "</pre>";
75
    $arr = [];
20 - 76
 
65 - 77
    // Check to see if the request found any results
78
    if (isset($result->products['total-matched'])) {
79
        // If the response was loaded, parse it and store in array
80
        foreach ($result->{'products'}->{'product'} as $product) {
81
            $barcode = (string)$product->{'upc'};
82
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
20 - 83
 
21 - 84
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 85
            // pending: FYE(4911961)
21 - 86
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
65 - 87
            $merchantId = strval($product->{'advertiser-id'});
88
            $merchantName = (string)$product->{'advertiser-name'};
21 - 89
 
65 - 90
            $title = (string)$product->{'name'};
91
            $pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
92
            if (empty($pic)) {
93
                switch ($merchantId) {
94
                    case 1111879: // ArkivMusic
95
                        $pic = "images/no-image-available.jpg";
96
                    break;
20 - 97
 
65 - 98
                    default:
94 - 99
                        continue 2;
65 - 100
                }
101
            }
102
            $url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
103
            if (empty($url)) {
104
                continue;
105
            }
20 - 106
 
65 - 107
            switch ($merchantId) {
108
                case 4911961: // FYE.com
66 - 109
                    $merchantName = "FYE";
110
                    $mediaType = "CD";
65 - 111
                    $freeShippingCap = 40.00;
112
                    $handlingTime = 1;
113
                    $shippingCost = 3.24;
81 - 114
                    $shippingEstimated = true;
65 - 115
                    $shippingCurrency = 'USD';
116
                    if (preg_match('/\d\d\d\d$/', $title)) {
66 - 117
                        $condition = 'Used';
118
                        $detailCondition = "Used";
65 - 119
                        $title = rtrim(substr($title, 0, -4));
120
                    }
121
                    else {
66 - 122
                        $condition = 'New';
123
                        $detailCondition = "Brand New";
65 - 124
                    }
125
                break;
126
 
127
                case 4926671: // Secondspin.com
66 - 128
                    $merchantName = "Secondspin";
65 - 129
                    $freeShippingCap = 0.00;
130
                    $handlingTime = 1;
131
                    $shippingCost = 3.99;
81 - 132
                    $shippingEstimated = true;
65 - 133
                    $shippingCurrency = 'USD';
66 - 134
                    $condition = 'Used';
135
                    $detailCondition = "Used";
136
                    $mediaType = "CD";
65 - 137
                    $pic = str_replace("fye.com", "secondspin.com", $pic);
138
                    if (endsWith($pic, "nopic-disc-300.png")) {
139
                        $pic = "https://www.secondspin.com/stores/ss/images/nopic-150.png";
140
                    }
141
                    $url = str_replace("fye.com", "secondspin.com", $url);
142
                break;
143
 
144
                case 1595844: // Sheet Music Plus
145
                    $freeShippingCap = 35.00;
146
                    $handlingTime = 1;
147
                    $shippingCost = 3.99;
81 - 148
                    $shippingEstimated = true;
65 - 149
                    $shippingCurrency = 'USD';
66 - 150
                    $condition = 'New';
151
                    $detailCondition = "Brand New";
152
                    $mediaType = "Book";
65 - 153
                break;
154
 
155
                case 1111879: // ArkivMusic
156
                    $freeShippingCap = 0.00;
157
                    $handlingTime = 1;
158
                    $shippingCost = 2.99;
81 - 159
                    $shippingEstimated = true;
65 - 160
                    $shippingCurrency = 'USD';
66 - 161
                    $condition = 'New';
162
                    $detailCondition = "Brand New";
163
                    $mediaType = "CD";
65 - 164
                break;
21 - 165
 
65 - 166
                default:
167
                    $freeShippingCap = 0.00;
168
                    $handlingTime = - 1;
169
                    $shippingCost = 0.00;
81 - 170
                    $shippingEstimated = true;
65 - 171
                    $shippingCurrency = 'USD';
66 - 172
                    $condition = 'Used';
173
                    $detailCondition = "Used";
174
                    $mediaType = "CD";
65 - 175
                break;
176
            }
20 - 177
 
65 - 178
            $country = 'US';
179
            $bestOffer = false;
20 - 180
 
181
            $price = 0;
182
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
65 - 183
                $price = minNotNull(array(
184
                    $product->{'price'},
185
                    $product->{'sale-price'}
186
                ));
187
            }
188
            else if (!empty($product->{'price'})) {
189
                $price = $product->{'price'};
190
            }
191
            else if (!empty($product->{'sale-price'})) {
192
                $price = $product->{'sale-price'};
193
            }
194
            $price = number_format(floatval($price) , 2, '.', '');
195
            if ($price <= "0.00") {
196
                continue;
197
            }
20 - 198
 
65 - 199
            $currency = 'USD';
200
            $timeLeft = 0;
201
            $listingType = 'Fixed';
202
            $location = 'US';
203
            $zip = '';
204
            $feedbackScore = - 1;
205
            $feedbackPercent = - 1;
206
            $sellerName = '';
20 - 207
 
65 - 208
            // bugbug
209
            //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 - 210
            // this is last after all checks
65 - 211
            $resultsMid[] = $merchantId;
94 - 212
            if ($numResultsMid > array_count_values($resultsMid) [$merchantId]) {
213
                $arr[] = array(
214
                    "Merchant" => $merchantName,
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,
143 - 239
                    "Show" => true,
240
                    "Details" => ""
94 - 241
                );
65 - 242
            }
243
        }
244
    }
245
    // If the response does not indicate 'Success,' log an error
246
    else {
96 - 247
        my_error_log($url);
65 - 248
        if (isset($result->{'error-message'})) {
96 - 249
            my_error_log($result->{'error-message'});
65 - 250
        }
251
    }
21 - 252
 
65 - 253
    return $arr;
20 - 254
}