Subversion Repositories cheapmusic

Rev

Rev 129 | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
1 - 1
<?php
65 - 2
include_once ('php/constants.php');
1 - 3
 
4
// Get eBay listings
110 - 5
function get_ebay($query, $searchCondition, $zipFlag = true) {
9 - 6
    $vendors = Vendors::getInstance();
10 - 7
    $config = $vendors->getVendor(Vendors::EBAY);
109 - 8
    $apicall = '[cached]';
13 - 9
 
110 - 10
    $resp = ($zipFlag ? getSearchCache("ebay", $query, $searchCondition) : false);
99 - 11
    if ($resp === false) {
12
        // API request variables
13
        $endpoint = 'https://svcs.ebay.com/services/search/FindingService/v1';
14
        $version = '1.13.0';
15
        $appid = $config['appid'];
16
        $trackingId = $config['trackingId'];
17
        $globalid = 'EBAY-US';
18
        $numResults = $config['numResults'];
1 - 19
 
99 - 20
        // Create a PHP array of the item filters you want to use in your request
21
        $filterarray = array(
22
            array(
23
                'name' => 'Condition',
24
                'value' => ($searchCondition == constant("NEW") ? 'New' : 'Used') ,
25
                'paramName' => '',
26
                'paramValue' => ''
65 - 27
            ) ,
99 - 28
            array(
29
                'name' => 'HideDuplicateItems',
30
                'value' => 'true',
31
                'paramName' => '',
32
                'paramValue' => ''
33
            ) ,
34
            array(
35
                'name' => 'ListingType',
36
                'value' => array(
37
                    'AuctionWithBIN',
38
                    'FixedPrice',
39
                    'StoreInventory'
40
                ) ,
41
                'paramName' => '',
42
                'paramValue' => ''
43
            ) ,
44
        );
1 - 45
 
99 - 46
        // Build the indexed item filter URL snippet
47
        $urlfilter = buildURLArray($filterarray);
1 - 48
 
99 - 49
        // Construct the findItemsByKeywords HTTP GET call
50
        $params = [];
51
        $params["OPERATION-NAME"] = "findItemsAdvanced";
52
        $params["SERVICE-VERSION"] = $version;
53
        $params["SECURITY-APPNAME"] = $appid;
54
        $params["GLOBAL-ID"] = $globalid;
55
        $params["REST-PAYLOAD"] = "";
56
        $params["affiliate.networkId"] = "9";
57
        $params["affiliate.trackingId"] = $trackingId;
58
        $params["affiliate.customId"] = "findCheapMusic";
59
        $params["paginationInput.entriesPerPage"] = $numResults;
60
        $params["sortOrder"] = "PricePlusShippingLowest";
110 - 61
        if (!empty($_SESSION["buyer"]["Zip"]) && $zipFlag) {
99 - 62
            $params["buyerPostalCode"] = $_SESSION["buyer"]["Zip"];
63
        }
64
        $params["outputSelector"] = "SellerInfo";
65
        $params["keywords"] = $query;
66
        if ($_SESSION["filterMediaType"]["CD"] && $_SESSION["filterMediaType"]["Record"]) {
67
            $params["categoryId(0)"] = "176985"; // Music Records
68
            $params["categoryId(1)"] = "176984"; // Music CDs
69
 
70
        }
71
        else if ($_SESSION["filterMediaType"]["CD"]) {
72
            $params["categoryId"] = "176984"; // Music CDs
73
 
74
        }
75
        else if ($_SESSION["filterMediaType"]["Record"]) {
76
            $params["categoryId"] = "176985"; // Music Records
77
 
78
        }
79
        $params = array_merge($params, $urlfilter);
1 - 80
 
99 - 81
        $pairs = array();
82
        foreach ($params as $key => $value) {
83
            array_push($pairs, rawurlencode($key) . (!empty($value) ? "=" . rawurlencode($value) : ""));
84
        }
85
        $canonical_query_string = join("&", $pairs);
81 - 86
 
99 - 87
        $apicall = $endpoint . "?" . $canonical_query_string;
81 - 88
 
99 - 89
        // Load the call and capture the document returned by eBay API
90
        $resp = getUrl($apicall);
91
        if (empty($resp)) {
92
            return ([]);
93
        }
94
        saveSearchCache("ebay", $query, $searchCondition, $resp);
89 - 95
    }
99 - 96
 
65 - 97
    $resp = simplexml_load_string($resp);
98
    $arr = array();
1 - 99
 
65 - 100
    // Check to see if the request was successful, else print an error
101
    if ((string)$resp->ack == "Success") {
102
        // If the response was loaded, parse it and store in array
103
        foreach ($resp
104
            ->searchResult->item as $item) {
105
            $title = (string)$item->title;
106
            $pic = str_replace('http://', 'https://', (string)$item->galleryURL);
107
            $url = str_replace('http://', 'https://', (string)$item->viewItemURL);
66 - 108
            $mediaType = (string)$item
65 - 109
                ->primaryCategory->categoryName;
66 - 110
            if ($mediaType == "CDs") {
111
                $mediaType = "CD";
65 - 112
            }
66 - 113
            else if ($mediaType == "CDs & DVDs") {
114
                $mediaType = "CD";
65 - 115
            }
66 - 116
            else if ($mediaType == "Records") {
117
                $mediaType = "Record";
65 - 118
            }
129 - 119
            else if ($mediaType == "Disques") {
120
                $mediaType = "Record";
121
            }
66 - 122
            $detailCondition = (string)$item
65 - 123
                ->condition->conditionDisplayName;
124
            $country = (string)$item->country;
81 - 125
            $bestOffer = (string)$item
65 - 126
                ->listingInfo->bestOfferEnabled;
127
            if ($item
128
                ->listingInfo->buyItNowAvailable == "true") {
129
                $price = (string)$item
130
                    ->listingInfo->convertedBuyItNowPrice;
131
                $currency = (string)$item
132
                    ->listingInfo
133
                    ->convertedBuyItNowPrice['currencyId'];
134
            }
135
            else {
136
                $price = (string)$item
137
                    ->sellingStatus->currentPrice;
138
                $currency = (string)$item
139
                    ->sellingStatus
140
                    ->currentPrice['currencyId'];
141
            }
142
            $price = number_format(floatval($price) , 2, '.', '');
143
            $timeLeft = decodeeBayTimeLeft($item
144
                ->sellingStatus
145
                ->timeLeft);
146
            $listingType = (string)$item
147
                ->listingInfo->listingType;
148
            $location = (string)$item->location;
149
            $zip = (string)$item->postalCode;
150
            $feedbackScore = (string)$item
151
                ->sellerInfo->feedbackScore;
152
            $feedbackPercent = (string)$item
153
                ->sellerInfo->positiveFeedbackPercent;
154
            $sellerName = (string)$item
155
                ->sellerInfo->sellerUserName;
156
            $handlingTime = (string)$item
157
                ->shippingInfo->handlingTime;
81 - 158
            $shippingEstimated = false;
65 - 159
            $shippingCost = number_format(floatval((string)$item
160
                ->shippingInfo
161
                ->shippingServiceCost) , 2, '.', '');
162
            $shippingCurrency = (string)$item
163
                ->shippingInfo
164
                ->shippingServiceCost['currencyId'];
81 - 165
            if ($shippingCost <= 0.00 && strpos($item->shippingInfo->shippingType, "Calculated") !== false) {
166
                $shippingCost = 3.00;
167
                $shippingCurrency = "USD";
168
                $shippingEstimated = true;
169
            }
1 - 170
 
65 - 171
            // skip items without gallery image
172
            if ($pic == "") {
173
                continue;
174
            }
1 - 175
 
65 - 176
            $arr[] = array(
177
                "Merchant" => "eBay",
66 - 178
                "Condition" => ($searchCondition == constant("NEW") ? 'New' : 'Used') ,
81 - 179
                "Title" => $title,
65 - 180
                "Barcode" => "",
181
                "BarcodeType" => "",
81 - 182
                "Image" => $pic,
183
                "URL" => $url,
184
                "MediaType" => $mediaType,
185
                "DetailCondition" => $detailCondition,
186
                "Country" => $country,
187
                "BestOffer" => $bestOffer,
188
                "TimeLeft" => $timeLeft,
189
                "Price" => $price,
190
                "Currency" => $currency,
191
                "ListingType" => $listingType,
192
                "Location" => $location,
193
                "Zip" => $zip,
194
                "FeedbackScore" => $feedbackScore,
195
                "FeedbackPercent" => $feedbackPercent,
196
                "SellerName" => $sellerName,
197
                "HandlingTime" => $handlingTime,
198
                "ShippingCost" => $shippingCost,
199
                "ShippingCurrency" => $shippingCurrency,
200
                "ShippingEstimated" => $shippingEstimated,
201
                "FreeShippingCap" => 0,
143 - 202
                "Show" => true,
203
                "Details" => ""
65 - 204
            );
205
        }
1 - 206
 
65 - 207
        return ($arr);
208
    }
209
    // If the response does not indicate 'Success,' log an error
210
    else {
105 - 211
        foreach ($resp->errorMessage->error as $error) {
96 - 212
            my_error_log($apicall);
213
            my_error_log("Severity: $error->severity; Category: $error->category; Domain: $error->domain; ErrorId: $error->errorId; Message: $error->message");
65 - 214
        }
110 - 215
 
216
        if ($error->errorId == 18 && $zipFlag) {
217
            my_error_log("Retry without zip code");
218
            return (get_ebay($query, $searchCondition, false));
219
        }
65 - 220
    }
110 - 221
 
222
    return ([]);
1 - 223
}
224
 
