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