Subversion Repositories cheapmusic

Rev

Rev 96 | Rev 136 | Go to most recent revision | Details | Compare with Previous | Last modification | View Log | RSS feed

Rev Author Line No. Line
20 - 1
<?php
65 - 2
include_once ('php/constants.php');
20 - 3
 
4
error_reporting(E_ALL);
5
 
6
// Get Walmart listings
66 - 7
function get_walmart($query, $searchCondition) {
20 - 8
    $vendors = Vendors::getInstance();
9
    $config = $vendors->getVendor(Vendors::WALMART);
10
 
11
    // Music
81 - 12
    $arrMusic = get_walmartCategory($config, $query, $searchCondition, $config["musicCategory"]);
20 - 13
 
14
    // Books
81 - 15
    $arrBooks = get_walmartCategory($config, $query, $searchCondition, $config["bookCategory"], $config["bookSubCategory"]);
20 - 16
 
65 - 17
    return (array_merge($arrMusic, $arrBooks));
20 - 18
}
19
 
20
// Get linkshare listings by advertiser id
81 - 21
function get_walmartCategory($config, $query, $searchCondition, $categoryId, $subCategories = []) {
65 - 22
    // API request variables
23
    $numResults = $config['numResults'];
20 - 24
 
99 - 25
    $result = getSearchCache("walmart", $query, $categoryId);
26
    if ($result === false) {
27
        // Construct the findItemsByKeywords HTTP GET call
28
        $params = [];
29
        $params["apiKey"] = $config["apiKey"];
30
        $params["lsPublisherId"] = $config["publisherId"];
31
        $params["numItems"] = $numResults;
32
        $params["start"] = "1";
33
        $params["sort"] = "price";
34
        $params["order"] = "asc";
35
        $params["categoryId"] = $categoryId;
36
        $params["query"] = $query;
20 - 37
 
99 - 38
        $pairs = array();
39
        foreach ($params as $key => $value) {
40
            array_push($pairs, rawurlencode($key)."=".rawurlencode($value));
41
        }
42
        $canonical_query_string = join("&", $pairs);
43
 
44
        $url = "https://api.walmartlabs.com/v1/search?" . $canonical_query_string;
45
        $result = getUrl($url);
46
 
47
        saveSearchCache("walmart", $query, $categoryId, $result);
81 - 48
    }
49
 
65 - 50
    $result = utf8_encode($result);
51
    $result = json_decode($result);
20 - 52
 
65 - 53
    $arr = [];
20 - 54
 
65 - 55
    //echo "$url<pre>";print_r($result);echo "</pre>";
56
    // Check to see if the request found any results
57
    if (isset($result->totalResults)) {
21 - 58
        if ($result->totalResults <= 0) {
59
            return [];
60
        }
61
 
65 - 62
        // If the response was loaded, parse it and store in array
63
        foreach ($result->items as $item) {
73 - 64
            if (!empty($subCategories) && !empty($item->categoryNode) && !in_array($item->categoryNode, $subCategories)) {
65 - 65
                continue;
66
            }
67
            $merchantName = "Walmart";
68
            if (isset($item->marketplace) && $item->marketplace) {
20 - 69
                $merchantName .= " Marketplace";
65 - 70
            }
71
            $barcode = (!empty($item->upc) ? $item->upc : "");
72
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);
20 - 73
 
65 - 74
            $title = (string)$item->name;
75
            $pic = str_replace('http://', 'https://', (string)$item->thumbnailImage);
76
            if (empty($pic)) {
77
                continue;
78
            }
27 - 79
 
80
            if (!isset($item->productTrackingUrl)) {
81
                continue;
82
            }
65 - 83
            $url = str_replace('http://', 'https://', (string)$item->productUrl);
84
            if (empty($url)) {
85
                continue;
86
            }
20 - 87
 
65 - 88
            if ((string)$result->categoryId == $config["musicCategory"]) {
80 - 89
                if (!empty($item->categoryPath) && strpos((string)$item->categoryPath, "Vinyl Records") !== false) {
66 - 90
                    $mediaType = "Record";
65 - 91
                }
80 - 92
                else if (!empty($item->categoryPath) && strpos((string)$item->categoryPath, "Cassette Tapes") !== false) {
65 - 93
                    continue;
94
                }
66 - 95
                else { // mediaType "...on CD or Vinyl"
65 - 96
                    if (strpos($title, "Vinyl") !== false) {
66 - 97
                        $mediaType = "Record";
65 - 98
                    }
99
                    else {
66 - 100
                        $mediaType = "CD";
65 - 101
                    }
102
                }
103
            }
104
            else {
66 - 105
                $mediaType = "Book";
65 - 106
            }
20 - 107
 
66 - 108
            $condition = 'New';
109
            $detailCondition = 'Brand New';
65 - 110
            $country = 'US';
111
            $bestOffer = false;
20 - 112
 
65 - 113
            $price = number_format(floatval(empty($item->salePrice) ? 0 : $item->salePrice) , 2, '.', '');
114
            if ($price <= "0.00") {
115
                continue;
116
            }
20 - 117
 
65 - 118
            $currency = 'USD';
119
            $timeLeft = 0;
120
            $listingType = 'Fixed';
121
            $location = 'US';
122
            $zip = '';
123
            $feedbackScore = - 1;
124
            $feedbackPercent = - 1;
125
            $sellerName = (!empty($item->sellerInfo) ? (string)$item->sellerInfo : "");
126
            $handlingTime = - 1;
24 - 127
            if (!(isset($item->marketplace) && $item->marketplace)) {
65 - 128
                $handlingTime = 1;
129
            }
130
            $shippingCost = 0.00;
81 - 131
            $shippingEstimated = false;
65 - 132
            if (isset($item->standardShipRate)) {
133
                $shippingCost = number_format(floatval($item->standardShipRate) , 2, '.', '');
134
            }
135
            $shippingCurrency = 'USD';
24 - 136
            $freeShippingCap = 0;
65 - 137
            //			if ($item->isTwoDayShippingEligible == "1" || $item->freeShippingOver35Dollars == "1") {
24 - 138
            if (!(isset($item->marketplace) && $item->marketplace)) {
65 - 139
                $freeShippingCap = 35.00;
140
            }
20 - 141
 
65 - 142
            $arr[] = array(
143
                "Merchant" => $merchantName,
81 - 144
                "Condition" => $condition,
145
                "Title" => $title,
146
                "Barcode" => $barcode,
147
                "BarcodeType" => $barcodeType,
148
                "Image" => $pic,
149
                "URL" => $url,
150
                "MediaType" => $mediaType,
151
                "DetailCondition" => $detailCondition,
152
                "Country" => $country,
153
                "BestOffer" => $bestOffer,
154
                "TimeLeft" => $timeLeft,
155
                "Price" => $price,
156
                "Currency" => $currency,
157
                "ListingType" => $listingType,
158
                "Location" => $location,
159
                "Zip" => $zip,
160
                "FeedbackScore" => $feedbackScore,
161
                "FeedbackPercent" => $feedbackPercent,
162
                "SellerName" => $sellerName,
163
                "HandlingTime" => $handlingTime,
164
                "ShippingCost" => $shippingCost,
165
                "ShippingEstimated" => $shippingEstimated,
166
                "ShippingCurrency" => $shippingCurrency,
167
                "FreeShippingCap" => $freeShippingCap,
65 - 168
                "Show" => true
169
            );
170
        }
171
    }
172
    // If the response does not indicate 'Success,' log the error(s)
173
    else {
174
        if (!empty($result->errors)) {
175
            foreach ($result->errors as $error) {
96 - 176
                my_error_log($url);
177
                my_error_log("$error->message ($error->code)");
65 - 178
            }
179
        }
180
    }
20 - 181
 
65 - 182
    return $arr;
20 - 183
}