Understanding the Ebay API

Hi
I'm trying to get a grips with the Ebay API I'm using PHP just because it seemed a good place to start

My End goal is to be able to compare the price of currently listed DVD's  with the cheapest equivalent for sale elsewhere on ebay

For now I wish to be able to search for a DVD by name and return new DVD's ordered by Lowest Price + P&P

Example search from within the UK

The code bellow builds on the Getting Started Tutorial

I'm confused with how to filter when I attempted to Enter DVD category into the filter array the page failed

Sample from the Var Dump
  public 'primaryCategory' => 
    object(SimpleXMLElement)[3]
      public 'categoryId' => string '617' (length=3)
      public 'categoryName' => string 'DVDs & Blu-rays' (length=15)
  public 'condition' => 
    object(SimpleXMLElement)[9]
      public 'conditionId' => string '1000' (length=4)
      public 'conditionDisplayName' => string 'New' (length=9)

Open in new window

I've no Idea how to Sort on price or Region code

Sample Script

<?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 = 'srrtg78w5465y6ujyj57uti0hk54y-035y5kMITwasHere';  // Replace with your own AppID
//$appid = 54tg3rty35tjhu46yu5677j757jMITwasHere;  // Replace with your own AppID
$globalid = 'EBAY-GB';  // Global ID of the eBay site you want to search (e.g., EBAY-DE)
//$query = 'the searchers';  // This returns many different items not just DVD's
$query = 'The Undefeated';  // This returns just DVD's
$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(
      // my attempt at building DVD caused errors
//    array(
//    'name' => 'primaryCategory',
//    'value' => '617',
//    'paramName' => 'categoryName',
//    'paramValue' => 'DVDs & Blu-rays'),
    array(
    'name' => 'MaxPrice',
    'value' => '25',
    'paramName' => 'Currency',
    'paramValue' => 'GBP'),
    array(
    'name' => 'FreeShippingOnly',
    'value' => 'true',
    'paramName' => '',
    'paramValue' => ''),
    array(
    'name' => 'ListingType',
    'value' => array('AuctionWithBIN','FixedPrice'),
    'paramName' => '',
    'paramValue' => ''),
  );

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

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

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

//var_dump($resp);
// 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) {
 var_dump($item);  // See what's returned for each item 
    $pic   = $item->galleryURL;
    $link  = $item->viewItemURL;
    $title = $item->title;
    $currentPrice = $item->sellingStatus->currentPrice;
    $convertedCurrentPrice =$item->sellingStatus->convertedCurrentPrice;
    $BestOffer = $item->listingInfo->bestOfferEnabled;
    $listingType= $item->listingInfo->listingType;
    $buyItNowAvailable= $item->listingInfo->buyItNowAvailable;

    // For each SearchResultItem node, build a link and append it to $results
    $results .= "<tr><td><img src=\"$pic\"></td><td><a href=\"$link\">$title</a></td><td>&pound;$currentPrice</td>"
            . "<td>&pound;$convertedCurrentPrice</td>"
            . "<td>$BestOffer</td>"
            . "<td>$listingType</td>"
            . "<td>$buyItNowAvailable</td>"
            . "</tr>";
  }
}
// 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>";
}

// 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 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;} </style>
</head>
<body>

<h1>eBay Search Results for <?php echo $query; ?></h1>

<table>
<tr>
  <td>
    <?php echo $results;?>
  </td>
</tr>
</table>

</body>
</html>

Open in new window

LVL 1
trevor1940Asked:
Who is Participating?
 
rgranlundConnect With a Mentor Commented:
https://developer.ebay.com/devzone/finding/callref/types/SortOrderType.html
There seems to be CountryDescending and PricePlusShippingLowest

At the bottom of the page here:
http://developer.ebay.com/devzone/finding/concepts/findingapiguide.html

Add this to your PHP:
$apicall .= "&PricePlusShippingAsc";

So this part would look like this:
$apicall = "$endpoint?";
$apicall .= "OPERATION-NAME=findItemsByKeywords";
$apicall .= "&SERVICE-VERSION=$version";
$apicall .= "&SECURITY-APPNAME=$appid";
$apicall .= "&GLOBAL-ID=$globalid";
$apicall .= "&keywords=$safequery";
$apicall .= "&paginationInput.entriesPerPage=3";
$apicall .= "&PricePlusShippingAsc";
$apicall .= "$urlfilter";

At least it is a place to start.
0
 
trevor1940Author Commented:
Thanx for that

Any suggestions how to fitter by Category DVD & Condition New?

I've been searching but haven't found the answer  
It isn't clear from the documentation how to build this part of the query
0
 