225
// Generates an indexed URL snippet from the array of item filters
65 - 226
function buildURLArray($filterarray) {
81 - 227
    $filter = [];
65 - 228
    $i = '0'; // Initialize the item filter index to 0
229
    // Iterate through each filter in the array
230
    foreach ($filterarray as $itemfilter) {
231
        // Iterate through each key in the filter
232
        foreach ($itemfilter as $key => $value) {
233
            if (is_array($value)) {
234
                foreach ($value as $j => $content) { // Index the key for each value
81 - 235
                    $filter["itemFilter($i).$key($j)"] = $content;
65 - 236
                }
237
            }
238
            else {
239
                if ($value != "") {
81 - 240
                    $filter["itemFilter($i).$key"] =$value;
65 - 241
                }
242
            }
243
        }
244
        $i++;
245
    }
13 - 246
 
81 - 247
    return $filter;
5 - 248
}
24 - 249
 
250
// convert "P##D##H##M##S" to "## days ## hours ## minutes left"
251
function decodeeBayTimeLeft($t) {
252
    preg_match_all('!\d+!', $t, $m);
253
 
254
    $s = "";
255
    if ($m[0][0] > 0) {
256
        $s .= $m[0][0] . " days ";
257
    }
258
 
259
    if ($m[0][1] > 0) {
260
        $s .= $m[0][1] . " hours ";
261
    }
262
 
263
    if ($m[0][2]) {
264
        $s .= $m[0][2] . " minutes left";
265
    }
65 - 266
 
267
    return $s;
268
}
269