如何在eBay API查询中搜索一系列数字?

时间:2012-04-22 22:51:54

标签: php ebay

以下代码返回结果列表,其中关键字“27”出现在项目标题或说明中。如何让它返回结果列表,其中关键字+或 - 1出现在描述中?所以在这种情况下26,27或28.查询变量在第10行。

<?php

error_reporting(E_ALL);  // Turn on all errors, warnings and notices for easier debugging

// API request variables
$endpoint = 'http://svcs.ebay.com/services/search/FindingService/v1';  // URL to call
$version = '1.0.0';  // API version supported by your application
$appid = 'carson2e7-8d18-4e3b-8267-5a37144ec50';  // Replace with your own AppID
$globalid = 'EBAY-US';  // Global ID of the eBay site you want to search (e.g., EBAY-DE)
$query = 'shirt 27';  // You may want to supply your own query
$safequery = urlencode($query);  // Make the query URL-friendly
$i = '0';  // Initialize the item filter index to 0

// Create a PHP array of the item filters you want to use in your request
$filterarray =
  array(
    array(
    'name' => 'MaxPrice',
    'value' => '1500',
    'paramName' => 'Currency',
    'paramValue' => 'USD'),
    array(
    'name' => 'FreeShippingOnly',
    'value' => 'false',
    'paramName' => '',
    'paramValue' => ''),
    array(
    'name' => 'ListingType',
    'value' => array('AuctionWithBIN','FixedPrice','StoreInventory'),
    'paramName' => '',
    'paramValue' => ''),
  );

// Generates an indexed URL snippet from the array of item filters
function buildURLArray ($filterarray) {
  global $urlfilter;
  global $i;
  // Iterate through each filter in the array
  foreach($filterarray as $itemfilter) {
    // Iterate through each key in the filter
    foreach ($itemfilter as $key =>$value) {
      if(is_array($value)) {
        foreach($value as $j => $content) { // Index the key for each value
          $urlfilter .= "&itemFilter($i).$key($j)=$content";
        }
      }
      else {
        if($value != "") {
          $urlfilter .= "&itemFilter($i).$key=$value";
        }
      }
    }
    $i++;
  }
  return "$urlfilter";
} // End of buildURLArray function

// Build the indexed item filter URL snippet
buildURLArray($filterarray);

// Construct the findItemsByKeywords HTTP GET call 
$apicall = "$endpoint?";
$apicall .= "OPERATION-NAME=findItemsAdvanced";
$apicall .= "&SERVICE-VERSION=$version";
$apicall .= "&SECURITY-APPNAME=$appid";
$apicall .= "&GLOBAL-ID=$globalid";
$apicall .= "&descriptionSearch=true";
$apicall .= "&categoryId=110";
$apicall .= "&keywords=$safequery";
$apicall .= "&paginationInput.entriesPerPage=100";
$apicall .= "$urlfilter";

// Load the call and capture the document returned by eBay API
$resp = simplexml_load_file($apicall);

// Check to see if the request was successful, else print an error
if ($resp->ack == "Success") {
  $results = '';
  // If the response was loaded, parse it and build links  
  foreach($resp->searchResult->item as $item) {
    $pic    = $item->galleryURL;
    $link   = $item->viewItemURL;
    $title  = $item->title;

    // For each SearchResultItem node, build a link and append it to $results
    $results .= "<a href=\"$link\" title=\"$title\"><img src=\"$pic\" height=\"250\" width=\"250\" style=\"margin:5px;\"></a>";
  }
}
// If the response does not indicate 'Success,' print an error
else {
  $results  = "<h3>Oops! The request was not successful. Make sure you are using a valid ";
  $results .= "AppID for the Production environment.</h3>";
}
?>

<!-- Build the HTML page with values from the call response -->
<html>
<head>
<title>eBay Search Results for <?php echo $query; ?></title>
<style type="text/css">
body {
  font-family: arial,sans-serif;
  }
.container {
  max-width: 1200px;
}

  </style>
</head>
<body>

<div class="container">

    <?php echo $results;?>

</div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

这比我想象的要容易......我修改了第10行:

$ query ='shirt(27,26,28)'; //您可能想提供自己的查询

在这里找到答案: http://developer.ebay.com/DevZone/XML/docs/WebHelp/wwhelp/wwhimpl/common/html/wwhelp.htm?context=eBay_XML_API&file=SearchingForItems-Searching_for_Items_by_Using_a_Query.html