Scott Fell, EE MVEConnect With a Mentor Developer & EE ModeratorCommented:
If you look at step 3 http://developer.ebay.com/DevZone/finding/HowTo/GettingStarted_PHP_NV_XML/GettingStarted_PHP_NV_XML.html it shows how to call filters

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

Open in new window

0
The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

 
trevor1940Author Commented:
Yes I know

If you look at the code I posted I attempted to add DVD Filter to that array but it failed
Clearly I have the category selection wrong but don't  understand how it should be
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I think you should be able to get that from the url

You have
//    array(
//    'name' => 'primaryCategory',
//    'value' => '617',
//    'paramName' => 'categoryName',
//    'paramValue' => 'DVDs & Blu-rays'),

Open in new window


Try

https://www.ebay.co.uk/sch/DVDs-Blu-rays/617/i.html?_from=R40&Format=DVD&_dcat=617&LH_ItemCondition=1000&_nkw=the+searchers&_sop=15
array(
 'name' => 'Format',
 'value' => 'DVD',
 'paramName' => '',
 'paramValue' => ''),

Open in new window


Also, remember to url encode your values.  If you are trying to post, "DVDs & Blu-rays" that should be, "DVDs%20%26%20Blu-rays" and you can do that in php  

http://php.net/manual/en/function.urlencode.php
urlencode('DVDs & Blu-rays');

Open in new window

0
 
trevor1940Author Commented:
I'm still struggling to get this working

The suggestion from scot caused the API to return an error

This make a correct call EG the API dosn't error but  returns CDs ('categoryId' => '176984' ) not DVDs

    array(
    'name' => 'categoryId',
    'value' => '617',
//    'paramName' => urlencode('DVDs & Blu-rays'),
    'paramName' => '',
    'paramValue' => ''
    ),

Open in new window

0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
> The suggestion from scot caused the API to return an error

What is the exact error?

> This make a correct call EG the API dosn't error but  returns CDs ('categoryId' => '176984' ) not DVDs
What is the category id for DVD's?  something other than 176984
0
 
trevor1940Author Commented:
The Category for DVD & Blu Rays is 617

When the API request fails The Error is "failed to open stream: HTTP request failed! "

And this part of the code kicks in

// 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>";
}

Open in new window


I'm clearly missing  / doing something wrong
0
 
trevor1940Author Commented:
I've not really solved this but searching google suggest may need to use a sdk instead of raw code

