Subversion Repositories cheapmusic

Rev

Rev 23 | Rev 38 | 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
// Get linkshare listings
6
function get_linkshare($query, $searchType) {
7
    $vendors = Vendors::getInstance();
8
    $config = $vendors->getVendor(Vendors::LINKSHARE);
9
 
10
    $token = getLinkshareToken($config);
11
 
12
    $arr = [];
13
    foreach($config["advertiser"] as $advertiser) {
14
        $arrTemp = get_linkshareAdvertiser($config, $token, $query, $searchType, $advertiser);
15
        $arr = array_merge($arr, $arrTemp);
16
    }
17
 
18
    return($arr);
19
}
20
 
21
// Get linkshare listings by advertiser id
22
function get_linkshareAdvertiser($config, $token, $query, $searchType, $advertiserId) {
23
// API request variables
24
	$safequery = urlencode($query);
25
	$numResults = $config['numResults'];
26
	$numResultsMid = $config['numResultsMid'];
27
	$resultsMid = [];
28
 
29
// Construct the findItemsByKeywords HTTP GET call
30
    $url = "https://api.rakutenmarketing.com/productsearch/1.0";
31
    $url .= "?keyword=" . $safequery;
32
    $url .= "&max=" . $numResults;
33
    $url .= "&pagenumber=1";
34
    $url .= "&sort=retailprice";
35
    $url .= "&sorttype=asc";
36
    $url .= "&mid=" . $advertiserId;
37
 
38
    $header = array(
39
        'Content-Type: application/x-www-form-urlencoded',
40
        'Accept: application/xml',
41
        'Accept-Language: en-US,en;q=0.5',
42
        'Accept-Charset: UTF-8,*;q=0.5',
43
        'Authorization: Bearer ' . $token
44
        );
45
 
46
    $ch = curl_init();
47
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
48
    curl_setopt($ch, CURLOPT_ENCODING, "gzip,deflate");
49
    curl_setopt($ch, CURLOPT_URL, $url);
50
    curl_setopt($ch, CURLOPT_AUTOREFERER, true);
51
    curl_setopt($ch, CURLOPT_HEADER, 0);
52
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
53
    $result = curl_exec ($ch);
54
 
55
	$result = utf8_encode($result);
56
	$result = simplexml_load_string($result);
57
 
58
    curl_close($ch);
59
 
23 - 60
//echo "$url<br><pre>";print_r($result);echo "</pre>";
21 - 61
 
20 - 62
	$arr = [];
63
 
64
// Check to see if the request found any results
65
	if (isset($result->TotalMatches)) {
66
  // If the response was loaded, parse it and store in array
67
		foreach ($result->item as $item) {
68
			$merchantId = strval($item->mid);
69
			$merchantName = (string)$item->merchantname;
70
			$barcode = (string)$item->upccode;
71
			$barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
72
 
73
			$title = (string)$item->productname;
74
			$pic = str_replace('http://', 'https://', (string)$item->imageurl);
75
			if (empty($pic)) {
76
			    continue;
77
			}
78
			$url = str_replace('http://', 'https://', (string)$item->linkurl);
79
			if (empty($url)) {
80
			    continue;
81
			}
82
 
21 - 83
			switch ($merchantId) {
84
			    case 35126: // Sam Ash Music
85
			        if ($item->category->primary == "Books/Dvds") {
24 - 86
                        $type = 'New';
21 - 87
   	    			    $condition = "Brand New";
88
    	    			$category = "Book";
24 - 89
        			    $freeShippingCap = 0.00; // 9.99 on webpage
90
            			$handlingTime = 1;
91
            			$shippingCost = 0.00;
92
            			$shippingCurrency = 'USD';
21 - 93
    	    		} else {
24 - 94
    	    		    continue 2; // next loop
21 - 95
    	    		}
96
			        break;
97
 
98
			    case 13770: // Music Notes
24 - 99
                    $type = 'New';
21 - 100
   				    $condition = "Brand New";
101
    				$category = "Book";
24 - 102
       			    $freeShippingCap = 0.00;
103
           			$handlingTime = 1;
104
        			$shippingCost = 0.00;
105
        			$shippingCurrency = 'USD';
21 - 106
			        break;
107
 
108
			    case 2653: // alibris (does not return any search results) bugbug
24 - 109
       			    $freeShippingCap = 39.00;
110
           			$handlingTime = 1;
111
        			$shippingCost = 3.99;
112
        			$shippingCurrency = 'USD';
21 - 113
			        break;
114
 
115
   				default:
24 - 116
                    $type = 'Used';
21 - 117
   				    $condition = "Used";
118
    				$category = "CD";
24 - 119
       			    $freeShippingCap = 0.00;
120
        			$handlingTime = -1;
121
        			$shippingCost = 0.00;
122
        			$shippingCurrency = 'USD';
21 - 123
    				break;
20 - 124
			}
125
 
126
			$country = 'US';
127
			$bestOffer = false;
128
 
129
			$price = 0;
130
			if (!empty($item->price) && !empty($item->saleprice)) {
131
    			$price = minNotNull(array($item->price, $item->saleprice));
132
    		} else if (!empty($item->price)) {
133
    			$price = $item->price;
134
    		} else if (!empty($item->saleprice)) {
135
    			$price = $item->saleprice;
136
    		}
137
			$price = number_format(floatval($price), 2, '.', '');
138
			if ($price <= "0.00") {
139
			    continue;
140
			}
141
 
142
			$currency = 'USD';
143
			$timeLeft = 0;
144
			$listingType = 'Fixed';
145
			$location = 'US';
146
			$zip = '';
147
			$feedbackScore = -1;
148
			$feedbackPercent = -1;
149
			$sellerName = '';
150
 
151
// bugbug
21 - 152
//ls_cj_csv(array($merchantName,(string)$item->category->primary,(string)$item->productname,(string)$item->description->short,"'".$barcode,$barcodeType,minNotNull(array($item->price, (string)$item->saleprice)),(string)$item->linkurl));
20 - 153
 
154
            // this is last after all checks
155
			$resultsMid[] = $merchantId;
156
			if (array_count_values($resultsMid)[$merchantId] > $numResultsMid) {
157
			    continue;
158
			}
159
 
160
			$arr[] = array(
161
				"Merchant" => $merchantName,
24 - 162
				"Type" => "$type",
20 - 163
				"Title" => "$title",
164
				"Barcode" => "$barcode",
165
				"BarcodeType" => "$barcodeType",
166
				"Image" => "$pic",
167
				"URL" => "$url",
168
				"Category" => "$category",
169
				"Condition" => "$condition",
170
				"Country" => "$country",
171
				"BestOffer" => "$bestOffer",
172
				"TimeLeft" => "$timeLeft",
173
				"Price" => "$price",
174
				"Currency" => "$currency",
175
				"ListingType" => "$listingType",
176
				"Location" => "$location",
177
				"Zip" => "$zip",
178
				"FeedbackScore" => "$feedbackScore",
179
				"FeedbackPercent" => "$feedbackPercent",
180
				"SellerName" => "$sellerName",
181
				"HandlingTime" => "$handlingTime",
182
				"ShippingCost" => "$shippingCost",
183
				"ShippingCurrency" => "$shippingCurrency",
24 - 184
				"FreeShippingCap" => "$freeShippingCap",
20 - 185
				"Show" => true
186
			);
187
		}
188
	}
189
// If the response does not indicate 'Success,' log the error(s)
190
	else {
191
		foreach ($result->Errors as $error) {
192
		    if ($error->ErrorID != "7186919") { // no product found, not an error
193
    			error_log($url);
194
	    		error_log("$error->ErrorText ($error->ErrorId)");
195
	    	}
196
		}
197
	}
198
 
199
	return $arr;
200
}
201
 
