Subversion Repositories cheapmusic

Rev

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