Subversion Repositories cheapmusic

Rev

Rev 21 | Rev 24 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 - 1
<?php
2
include_once('php/constants.php');
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
 
20 - 11
    $flatShipping = array (
12
        "4258829"=>"2.98",  // Barnes & Noble
13
        "129899"=>"3.00",   // Booksamillion.com
14
        "4911961"=>"3.24",  // FYE.com
15
        "4926671"=>"3.99",  // Secondspin.com
16
        "1111879"=>"2.99",  // ArkivMusic
17
        "5153127"=>"0.00",  // OnBuy.com
18
        "1595844"=>"0.00"   // Sheet Music Plus
19
    );
20
 
21
// API request variables
22
    $query = '+' . str_replace(" ", " +", $query); // enforce 'and' logic
23
	$safequery = urlencode($query);
24
	$numResults = $config['numResults'];
25
	$numResultsMid = $config['numResultsMid'];
26
	$resultsMid = [];
27
 
28
// Construct the findItemsByKeywords HTTP GET call
29
    $url = "https://product-search.api.cj.com/v2/product-search";
30
    $url .= "?website-id=" . $config['websiteId'];
31
 
32
    if (in_array($_SESSION["barcode"]["Type"], array("UPC","EAN","ISBN"))) {
33
        $url .= "&upc=";
34
    } else {
35
        $url .= "&keywords=";
36
    }
37
    $url .= $safequery;
38
 
39
    $url .= "&records-per-page=" . $numResults;
40
    $url .= "&page-number=1";
41
    $url .= "&sort-by=price";
42
    $url .= "&sort-order=asc";
43
    $url .= "&advertiser-ids=" . $config['advertiserIds'];
44
 
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']
51
        );
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);
60
    $result = curl_exec ($ch);
61
 
62
	$result = utf8_encode($result);
63
	$result = simplexml_load_string($result);
64
 
65
    curl_close($ch);
66
 
23 - 67
//echo "$url<pre>";print_r($result);echo "</pre>";
20 - 68
 
69
	$arr = [];
70
 
71
// Check to see if the request found any results
72
	if (isset($result->products['total-matched'])) {
73
    // If the response was loaded, parse it and store in array
74
		foreach ($result->{'products'}->{'product'} as $product) {
75
		    $barcode = (string)$product->{'upc'};
76
		    $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
77
 
21 - 78
            // approved: ArkivMusic(1111879), Secondspin(4926671), Sheet Music Plus(1595844)
20 - 79
            // pending: FYE(4911961)
21 - 80
            // declined: Barnes & Noble, Booksamillion, OnBuy.com
81
 
20 - 82
			$merchantId = strval($product->{'advertiser-id'});
83
			$merchantName = (string)$product->{'advertiser-name'};
84
 
85
			$title = (string)$product->{'name'};
86
			$pic = str_replace('http://', 'https://', (string)$product->{'image-url'});
87
			if (empty($pic)) {
21 - 88
			    switch ($merchantId) {
89
        			case 1111879: // ArkivMusic
90
        			    $pic = "images/no-image-available.jpg";
91
			            break;
92
 
93
			        default:
94
        			    continue;
95
			    }
20 - 96
			}
97
			$url = str_replace('http://', 'https://', (string)$product->{'buy-url'});
98
			if (empty($url)) {
99
			    continue;
100
			}
101
 
21 - 102
			switch ($merchantId) {
103
			    case 4911961: // FYE.com
104
    				$category = "CD";
105
    				if (preg_match('/\d\d\d\d$/', $title)) {
106
                        $type = 'USED';
107
    				    $condition = "Used";
108
    				    $title = rtrim(substr($title, 0, -4));
109
    				} else {
110
                        $type = 'NEW';
111
    				    $condition = "Brand New";
112
    				}
113
    				break;
114
 
115
   				case 4926671: // Secondspin.com
116
                    $type = 'USED';
117
   				    $condition = "Used";
118
    				$category = "CD";
119
    				break;
120
 
121
    			case 1595844: // Sheet Music Plus
122
                    $type = 'NEW';
123
   				    $condition = "Brand New";
124
    				$category = "Book";
125
    				break;
126
 
127
    			case 1111879: // ArkivMusic
128
                    $type = 'NEW';
129
   				    $condition = "Brand New";
130
    				$category = "CD";
131
    				break;
132
 
133
   				default:
134
                    $type = 'USED';
135
   				    $condition = "Used";
136
    				$category = "CD";
137
    				break;
20 - 138
			}
139
 
140
			$country = 'US';
141
			$bestOffer = false;
142
 
143
            $price = 0;
144
            if (!empty($product->{'price'}) && !empty($product->{'sale-price'})) {
145
    			$price = minNotNull(array($product->{'price'}, $product->{'sale-price'}));
146
    		} else if (!empty($product->{'price'})) {
147
    			$price = $product->{'price'};
148
    		} else if (!empty($product->{'sale-price'})) {
149
    			$price = $product->{'sale-price'};
150
    		}
151
			$price = number_format(floatval($price), 2, '.', '');
152
			if ($price <= "0.00") {
153
			    continue;
154
			}
155
 
156
			$currency = 'USD';
157
			$timeLeft = 0;
158
			$listingType = 'Fixed';
159
			$location = 'US';
160
			$zip = '';
161
			$feedbackScore = -1;
162
			$feedbackPercent = -1;
163
			$sellerName = '';
164
			$handlingTime = -1;
165
			$shippingCost = $flatShipping[$merchantId];
166
			$shippingCurrency = 'USD';
167
 
168
// bugbug
21 - 169
//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 - 170
 
171
            // this is last after all checks
172
			$resultsMid[] = $merchantId;
173
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
174
			    continue;
175
			}
176
 
177
			$arr[] = array(
178
				"Merchant" => $merchantName,
21 - 179
				"Type" => ($searchType == constant("NEW") ? 'New' : 'Used'),
20 - 180
				"Title" => "$title",
181
				"Barcode" => "$barcode",
182
				"BarcodeType" => "$barcodeType",
183
				"Image" => "$pic",
184
				"URL" => "$url",
185
				"Category" => "$category",
186
				"Condition" => "$condition",
187
				"Country" => "$country",
188
				"BestOffer" => "$bestOffer",
189
				"TimeLeft" => "$timeLeft",
190
				"Price" => "$price",
191
				"Currency" => "$currency",
192
				"ListingType" => "$listingType",
193
				"Location" => "$location",
194
				"Zip" => "$zip",
195
				"FeedbackScore" => "$feedbackScore",
196
				"FeedbackPercent" => "$feedbackPercent",
197
				"SellerName" => "$sellerName",
198
				"HandlingTime" => "$handlingTime",
199
				"ShippingCost" => "$shippingCost",
200
				"ShippingCurrency" => "$shippingCurrency",
201
				"Show" => true
202
			);
203
		}
204
	}
205
// If the response does not indicate 'Success,' log an error
206
	else {
207
    	error_log($url);
208
    	if (isset($result->{'error-message'})) {
209
    		error_log($result->{'error-message'});
210
    	}
211
	}
21 - 212
 
20 - 213
	return $arr;
214
}