Subversion Repositories cheapmusic

Rev

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