https://sdk.intradesys.com/ebay_api_sdk_test_tool?load=4c56ff4ce4aaf9573aa5dff913df997a
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I can run your code from Sample Script in your original question and get output
object(SimpleXMLElement)#3 (17) {
  ["itemId"]=&gt;
  string(12) "302393938957"
  ["title"]=&gt;
  string(53) "The Comancheros/The Undefeated DVD (2003) John Wayne "
  ["globalId"]=&gt;
  string(7) "EBAY-GB"
  ["primaryCategory"]=&gt;
  object(SimpleXMLElement)#5 (2) {
    ["categoryId"]=&gt;
    string(3) "617"
    ["categoryName"]=&gt;
    string(15) "DVDs &amp; Blu-rays"
  }
  ["galleryURL"]=&gt;
  string(63) "http://thumbs2.ebaystatic.com/m/mBDsbLQ12qEGzPBLdElHfBw/140.jpg"
  ["viewItemURL"]=&gt;
  string(86) "http://www.ebay.co.uk/itm/Comancheros-The-Undefeated-DVD-2003-John-Wayne-/302393938957"
  ["productId"]=&gt;
  string(7) "3959919"
  ["paymentMethod"]=&gt;
  string(6) "PayPal"
  ["autoPay"]=&gt;
  string(4) "true"
  ["location"]=&gt;
  string(14) "United Kingdom"
  ["country"]=&gt;
  string(2) "GB"
  ["shippingInfo"]=&gt;
  object(SimpleXMLElement)#6 (3) {
    ["shippingServiceCost"]=&gt;
    string(3) "0.0"
    ["shippingType"]=&gt;
    string(4) "Free"
    ["shipToLocations"]=&gt;
    string(9) "Worldwide"
  }
  ["sellingStatus"]=&gt;
  object(SimpleXMLElement)#7 (4) {
    ["currentPrice"]=&gt;
    string(3) "1.8"
    ["convertedCurrentPrice"]=&gt;
    string(3) "1.8"
    ["sellingState"]=&gt;
    string(6) "Active"
    ["timeLeft"]=&gt;
    string(13) "P16DT19H21M3S"
  }
  ["listingInfo"]=&gt;
  object(SimpleXMLElement)#8 (7) {
    ["bestOfferEnabled"]=&gt;
    string(5) "false"
    ["buyItNowAvailable"]=&gt;
    string(5) "false"
    ["startTime"]=&gt;
    string(24) "2017-07-25T12:18:30.000Z"
    ["endTime"]=&gt;
    string(24) "2018-04-21T12:18:30.000Z"
    ["listingType"]=&gt;
    string(10) "FixedPrice"
    ["gift"]=&gt;
    string(5) "false"
    ["watchCount"]=&gt;
    string(2) "11"
  }
  ["condition"]=&gt;
  object(SimpleXMLElement)#9 (2) {
    ["conditionId"]=&gt;
    string(4) "4000"
    ["conditionDisplayName"]=&gt;
    string(9) "Very Good"
  }
  ["isMultiVariationListing"]=&gt;
  string(5) "false"
  ["topRatedListing"]=&gt;
  string(4) "true"
}
object(SimpleXMLElement)#21 (17) {
  ["itemId"]=&gt;
  string(12) "291613957092"
  ["title"]=&gt;
  string(69) "The Undefeated/The Comancheros/North to Alaska DVD (2009) John Wayne "
  ["globalId"]=&gt;
  string(7) "EBAY-GB"
  ["primaryCategory"]=&gt;
  object(SimpleXMLElement)#3 (2) {
    ["categoryId"]=&gt;
    string(3) "617"
    ["categoryName"]=&gt;
    string(15) "DVDs &amp; Blu-rays"
  }
  ["galleryURL"]=&gt;
  string(63) "http://thumbs1.ebaystatic.com/m/mzlj2TN7uXKRzBFDHSwOrRg/140.jpg"
  ["viewItemURL"]=&gt;
  string(99) "http://www.ebay.co.uk/itm/Undefeated-The-Comancheros-North-Alaska-DVD-2009-John-Wayne-/291613957092"
  ["productId"]=&gt;
  string(8) "72542901"
  ["paymentMethod"]=&gt;
  string(6) "PayPal"
  ["autoPay"]=&gt;
  string(4) "true"
  ["location"]=&gt;
  string(14) "United Kingdom"
  ["country"]=&gt;
  string(2) "GB"
  ["shippingInfo"]=&gt;
  object(SimpleXMLElement)#22 (3) {
    ["shippingServiceCost"]=&gt;
    string(3) "0.0"
    ["shippingType"]=&gt;
    string(4) "Free"
    ["shipToLocations"]=&gt;
    string(9) "Worldwide"
  }
  ["sellingStatus"]=&gt;
  object(SimpleXMLElement)#23 (4) {
    ["currentPrice"]=&gt;
    string(4) "2.59"
    ["convertedCurrentPrice"]=&gt;
    string(4) "2.59"
    ["sellingState"]=&gt;
    string(6) "Active"
    ["timeLeft"]=&gt;
    string(14) "P24DT16H34M20S"
  }
  ["listingInfo"]=&gt;
  object(SimpleXMLElement)#24 (7) {
    ["bestOfferEnabled"]=&gt;
    string(5) "false"
    ["buyItNowAvailable"]=&gt;
    string(5) "false"
    ["startTime"]=&gt;
    string(24) "2015-11-11T09:31:47.000Z"
    ["endTime"]=&gt;
    string(24) "2018-04-29T09:31:47.000Z"
    ["listingType"]=&gt;
    string(10) "FixedPrice"
    ["gift"]=&gt;
    string(5) "false"
    ["watchCount"]=&gt;
    string(1) "2"
  }
  ["condition"]=&gt;
  object(SimpleXMLElement)#25 (2) {
    ["conditionId"]=&gt;
    string(4) "4000"
    ["conditionDisplayName"]=&gt;
    string(9) "Very Good"
  }
  ["isMultiVariationListing"]=&gt;
  string(5) "false"
  ["topRatedListing"]=&gt;
  string(4) "true"
}
object(SimpleXMLElement)#6 (17) {
  ["itemId"]=&gt;
  string(12) "291359519480"
  ["title"]=&gt;
  string(37) "The Undefeated DVD (2005) John Wayne "
  ["globalId"]=&gt;
  string(7) "EBAY-GB"
  ["primaryCategory"]=&gt;
  object(SimpleXMLElement)#21 (2) {
    ["categoryId"]=&gt;
    string(3) "617"
    ["categoryName"]=&gt;
    string(15) "DVDs &amp; Blu-rays"
  }
  ["galleryURL"]=&gt;
  string(63) "http://thumbs1.ebaystatic.com/m/mZh2rcq8GHicMkTHRVCnxYA/140.jpg"
  ["viewItemURL"]=&gt;
  string(70) "http://www.ebay.co.uk/itm/Undefeated-DVD-2005-John-Wayne-/291359519480"
  ["productId"]=&gt;
  string(8) "45532467"
  ["paymentMethod"]=&gt;
  string(6) "PayPal"
  ["autoPay"]=&gt;
  string(4) "true"
  ["location"]=&gt;
  string(14) "United Kingdom"
  ["country"]=&gt;
  string(2) "GB"
  ["shippingInfo"]=&gt;
  object(SimpleXMLElement)#24 (3) {
    ["shippingServiceCost"]=&gt;
    string(3) "0.0"
    ["shippingType"]=&gt;
    string(4) "Free"
    ["shipToLocations"]=&gt;
    string(9) "Worldwide"
  }
  ["sellingStatus"]=&gt;
  object(SimpleXMLElement)#23 (4) {
    ["currentPrice"]=&gt;
    string(4) "1.53"
    ["convertedCurrentPrice"]=&gt;
    string(4) "1.53"
    ["sellingState"]=&gt;
    string(6) "Active"
    ["timeLeft"]=&gt;
    string(14) "P29DT22H34M48S"
  }
  ["listingInfo"]=&gt;
  object(SimpleXMLElement)#22 (7) {
    ["bestOfferEnabled"]=&gt;
    string(5) "false"
    ["buyItNowAvailable"]=&gt;
    string(5) "false"
    ["startTime"]=&gt;
    string(24) "2015-01-20T15:32:15.000Z"
    ["endTime"]=&gt;
    string(24) "2018-05-04T15:32:15.000Z"
    ["listingType"]=&gt;
    string(10) "FixedPrice"
    ["gift"]=&gt;
    string(5) "false"
    ["watchCount"]=&gt;
    string(1) "2"
  }
  ["condition"]=&gt;
  object(SimpleXMLElement)#3 (2) {
    ["conditionId"]=&gt;
    string(4) "4000"
    ["conditionDisplayName"]=&gt;
    string(9) "Very Good"
  }
  ["isMultiVariationListing"]=&gt;
  string(5) "false"
  ["topRatedListing"]=&gt;
  string(4) "true"
}

