Subversion Repositories cheapmusic

Rev

Rev 58 | Rev 66 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

<?php
include_once ('php/constants.php');

error_reporting(E_ALL);

// Get Walmart listings
function get_walmart($query, $searchType) {
    $vendors = Vendors::getInstance();
    $config = $vendors->getVendor(Vendors::WALMART);
    $safequery = urlencode($query);

    // Music
    $arrMusic = get_walmartCategory($config, $safequery, $searchType, $config["musicCategory"]);

    // Books
    $arrBooks = get_walmartCategory($config, $safequery, $searchType, $config["bookCategory"], $config["bookSubCategory"]);

    return (array_merge($arrMusic, $arrBooks));
}

// Get linkshare listings by advertiser id
function get_walmartCategory($config, $safequery, $searchType, $categoryId, $subCategories = []) {
    // API request variables
    $numResults = $config['numResults'];

    // Construct the findItemsByKeywords HTTP GET call
    $url = "https://api.walmartlabs.com/v1/search";
    $url .= "?apiKey=" . $config["apiKey"];
    $url .= "&lsPublisherId=" . $config["publisherId"];
    $url .= "&numItems=" . $numResults;
    $url .= "&start=1";
    $url .= "&sort=price";
    $url .= "&order=asc";
    $url .= "&categoryId=" . $categoryId;
    $url .= "&query=" . $safequery;

    $result = getUrl($url);
    $result = utf8_encode($result);
    $result = json_decode($result);

    $arr = [];

    //echo "$url<pre>";print_r($result);echo "</pre>";
    // Check to see if the request found any results
    if (isset($result->totalResults)) {
        if ($result->totalResults <= 0) {
            return [];
        }

        // If the response was loaded, parse it and store in array
        foreach ($result->items as $item) {
            if (!empty($subCategories) && !in_array($item->categoryNode, $subCategories)) {
                continue;
            }
            $merchantName = "Walmart";
            if (isset($item->marketplace) && $item->marketplace) {
                $merchantName .= " Marketplace";
            }
            $barcode = (!empty($item->upc) ? $item->upc : "");
            $barcodeType = clsLibGTIN::GTINCheck($barcode, false, 1);

            $title = (string)$item->name;
            $pic = str_replace('http://', 'https://', (string)$item->thumbnailImage);
            if (empty($pic)) {
                continue;
            }

            if (!isset($item->productTrackingUrl)) {
                continue;
            }
            $url = str_replace('http://', 'https://', (string)$item->productUrl);
            if (empty($url)) {
                continue;
            }

            if ((string)$result->categoryId == $config["musicCategory"]) {
                if (strpos((string)$item->categoryPath, "Vinyl Records") !== false) {
                    $category = "Record";
                }
                else if (strpos((string)$item->categoryPath, "Cassette Tapes") !== false) {
                    continue;
                }
                else { // category "...on CD or Vinyl"
                    if (strpos($title, "Vinyl") !== false) {
                        $category = "Record";
                    }
                    else {
                        $category = "CD";
                    }
                }
            }
            else {
                $category = "Book";
            }

            $type = 'New';
            $condition = 'Brand New';
            $country = 'US';
            $bestOffer = false;

            $price = number_format(floatval(empty($item->salePrice) ? 0 : $item->salePrice) , 2, '.', '');
            if ($price <= "0.00") {
                continue;
            }

            $currency = 'USD';
            $timeLeft = 0;
            $listingType = 'Fixed';
            $location = 'US';
            $zip = '';
            $feedbackScore = - 1;
            $feedbackPercent = - 1;
            $sellerName = (!empty($item->sellerInfo) ? (string)$item->sellerInfo : "");
            $handlingTime = - 1;
            if (!(isset($item->marketplace) && $item->marketplace)) {
                $handlingTime = 1;
            }
            $shippingCost = 0.00;
            if (isset($item->standardShipRate)) {
                $shippingCost = number_format(floatval($item->standardShipRate) , 2, '.', '');
            }
            $shippingCurrency = 'USD';
            $freeShippingCap = 0;
            //                  if ($item->isTwoDayShippingEligible == "1" || $item->freeShippingOver35Dollars == "1") {
            if (!(isset($item->marketplace) && $item->marketplace)) {
                $freeShippingCap = 35.00;
            }

            $arr[] = array(
                "Merchant" => $merchantName,
                "Type" => "$type",
                "Title" => "$title",
                "Barcode" => "$barcode",
                "BarcodeType" => "$barcodeType",
                "Image" => "$pic",
                "URL" => "$url",
                "Category" => "$category",
                "Condition" => "$condition",
                "Country" => "$country",
                "BestOffer" => "$bestOffer",
                "TimeLeft" => "$timeLeft",
                "Price" => "$price",
                "Currency" => "$currency",
                "ListingType" => "$listingType",
                "Location" => "$location",
                "Zip" => "$zip",
                "FeedbackScore" => "$feedbackScore",
                "FeedbackPercent" => "$feedbackPercent",
                "SellerName" => "$sellerName",
                "HandlingTime" => "$handlingTime",
                "ShippingCost" => "$shippingCost",
                "ShippingCurrency" => "$shippingCurrency",
                "FreeShippingCap" => "$freeShippingCap",
                "Show" => true
            );
        }
    }
    // If the response does not indicate 'Success,' log the error(s)
    else {
        if (!empty($result->errors)) {
            foreach ($result->errors as $error) {
                error_log($url);
                error_log("$error->message ($error->code)");
            }
        }
    }

    return $arr;
}