Subversion Repositories cheapmusic

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 - 1
<?php
2
include_once('php/constants.php');
3
 
4
error_reporting(E_ALL);  // Turn on all errors, warnings and notices for easier debugging
5
 
6
// Get eBay listings
5 - 7
function get_ebay($query, $searchType)
8
{
1 - 9
// API request variables
5 - 10
	$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';
11
	$version = '1.13.0';
12
	$appid = 'UweJacob-MusicCDs-PRD-0eaa5c961-9b7ca596';
13
	$trackingId = '5338527412'; // EPN Campaign Id
14
	$globalid = 'EBAY-US';
15
	$safequery = urlencode($query);  // Make the query URL-friendly
16
	$numResults = 20; // Maximum Number of Search Results
1 - 17
 
18
// Create a PHP array of the item filters you want to use in your request
5 - 19
	$filterarray = array(
20
		array(
21
			'name' => 'Condition',
22
			'value' => ($searchType == constant("NEW") ? 'New' : 'Used'),
23
			'paramName' => '',
24
			'paramValue' => ''
25
		),
26
		array(
27
			'name' => 'HideDuplicateItems',
28
			'value' => 'true',
29
			'paramName' => '',
30
			'paramValue' => ''
31
		),
32
		array(
33
			'name' => 'ListingType',
34
			'value' => array('AuctionWithBIN', 'FixedPrice', 'StoreInventory'),
35
			'paramName' => '',
36
			'paramValue' => ''
37
		),
38
	);
1 - 39
 
40
// Build the indexed item filter URL snippet
5 - 41
	$urlfilter = buildURLArray($filterarray);
1 - 42
 
43
// Construct the findItemsByKeywords HTTP GET call
5 - 44
	$apicall = "$endpoint?";
45
	$apicall .= "OPERATION-NAME=findItemsAdvanced";
46
	$apicall .= "&SERVICE-VERSION=$version";
47
	$apicall .= "&SECURITY-APPNAME=$appid";
48
	$apicall .= "&GLOBAL-ID=$globalid";
49
	$apicall .= "&REST-PAYLOAD";
50
	$apicall .= "&affiliate.networkId=9";
51
	$apicall .= "&affiliate.trackingId=$trackingId";
52
	$apicall .= "&affiliate.customId=findCheapMusic";
53
	$apicall .= "&paginationInput.entriesPerPage=$numResults";
54
	$apicall .= "&sortOrder=PricePlusShippingLowest";
55
	if (isset($_SESSION['buyer"]["Zip'])) {
56
		$apicall .= "&buyerPostalCode=" . $_SESSION['buyer"]["Zip'];
57
	}
58
	$apicall .= "&outputSelector=SellerInfo";
59
	$apicall .= "&keywords=$safequery";
60
	if ($_SESSION["filterMediaType"]["CD"] && $_SESSION["filterMediaType"]["Record"]) {
61
		$apicall .= "&categoryId(0)=176985"; // Music Records
62
		$apicall .= "&categoryId(1)=176984"; // Music CDs
63
	} else if ($_SESSION["filterMediaType"]["CD"]) {
64
		$apicall .= "&categoryId=176984"; // Music CDs
65
	} else if ($_SESSION["filterMediaType"]["Record"]) {
66
		$apicall .= "&categoryId=176985"; // Music Records
67
	}
68
	$apicall .= "$urlfilter";
1 - 69
 
70
// Load the call and capture the document returned by eBay API
5 - 71
	$resp = simplexml_load_file($apicall);
1 - 72
 
5 - 73
	$arr = array();
1 - 74
 
75
// Check to see if the request was successful, else print an error
5 - 76
	if ($resp->ack == "Success") {
1 - 77
  // If the response was loaded, parse it and store in array
5 - 78
		foreach ($resp->searchResult->item as $item) {
79
			$title = $item->title;
80
			$pic = str_replace('http://', 'https://', $item->galleryURL);
81
			$url = str_replace('http://', 'https://', $item->viewItemURL);
82
			$category = $item->primaryCategory->categoryName;
83
			if ($category == "CDs") {
84
				$category = "CD";
85
			}
86
			if ($category == "CDs & DVDs") {
87
				$category = "CD";
88
			}
89
			if ($category == "Records") {
90
				$category = "Record";
91
			}
92
			$condition = $item->condition->conditionDisplayName;
93
			$country = $item->country;
94
			$bestOffer = $item->listingInfo->bestOfferEnabled;
95
			if ($item->listingInfo->buyItNowAvailable == "true") {
96
				$price = $item->listingInfo->convertedBuyItNowPrice;
97
				$currency = $item->listingInfo->convertedBuyItNowPrice['currencyId'];
98
			} else {
99
				$price = $item->sellingStatus->currentPrice;
100
				$currency = $item->sellingStatus->currentPrice['currencyId'];
101
			}
102
			$price = number_format(floatval($price), 2, '.', '');
103
			$timeLeft = $item->sellingStatus->timeLeft;
104
			$listingType = $item->listingInfo->listingType;
105
			$location = $item->location;
106
			$zip = $item->postalCode;
107
			$feedbackScore = $item->sellerInfo->feedbackScore;
108
			$feedbackPercent = $item->sellerInfo->positiveFeedbackPercent;
109
			$sellerName = $item->sellerInfo->sellerUserName;
110
			$handlingTime = $item->shippingInfo->handlingTime;
111
			$shippingCost = number_format(floatval($item->shippingInfo->shippingServiceCost), 2, '.', '');
112
			$shippingCurrency = $item->shippingInfo->shippingServiceCost['currencyId'];
1 - 113
 
114
    // skip items without gallery image
5 - 115
			if ($pic == "") {
116
				continue;
117
			}
1 - 118
 
5 - 119
			$arr[] = array(
120
				"Merchant" => "eBay",
121
				"Type" => ($searchType == constant("NEW") ? 'New' : 'Used'),
122
				"Title" => "$title",
123
				"Image" => "$pic",
124
				"URL" => "$url",
125
				"Category" => "$category",
126
				"Condition" => "$condition",
127
				"Country" => "$country",
128
				"BestOffer" => "$bestOffer",
129
				"TimeLeft" => "$timeLeft",
130
				"Price" => "$price",
131
				"Currency" => "$currency",
132
				"ListingType" => "$listingType",
133
				"Location" => "$location",
134
				"Zip" => "$zip",
135
				"FeedbackScore" => "$feedbackScore",
136
				"FeedbackPercent" => "$feedbackPercent",
137
				"SellerName" => "$sellerName",
138
				"HandlingTime" => "$handlingTime",
139
				"ShippingCost" => "$shippingCost",
140
				"ShippingCurrency" => "$shippingCurrency",
141
				"Show" => true
142
			);
143
		}
1 - 144
 
5 - 145
		return ($arr);
146
	}
1 - 147
// If the response does not indicate 'Success,' log an error
5 - 148
	else {
149
		foreach ($resp->errorMessage->error as $error) {
150
			error_log($apicall);
151
			error_log(
152
				"Severity: $error->severity; Category: $error->category; Domain: $error->domain; ErrorId: $error->errorId; Message: $error->message"
153
			);
154
		}
155
		return ([]);
156
	}
1 - 157
}
158
 
159
// Generates an indexed URL snippet from the array of item filters
5 - 160
function buildURLArray($filterarray)
161
{
162
	$filter = '';
163
	$i = '0';  // Initialize the item filter index to 0
1 - 164
 
165
  // Iterate through each filter in the array
5 - 166
	foreach ($filterarray as $itemfilter) {
1 - 167
    // Iterate through each key in the filter
5 - 168
		foreach ($itemfilter as $key => $value) {
169
			if (is_array($value)) {
170
				foreach ($value as $j => $content) { // Index the key for each value
171
					$filter .= "&itemFilter($i).$key($j)=$content";
172
				}
173
			} else {
174
				if ($value != "") {
175
					$filter .= "&itemFilter($i).$key=$value";
176
				}
177
			}
178
		}
179
		$i++;
180
	}
1 - 181
 
5 - 182
	return "$filter";
183
}
184
?>