Subversion Repositories cheapmusic

Rev

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