Coding For Online Success

Using eBay’s API to search auctions and return results


Some of you have are familiar with eBay’s RSS feeds to return auctions based off of ‘static’ search, meaning it’s a search that you’ve performed and a search that will return the auctions based off of the same search criteria. Let’s say you want to let visitors to your site enter search terms have a auctions returned directly within your site.

To do this you’ll need to use eBay’s API. This is more advanced than the other tools that eBay provides (RSS, Editor Kit, Flexible Destination Tool), but is not too bad. As I’ve stated earlier, I primarily code in PHP/MySQL, so these examples will be geared towards that language.

The eBay API works like any other web service. You craft a XML document that has certain parameters that the API expects, including the search term. The API takes this XML and executes the commands on their server and returns you a XML file containing the results. You then have to parse the XML file to present the search results. Now, to the novice, this will sound daunting, but it’s not too bad, just take the piece slow.

To communicate with eBay’s API, we must have a few things in place first:
– Must be a member of eBay’s Affiliate Program (You must have a valid eBay account first though, you can get one here).

– Next you need to join eBay’s developer’s program. You can join here.

After joining everything, you’ll need to create keys. These keys will authenticate your site to the eBay API. You can generate keys here:

Next, you’ll need to use the key to generate a authentication token:

Okay, enough of the setup stuff already! We’re ready to put some code together now. There are a few variables that were created when you created your keys (devID, appID and certID). The authentication token will be called userToken.

Let’s pick up where we left off on the last post about capturing search data. Let’s reuse the search searchResults.php file we created to capture and store search data. But now, let’s send the search criteria to eBay to return auction results.

This is where we were at on the search capture:

