Getting the cheapest Buy It Nows with Ebay Finding Service API and PHP

October 13, 2012

Prerequistes: cURL and a Ebay Account.

Everyone likes a good deal.

With the Ebay API you can build your own tools that may give you a leg up on all the other deal seekers. Ebay’s API is quite open and easy to use. In some cases the api allows you to get data that the site wouldn’t(or at least not user-friendly enough for me to know how). But one of the most useful things I have found is being able to search listings by a product and filter the results. “But I can do that on the website!”. Sure, but with PHP and this API you don’t have to spend your hours sifting the site. With PHP, we can do any number of things to do find the deals, without spending the time and here is how:

For an example, let’s say I was looking for a deal on a Blu Ray of one of my favorite movies: Memento. For this API I needed the UPC number of this product and a quick google search provided 043396154162. Now that we have the UPC, we can start using the API.

$app_name = YOUR_EBAY_API_APP_NAME;
$upc = '043396154162';
$api_call ="http://svcs.ebay.com/services/search/FindingService/v1?OPERATION-NAME=findItemsByProduct";
$api_call .="&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=".$app_name;
$api_call .="&RESPONSE-DATA-FORMAT=XML&REST-PAYLOAD";
$api_call .="&paginationInput.entriesPerPage=10"; //I only want to see the top ten lowest priced
$api_call .="&productId.@type=UPC&productId=".$upc; //This is telling the API what product you want
$api_call .="&itemFilter(0).name=Condition&itemFilter(0).value(0)=Used"; // Used will be cheapest
$api_call .="&itemFilter(1).name=ListingType&itemFilter(1).value(0)=FixedPrice" //I don't want auctions
$api_call .="&itemFilter(1).value(1)=AuctionWithBIN"; //These have Buy it Now so include them
$api_call .="&sortOrder=PricePlusShippingLowest"; //I want to see the best deals

Now that the request is set up, we can use a simple cURL call to get the xml response.

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $api_call);
curl_setopt($ch, CURLOPT_TIMEOUT, 15);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_ENCODING , "gzip");			
$result = curl_exec($ch);
curl_close($ch);

$deals_object = simplexml_load_string($response);

Your call should come back successful and load $deals_object with all the results. Now let’s see what our top result is. We do need to add some logic to pull the right price depending on the listing type.

$cheapest = $res->searchResult->item[0];

if($cheapest->listingInfo->buyItNowAvailable)
{
  $total = $cheapest->listingInfo->buyItNowPrice + $cheapest->shippingInfo->shippingServiceCost;
  echo 'viewItemUrl.'">Let's buy for $'.$total.'';
}
else
{
  $total = $cheapest->sellingStatus->currentPrice + $cheapest->shippingInfo->shippingServiceCost;
  echo 'viewItemUrl.'">Let's buy for $'.$total.'';
}

This is just a simple piece of code that would give you the url link to the cheapest copy on ebay at the moment. You could make it much more dynamic. I could set it up to only check if there is a purchasable copy for under $2, and if there is to email me the link. Then set that code to reoccur every so often. The possibilities are great and the deals to be found are even greater.

Stay in Touch!

Subscribe to our newsletter.

Solutions Architecture

browse through our blog articles

Blog Archive