<!-- Build the HTML page with values from the call response -->
<title>eBay Search Results for The Undefeated</title>
<style type="text/css">body { font-family: arial,sans-serif;} </style>
<h1>eBay Search Results for The Undefeated</h1>

<table>
<tbody><tr>
  <td>
    </td></tr><tr><td><img src="http://thumbs2.ebaystatic.com/m/mBDsbLQ12qEGzPBLdElHfBw/140.jpg"></td><td><a href="http://www.ebay.co.uk/itm/Comancheros-The-Undefeated-DVD-2003-John-Wayne-/302393938957">The Comancheros/The Undefeated DVD (2003) John Wayne </a></td><td>£1.8</td><td>£1.8</td><td>false</td><td>FixedPrice</td><td>false</td></tr><tr><td><img src="http://thumbs1.ebaystatic.com/m/mzlj2TN7uXKRzBFDHSwOrRg/140.jpg"></td><td><a href="http://www.ebay.co.uk/itm/Undefeated-The-Comancheros-North-Alaska-DVD-2009-John-Wayne-/291613957092">The Undefeated/The Comancheros/North to Alaska DVD (2009) John Wayne </a></td><td>£2.59</td><td>£2.59</td><td>false</td><td>FixedPrice</td><td>false</td></tr><tr><td><img src="http://thumbs1.ebaystatic.com/m/mZh2rcq8GHicMkTHRVCnxYA/140.jpg"></td><td><a href="http://www.ebay.co.uk/itm/Undefeated-DVD-2005-John-Wayne-/291359519480">The Undefeated DVD (2005) John Wayne </a></td><td>£1.53</td><td>£1.53</td><td>false</td><td>FixedPrice</td><td>false</td></tr>  

</tbody></table>

Open in new window


Is your question about sorting this ruturned data or your error you are now talking about?
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
Please click the "Report Question" link in your question.  You will want the Moderators to help you obfuscate your credentials. I shouldn'thave been able to run your script. I also suggest chaning your api credentials too .
0
 
Scott Fell, EE MVEDeveloper & EE ModeratorCommented:
I was able to run your code but if you are getting the streaming error, that suggests the url you are submitting.  
// Load the call and capture the document returned by eBay API
$resp = simplexml_load_file($apicall);

Open in new window


However, it will not be in that line directly and instead the code you used to build $apicall.

If you are going to use the SDK,I suggest starting here http://devbay.net/sdk/guides/
0
 
trevor1940Author Commented:
Hi,
Thanx
If you changed the query to "The Searchers" the return isn't DVD's it's mainly CD's from the groupe

I forgot about XXing the API ID
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.