202
// Get Linkshare Bearer Token
203
function getLinkshareToken($config)
204
{
205
    static $expiration = 0;
206
    static $accessToken = '';
207
    static $refreshToken = '';
208
 
209
    if ($expiration == 0 || time() > $expiration) {
210
        $url="https://api.rakutenmarketing.com/token";
211
        $postdata = "grant_type=password&username=" . $config['user'] . "&password=" . $config['password'] . "&scope=" . $config['scope'];
212
        $header = array(
213
            'Content-Type: application/x-www-form-urlencoded',
214
            'Authorization: ' . $config['authorizationToken']
215
            );
216
 
217
        $ch = curl_init();
218
        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
219
        curl_setopt($ch, CURLOPT_URL, $url);
220
        curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, FALSE);
221
        curl_setopt($ch, CURLOPT_TIMEOUT, 60);
222
        curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
223
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
224
        curl_setopt($ch, CURLOPT_REFERER, $url);
225
        curl_setopt($ch, CURLOPT_POSTFIELDS, $postdata);
226
        curl_setopt($ch, CURLOPT_POST, 1);
227
        $result = json_decode(curl_exec ($ch));
228
 
229
        curl_close($ch);
230
 
231
        $expiration = time() + $result->{'expires_in'};
232
        $accessToken = $result->{'access_token'};
233
        $refreshToken = $result->{'refresh_token'};
234
        $tokenType =  $result->{'token_type'};
235
    }
236
 
237
	return $accessToken;
238
}