if(!isset($_POST["Criteria"]) || strlen(trim($_POST["Criteria"])) == 0){
if(strlen(trim($_POST["Criteria"])) < 5){
echo "Search Criteria is too short";
$_POST["Criteria"] = trim(htmlspecialchars(strip_tags(addslashes($_POST["Criteria"]))));
if(strlen(trim($_POST["Criteria"])) == 0){
$monthYear = date(‘my’);
$sql = "UPDATE tblSearch
SET Count = Count + 1
WHERE Criteria = ‘"
AND MonthYear = ‘"
trigger_error(sprintf("SQL Error: %d: %s\n" , mysql_errno(), mysql_error()), E_USER_ERROR);
$sql = "INSERT INTO tblSearch(Criteria, MonthYear, Count)
trigger_error(sprintf("SQL Error: %d: %s\n" , mysql_errno(), mysql_error()), E_USER_ERROR);

Now let’s add our connection to eBay!

//set the Server to use (Sandbox or Production)
//$serverUrl = ‘’;
$serverUrl = ‘’;
//the token representing the eBay user to assign the call with
$userToken = ‘AgAAAA**AQAAAA**aAAAAA**Bukb….thisWillBeAGiantString…..3+’;

Here we’re going to add our variables and credentials to talk to eBay’s API. You’ll notice that I have two serverUrl variables set. One is commented and one is not. You should do your testing on the Sandbox Url first, then when you’re good, switch to the production Url. Currently the sandbox Url is commented. This is really important when ‘setting’ values with the API (ie: submitting auctions, etc.). The API is EXTREMELY powerful.

You can find all the information you need about the eBay API here: (you must be logged in to view the docs). They also have a support forum as well.

Next, we’re going to add a few more variables about what we want to do and where we’re coming from:

$siteID                         = 0;
$verb                           = ‘GetSearchResults’;
$compatabilityLevel = 433;
$query                          = $_POST["Criteria"];

The GetSearchResults is the ‘verb’ that tells the API what we want to do. SiteID of 0 is US, Not sure about the compatibilityLevel, and the query is the search string that we want to search on.

Now we need to assemble our request XML

$headers                        = buildEbayHeaders($devID, $appID, $certID, $compatabilityLevel, $siteID, $verb);
$requestXmlBody         = ‘<?xml version="1.0" encoding="utf-8" ?>’;//<?php
$requestXmlBody         .= ‘<GetSearchResultsRequest xmlns="urn:ebay:apis:eBLBaseComponents">’;
$requestXmlBody         .= "<RequesterCredentials> <eBayAuthToken>$userToken</eBayAuthToken> </RequesterCredentials>";
//$requestXmlBody               .= "<CategoryID>11700</CategoryID>";
$requestXmlBody         .= "<Pagination>";
$requestXmlBody         .= "    <EntriesPerPage>25</EntriesPerPage>";
$requestXmlBody         .= "    <PageNumber>1</PageNumber>";
$requestXmlBody         .= "</Pagination>";
$requestXmlBody         .= "<Query>$query</Query>";
$requestXmlBody         .= ‘</GetSearchResultsRequest>’;
$requestXmlBody         .= ‘<GetSearchResultsRequest xmlns="urn:ebay:apis:eBLBaseComponents">’;
$requestXmlBody         .= "<RequesterCredentials> <eBayAuthToken>$userToken</eBayAuthToken> </RequesterCredentials>";
$requestXmlBody         .= "<Query>$query</Query>";
$requestXmlBody         .= ‘</GetSearchResultsRequest>’;
$setOneEmpty            = true;
$setOneError            = false;
$responseXml            = sendHttpRequest($requestXmlBody, $serverUrl, $headers);

Here we’re including a script that has some functions that we need buildEbayHeaders() and sendHttpRequest(). These function basically form the XML headers and send the request. After we have the XML headers created, we add the XML data where we have search paramters. You can search on a specific category or categories. You can return pages of data, there are many options that you can apply to narrow the search or adjust how this functions.

Here is everything you’ll need to know about GetSearchResults:

Contents of inc_EbayFunctions.php

          * AUTHOR: Michael Hawthornthwaite – Acid Computer Services ( *

        /**     sendHttpRequest
                Sends a HTTP request to the specified server with the body and headers passed
                Input:  $requestBody
                Output: The HTTP Response as a String

        function sendHttpRequest($requestBody, $serverUrl, $headers)
                //initialise a CURL session
                $connection = curl_init();
                //set the server we are using (could be Sandbox or Production server)
                curl_setopt($connection, CURLOPT_URL, $serverUrl);
                //stop CURL from verifying the peer’s certificate
                curl_setopt($connection, CURLOPT_SSL_VERIFYPEER, 0);
                curl_setopt($connection, CURLOPT_SSL_VERIFYHOST, 0);
                //set the headers using the array of headers
                curl_setopt($connection, CURLOPT_HTTPHEADER, $headers);
                //set method as POST
                curl_setopt($connection, CURLOPT_POST, 1);
                //set the XML body of the request
                curl_setopt($connection, CURLOPT_POSTFIELDS, $requestBody);
                //set it to return the transfer as a string from curl_exec
                curl_setopt($connection, CURLOPT_RETURNTRANSFER, 1);
                //Send the Request
                $response = curl_exec($connection);
                //close the connection
                //return the response
                return $response;
        /**     buildEbayHeaders
                Generates an array of string to be used as the headers for the HTTP request to eBay
                Input:  $developerID
                Output: String Array of Headers

        function buildEbayHeaders($developerID, $applicationID, $certificateID, $compatLevel, $siteID, $verb)
                $headers = array (
                        //Regulates versioning of the XML interface for the API
                        "X-EBAY-API-COMPATIBILITY-LEVEL: $compatLevel",
                        //set the keys
                        "X-EBAY-API-DEV-NAME: $developerID",
                        "X-EBAY-API-APP-NAME: $applicationID",
                        "X-EBAY-API-CERT-NAME: $certificateID",
                        //te name of the call we are requesting
                        "X-EBAY-API-CALL-NAME: $verb",                 
                        //SiteID must also be set in the Request’s XML
                        //SiteID = 0  (US) – UK = 3, Canada = 2, Australia = 15, ….
                        //SiteID Indicates the eBay site to associate the call with
                        "X-EBAY-API-SITEID: $siteID",
                return $headers;

Okay, so we have the ‘question’ to ask eBay, and we sent the request. What do we do with the response? Here ya go:

if(stristr($responseXml, ‘HTTP 404’) || $responseXml == )
        trigger_error("Error sending request", E_USER_ERROR);
$responseDoc = domxml_open_mem($responseXml);
$errors = $responseDoc->get_elements_by_tagname(‘Errors’);

if(count($errors) > 0){
        $code = $errors[0]->get_elements_by_tagname(‘ErrorCode’);
        $shortMsg = $errors[0]->get_elements_by_tagname(‘ShortMessage’);
        $longMsg = $errors[0]->get_elements_by_tagname(‘LongMessage’);
        $msg                            = "";
        $msg                            .= $code[0]->get_content(). ‘ : ‘. str_replace(">", ">", str_replace("<", "<", $shortMsg[0]->get_content()));
        if(count($longMsg) > 0){
                $msg                    .= str_replace(">", ">", str_replace("<", "<", $longMsg[0]->get_content()));
        trigger_error($msg, E_USER_ERROR);
        $itemNodes = $responseDoc->get_elements_by_tagname(‘Item’);

We check to see if we have a responseXml from eBay. If there were errors we want to see it and then stop page execution. Again, I’m using custom error handling, so you could echo the message and exit(); while in development. The final $itemNodes is a array of our auction results. I want to go through this array and output them so they appear similar to my RSS output on other pages.

Here is how I loop through that result set:

<div id="head">
        <h3>Search Results</h3>
<div id="box"><?php
if(count($itemNodes) > 0){
        echo "<strong>Your search returned ".count($itemNodes)." results for \"".$_POST["Criteria"]."\"</strong><br><br>";
        foreach($itemNodes as $item){
                //get the required nodes from the results
                $itemID                         = $item->get_elements_by_tagname(‘ItemID’);
                $title                          = $item->get_elements_by_tagname(‘Title’);
                $price                          = $item->get_elements_by_tagname(‘CurrentPrice’);
                $startTime                      = $item->get_elements_by_tagname(‘StartTime’);
                $bidCount                       = $item->get_elements_by_tagname(‘BidCount’);
                $endTime                        = $item->get_elements_by_tagname(‘EndTime’);
                $buyItNow                       = $item->get_elements_by_tagname(‘BuyItNowPrice’);
                $endTime                        = ebayDate($endTime[0]->get_content());
                $link                           = $item->get_elements_by_tagname(‘ViewItemURL’);
                $image                          = $item->get_elements_by_tagname(‘GalleryType’);
                $imageUrl                       = "".$itemID[0]->get_content()."_0.jpg";
                $itemID                         = $itemID[0]->get_content();?>
                <a rel="nofollow" class="auction" target="_blank" href="<?=$itemID?>/"><?=$title[0]->get_content();?></a><br>
                <table border="0" cellpadding="8">
                        <a rel="nofollow" class="auction" target="_blank" href="<?=$itemID?>/"><img width=80 hspace=5 vspace=5 border="0" src="<?=$imageUrl?>"></a>
                        <td><strong>US $<?=number_format($price[0]->get_content(),2);?></strong> <?if(isset($bidCount)){?>(<?=$bidCount[0]->get_content()?> Bid<?if($bidCount[0]->get_content() != ‘1’){echo ‘s’;}?>)<?}?>
                                echo "<span class=\"red\"><strong><u>Buy It Now: $".number_format($buyItNow[0]->get_content(),2)."</u></strong></span><br>";
                        End Date: <?=$endTime?>

                        <a rel="nofollow" class="auction" target="_blank" href="<?=$itemID?>/">Bid now</a> | <a rel="nofollow" class="auction" target="_blank" href="<?=$itemID?>/">Add to watch list</a>
                <div id="line"></div>
        echo "Sorry, no results were found for your search. Please check your spelling.<br><br><br>";

This creates tables similar to what is returned from the RSS feeds to maintain consistency of the auction results throughout my site. You’ll need to work on method of adding your rover eBay code to direct back to eBay containing your PID. I have a special way of doing it.. and will discuss that later down the road. But for now you could try something like this:”.$itemID

So let’s recap…

We built a search box for your website, when a visitor submits their search string, we capture it for good data down the road, then we send it to eBay and return the results and display them similar to our RSS output. It’s great to capture this search data, because we can create new pages down the road (ie: Popular Searches) or more importantly showcase those search items on web page, focus our SEO energy using those keywords, etc.

Ping me if you have any questions or have troubles getting this to work!


  1. Love the code – having a fiddle now. One question is that ebaydate() is not defined anywhere – do you have a snippet to auto convert what they send back?


  2. I’m not liking this function.. and will probably re-write it now.. but this is what I have in place.

    < ?php function getShortDate($stringDate){ if($stringDate == "") return ""; else{ $splitDate = explode(" " , $stringDate); $dateArray = explode("-", $splitDate[0]); $dateYear = $dateArray[0]; $dateMonth = $dateArray[1]; $dateDay = $dateArray[2]; $formattedDate = $dateMonth . "/" . $dateDay . "/" . $dateYear; unset($splitDate); unset($dateArray); return $formattedDate; } } function ebayDate($inStringDate){ $dateArr = explode("T",$inStringDate); $date = getShortDate($dateArr[0]); $dateSplit = explode("-",$dateArr[0]); $month = $dateSplit[1]; $year = $dateSplit[0]; $day = $dateSplit[2]; $timeArr = explode(":",rtrim(rtrim($dateArr[1],"000Z"),'.')); $hour = $timeArr[0]; $minute = $timeArr[1]; $second = $timeArr[2]; unset($dateArr); unset($dateSplit); unset($timeArr); return date("m/d/y H:i:s", gmmktime($hour -1 , $minute, $second, $month, $day, $year))." PDT"; } ?>

Leave a Reply

Required fields are marked *.

CommentLuv badge

This site uses Akismet to reduce spam. Learn how your comment data is processed.