Merchandising API
 
Creating a PHP Application with the Merchandising and Shopping APIs

Follow this tutorial to build an application that uses both the eBay Merchandising and Shopping APIs. The tutorial shows how to use the output of one call as input for another. As you complete this tutorial, you will learn the differences between the Merchandising API and other eBay APIs.

Running application

What's in this Tutorial

This tutorial contains the following sections:

Back to top

Complete Source Code

The completed application code is provided as a downloadable ZIP file, Sample_gmwi_gsi_grci_NV_XML.zip. You must replace instances of "YourAppID" in the tutorial code with your production AppID.

The sample code was built and tested with PHP 5.2.1 on Apache 2.2.4.

Back to top

Before You Begin

All that's required to complete this tutorial is an AppID. If you are already a member of the eBay Developers Program, go to your My Account page to generate and/or retrieve your production AppID. If you aren't a member, Join Now. Joining is free, and you get 5,000 API calls a day just for joining!

If you are new to eBay API, we suggest you start with Getting Started with Search in the eBay Shopping API, which is a very short, simple PHP tutorial to get you started.


Back to top

Step 1: Create a Function for the getMostWatchedItems Call

In this step, you will create a basic PHP application with a getMostWatchedItemsResults function. The function makes a getMostWatchedItems call and displays the results in a table.

To build a simple PHP application:

  1. Start the file by adding the following HTML code.

    Create a new text file and add the following code. You will be adding PHP code to the body of this HTML.

    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
    <title>Merchandising Tutorial Sample</title>
    <style type="text/css">body { font-family: arial,sans-serif; font-size: small; } </style>
    </head>
    <body>
    
    </body>
    </html>
    

  2. Add the following PHP code to the BODY of the HTML to set error checking and define some global variables.

    The $m_endpoint variable specifies the production service endpoint for the Merchandising API. This endpoint is unique to the Merchandising API. You will need to set $appid to your production AppID. In this application, we'll use SimpleXML to parse the call responses, so we set response encoding to XML.

    ...
    </head>
    <body>
    
    <?php
      // Turn on all errors, warnings and notices for easier PHP debugging
      error_reporting(E_ALL);
    
      // Define global variables
      $m_endpoint = 'http://svcs.ebay.com/MerchandisingService?';  // Merchandising URL to call
      $appid = 'YourAppID';  // You will need to supply your own AppID
      $responseEncoding = 'XML';  // Type of response we want back
    
    ?>
    
    </body>
    </html>
    
  3. Add the getMostWantedItemsResults function after the global variables.

    The function will contain the code that makes the API call, parses the results, and builds a return response that can be displayed on a web page. The arguments, $selectedItemID and $cellColor, will be defined later in the tutorial.

      ...
      // Define global variables and settings
      $m_endpoint = 'http://svcs.ebay.com/MerchandisingService?';  // Merchandising URL to call
      $appid = 'YourAppID';  // You will need to supply your own AppID
      $responseEncoding = 'XML';  // Type of response we want back
    
      // Create a function for the getMostWatchedItems call
      function getMostWatchedItemsResults ($selectedItemID = '', $cellColor = '') {
    
      } // End of getMostWatchedItemsResults function
    
    ?>
    
    </body>
    </html>
    
  4. To the function, add the global variables.

    These variables were defined earlier. At this point, all of the defined global variables are used by the getMostWatchedItemsResults function, but you will be adding more global variables later in the tutorial for use by other functions.

      ...
      // Create a function for the getMostWatchedItems call
      function getMostWatchedItemsResults ($selectedItemID = '', $cellColor = '') {
        global $m_endpoint;
        global $appid;
        global $responseEncoding;
    
      } // End of getMostWatchedItemsResults function
    
    ?>
    
    </body>
    </html>
    
  5. Add the code to build and load the call.

    This applicaiton makes a URL format call. Note the URL parameters are different from those used with the eBay Shopping API. Note also that the call input fields, maxResults and categoryId, begin with lowercase letters. The call response is set to $resp.

      ...
      // Create a function for the getMostWatchedItems call
      function getMostWatchedItemsResults ($selectedItemID = '', $cellColor = '') {
        global $m_endpoint;
        global $appid;
        global $responseEncoding;
    
        // Construct getMostWatchedItems call with maxResults and categoryId as input
        $apicalla  = "$m_endpoint";
        $apicalla .= "OPERATION-NAME=getMostWatchedItems";
        $apicalla .= "&SERVICE-VERSION=1.0.0";
        $apicalla .= "&CONSUMER-ID=$appid";
        $apicalla .= "&RESPONSE-DATA-FORMAT=$responseEncoding";
        $apicalla .= "&maxResults=3";
        $apicalla .= "&categoryId=293";
    
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicalla);
    
      } // End of getMostWatchedItemsResults function
    
    ?>
    
    </body>
    </html>
    

    The following table lists the parameters used in the call. Each of the URL parameters and input fields are unique to the Merchandising API.

    Input Parameter Value Description
    URL Parameters
    OPERATION-NAME getMostWatchedItems The name of the call being submitted.
    SERVICE-VERSION 1.0.0 The version of the Merchandising Web Service.
    CONSUMER-ID $appid This value is a global variable that you must set to your production AppID. Although this URL parameter is different from the URL parameter used to specify your AppID in the Shopping API (&appid), the same AppID will be used for both.
    RESPONSE-DATA-FORMAT $responseEncoding This value is a global variable set to XML. This specifies that the call response be returned in XML format.
    Call Input Fields
    maxResults 3 This is the maximum number of items returned in the response. Because the call response is based on eBay buyer activity, it is possible for some categories that the number items returned will be fewer than the specified maximum. you specify and results can vary from category to category.
    categoryId 293 This is the category ID for the category from which you want to retrieve most watched items. This ID corresponds to the Consumer Electronics category on the eBay US site.
  6. Add code to confirm a response was returned by eBay.

    The following code confirms the response ($resp) is not null, otherwise an error is printed.

        ...
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicalla);
    
        // Check to see if the response was loaded, else print an error
        if ($resp) {
          // Set return value for the function to null
          $retna = '';
    
        } else {
          // If there was no response, print an error
          $retna = "Dang! Must not have got the getMostWatchedItems response!<br>";
          $retna .= "Call used was: $apicalla";
        }  // End if response exists
    
        // Return the function's value
        return $retna;
    
      } // End of getMostWatchedItemsResults function
    
    ?>
    
    </body>
    </html>
    
  7. Verify whether the call was successful, and if so, construct the HTML to display data from the response.

    If there is a call response, the following code checks the call response for errors. If there are no errors, the following code creates a heading and a table to display selected information from the call response. Note the following code also checks for which price is returned. If the call request had errors, an error is printed.

        ...
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicalla);
    
        // Check to see if the response was loaded, else print an error
        if ($resp) {
          // Set return value for the function to null
          $retna = '';
    
        // Verify whether call was successful
        if ($resp->ack == "Success") {
    
          // If there were no errors, build the return response for the function
          $retna .= "<h1>Top 3 Most Watched Items in the ";
          $retna .=  $resp->itemRecommendations->item->primaryCategoryName;
          $retna .= " Category</h1> \n";
    
          // Build a table for the 3 most watched items
          $retna .= "<!-- start table in getMostWatchedItemsResults --> \n";
          $retna .= "<table width=\"100%\" cellpadding=\"5\" border=\"0\"><tr> \n";
    
          // For each item node, build a table cell and append it to $retna
          foreach($resp->itemRecommendations->item as $item) {
    
            // Determine which price to display
            if ($item->currentPrice) {
            $price = $item->currentPrice;
            } else {
            $price = $item->buyItNowPrice;
            }
    
            // For each item, create a cell with imageURL, viewItemURL, watchCount, currentPrice
            $retna .= "<td $thisCellColor valign=\"bottom\"> \n";
            $retna .= "<img src=\"$item->imageURL\"> \n";
            $retna .= "<p><a href=\"" . $item->viewItemURL . "\">" . $item->title . "</a></p>\n";
            $retna .= 'Watch count: <b>' . $item->watchCount . "</b><br> \n";
            $retna .= 'Current price: <b>$' . $price . "</b><br><br> \n";
            $retna .= "</td> \n";
          }
          $retna .= "</tr></table> \n<!-- finish table in getMostWatchedItemsResults --> \n";
    
          } else {
              // If there were errors, print an error
              $retna = "The response contains errors<br>";
              $retna .= "Call used was: $apicalla";
    
        }  // if errors
    
        } else {
          // If there was no response, print an error
          $retna = "Dang! Must not have got the getMostWatchedItems response!<br>";
          $retna .= "Call used was: $apicalla";
        }  // End if response exists
    
        // Return the function's value
        return $retna;
    
      } // End of getMostWatchedItemsResults function
    
    ?>
    
    </body>
    </html>
    
  8. Add the code to display the results.

    The following code prints the return reponse (the value of $retna) for the getMostWatchedItemsResults function.

        ...
        // Return the function's value
        return $retna;
    
      } // End of getMostWatchedItemsResults function
    
        // Display the response data
        print getMostWatchedItemsResults('', '');
    
    ?>
    
    </body>
    </html>
    
  9. Save the file as a PHP file and run it on your PHP-enabled server.

    At this point, the application displays the top three most watched items from the Consumer Electronics category:

    Running application (Step 1)


Back to top

Step 2: Add Form Elements to getMostWatchedItemsResults and Create a Function for GetSingleItem

In this step you will create a second function, getSingleItemResults, which makes a GetSingleItem call from the eBay Shopping API, and displays the results in a table. This function uses output from the getMostWatchedItems call in the previous function as input. You will add form elements to the getMostWatchedItemsResults function to select a most watched item and store its associated item ID.

You will also add a getPrettyTimeFromEbayTime function to format the timeLeft value to be more easily read.

  1. Add a global variable to set the service endpoint for the Shopping API.

    The service endpoint used with the Shopping API (http://open.api.ebay.com/shopping?) is different from the service endpoint you specified for the Merchandising API (http://svcs.ebay.com/MerchandisingService?). You will be adding a new function that makes a Shopping API call. The $cellColor variable will be used to highlight selected items in the most watched items table.

    ...
    <?php
      // turn on all errors, warnings and notices for easier debugging
      error_reporting(E_ALL);
    
      // Define global variables and settings
      $s_endpoint = "http://open.api.ebay.com/shopping?";  // Shopping URL to call
      $cellColor = "bgcolor=\"#dfefff\"";  // Light blue background used for selected items 
      $m_endpoint = "http://svcs.ebay.com/MerchandisingService";  // Merchandising URL to call
      $appid = "YourAppID";  // You will need to supply your own AppID
      $responseEncoding = "XML";  // Type of response we want back
      ...
    
  2. Add code to format the table cell for the selected most watched item.

    The added code in bold checks each item to in the most watched items table to see if the item ID matches the $selectedItemID variable. When there is a match, the background color for the table cell containing the selected item to light blue.

          ...
          // For each item node, build a table cell and append it to $retna
          foreach($resp->itemRecommendations->item as $item) {
    
            // Set the cell color blue for the selected most watched item
            if ($selectedItemID == $item->itemId) {
              $thisCellColor = $cellColor;
            } else {
              $thisCellColor = '';
            }
    
            // Determine which price to display
            if ($item->currentPrice) {
            $price = $item->currentPrice;
            } else {
            $price = $item->buyItNowPrice;
            }
            ...
    
  3. Add form elements to the getMostWatchedItemsResults function to select an item.

    These form elements add a button to each cell of the most watched items table. When you click the button, it records the item ID for the item displayed in the cell.

            ...
            // For each item, create a cell with imageURL, viewItemURL, watchCount, currentPrice
            $retna .= "<td $thisCellColor valign=\"bottom\"> \n";
            $retna .= "<img src=\"$item->imageURL\"> \n";
            $retna .= "<p><a href=\"" . $item->viewItemURL . "\">" . $item->title . "</a></p>\n";
            $retna .= 'Watch count: <b>' . $item->watchCount . "</b><br> \n";
            $retna .= 'Current price: <b>$' . $price . "</b><br><br> \n";
            $retna .= "<FORM ACTION=\"" . $_SERVER['PHP_SELF'] . "\" METHOD=\"POST\"> \n";
            $retna .= "<INPUT TYPE=\"hidden\" NAME=\"Selection\" VALUE=\"$item->itemId\"> \n";
            $retna .= "<INPUT TYPE=\"submit\" NAME=\"$item->itemId\" ";
            $retna .= "VALUE=\"Get Details and Related Category Items\"> \n";
            $retna .= "</FORM> \n";
            $retna .= "</td> \n";
            ...
    
  4. Add a getSingleItemResults function.

    This function will make a GetSingleItem call using the item ID captured by the form elements in the most watched items table and display additional item details for the selected item.

        ...
        } else {
          // If there was no response, print an error
          $retna = "Dang! Must not have got the getMostWatchedItems response!";
        }  // if $resp
    
        // Return the function's value
        return $retna;
    
      } // End of getMostWatchedItemsResults function
    
      // Use itemId from selected most watched item as input for a GetSingleItem call
      function getSingleItemResults ($selectedItemID) {
    
      } // End of getSingleItemResults function
    
      // Display the response data
        print getMostWatchedItemsResults('', '');
    
    ?>
    
    </body>
    </html>
    
  5. Add the global variables used by the function.

    Note that you can use the same AppID (see the $appid variable) for Shopping API and Merchandising API calls.

      ...
      // Use itemId from selected most watched item as input for a GetSingleItem call
      function getSingleItemResults ($selectedItemID) {
        global $s_endpoint;
        global $appid;
        global $responseEncoding;
        global $cellColor;
    
        $retnb  = '';
    
        return $retnb;
    
      } // End of getSingleItemResults function
      ...
    
  6. Add the code to construct and load the GetSingleItem call.

    The Shopping call uses different URL parameters and input parameters than Merchandising calls, but the basic structure is similar.

      ...
      // Use itemId from selected most watched item as input for a GetSingleItem call
      function getSingleItemResults ($selectedItemID) {
        global $s_endpoint;
        global $appid;
        global $responseEncoding;
        global $cellColor;
    
        $retnb  = '';
    
        // Construct the GetSingleItem call
        $apicallb  = "$s_endpoint";
        $apicallb .= "callname=GetSingleItem";
        $apicallb .= "&version=563";
        $apicallb .= "&appid=$appid";
        $apicallb .= "&itemid=$selectedItemID";
        $apicallb .= "&responseencoding=$responseEncoding";
        $apicallb .= "&includeselector=Details,ShippingCosts";
    
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicallb);
    
        // Check to see if the response was loaded, else print an error
        if ($resp) {
    
        } else {
        // If there was no response, print an error
        $retnb = "Dang! Must not have got the GetSingleItem response!";
        }  // if $resp
    
        return $retnb;
    
      } // End of getSingleItemResults function
      ...
    
  7. Add the code to parse the response and build the HTML to display the data.

    Since you specified XML response encoding for the GetSingleItem call, the code to parse and build a response is very similar to what we did in the getMostWatchedItemsResults function.

      ...
      // Use itemId from selected most watched item as input for a GetSingleItem call
      function getSingleItemResults ($selectedItemID) {
        global $localTimeZone;
        global $s_endpoint;
        global $appid;
        global $responseEncoding;
        global $cellColor;
    
        $retnb  = '';
    
        // Construct the GetSingleItem call
        $apicallb  = "$s_endpoint";
        $apicallb .= "callname=GetSingleItem";
        $apicallb .= "&version=563";
        $apicallb .= "&appid=$appid";
        $apicallb .= "&itemid=$selectedItemID";
        $apicallb .= "&responseencoding=$responseEncoding";
        $apicallb .= "&includeselector=Details,ShippingCosts";
    
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicallb);
    
        // Check to see if the response was loaded, else print an error
        if ($resp) {
    
           // If there is a response check for a picture of the item to display
          if ($resp->Item->PictureURL) {
          $picURL = $resp->Item->PictureURL;
          } else {
          $picURL = "http://pics.ebaystatic.com/aw/pics/express/icons/iconPlaceholder_96x96.gif";
          }
    
          // Check for shipping cost information
          if ($resp->Item->ShippingCostSummary->ShippingServiceCost) {
          $shippingCost = "\$" . $resp->Item->ShippingCostSummary->ShippingServiceCost;
          } else {
          $shippingCost = "Not Specified";
          }
    
          // Build a table of item and user details for the selected most watched item
          $retnb .= "<!-- start table in getSingleItemResults --> \n";
          $retnb .= "<table width=\"100%\" cellpadding=\"5\"><tr> \n";
          $retnb .= "<td $cellColor width=\"50%\">\n";
          $retnb .= "<div align=\"left\"> <!-- left align item details --> \n";
          $retnb .= "Current price: <b>\$" . $resp->Item->ConvertedCurrentPrice . "</b><br> \n";
          $retnb .= "Shipping cost: <b>" . $shippingCost . "</b><br>\n";
          $retnb .= "Time left: <b>" . getPrettyTimeFromEbayTime($resp->Item->TimeLeft) . "</b><br> \n";
          $retnb .= "</div></td> \n";
          $retnb .= "<td $cellColor><div align=\"left\"> <!-- left align item details --> \n";
          $retnb .= "Seller ID: <b>" . $resp->Item->Seller->UserID . "</b><br> \n";
          $retnb .= "Feedback score: <b>" . $resp->Item->Seller->FeedbackScore . "</b><br> \n";
          $retnb .= "Positive Feedback: <b>" . $resp->Item->Seller->PositiveFeedbackPercent . "</b><br>\n";
          $retnb .= "</div></td></tr></table> \n<!-- finish table in getSingleItemResults --> \n"; 
    
        } else {
        // If there was no response, print an error
        $retnb = "Dang! Must not have got the GetSingleItem response!";
        }  // if $resp
    
        return $retnb;
    
      } // End of getSingleItemResults function
      ...
    
  8. Add functions to make the eBay time values pretty.

    GetSingleItem returns a duration value for the time left before an item ends (Item.TimeLeft) in ISO 8601 format (e.g., P2DT23H32M51S). These two functions convert the TimeLeft value into a more readable format, such as "2 days 23 hours 32 minutes 51 seconds."

        ...
        return $retnb;
      } // function getSingleItemResults
    
      // Make returned eBay times pretty
      function getPrettyTimeFromEbayTime($eBayTimeString){
        // Input is of form 'PT12M25S'
        $matchAry = array(); // null out array which will be filled
        $pattern = "#P([0-9]{0,3}D)?T([0-9]?[0-9]H)?([0-9]?[0-9]M)?([0-9]?[0-9]S)#msiU";
        preg_match($pattern, $eBayTimeString, $matchAry);
    
        $days  = (int) $matchAry[1];
        $hours = (int) $matchAry[2];
        $min   = (int) $matchAry[3];  // $matchAry[3] is of form 55M - cast to int
        $sec   = (int) $matchAry[4];
    
        $retnStr = '';
        if ($days)  { $retnStr .= " $days day"   . pluralS($days);  }
        if ($hours) { $retnStr .= " $hours hour" . pluralS($hours); }
        if ($min)   { $retnStr .= " $min minute" . pluralS($min);   }
        if ($sec)   { $retnStr .= " $sec second" . pluralS($sec);   }
    
        return $retnStr;
      } // function
    
      function pluralS($intIn) {
        // if $intIn > 1 return an 's', else return null string
        if ($intIn > 1) {
          return 's';
        } else {
          return '';
        }
      } // function
    
        // Display the response data
        print getMostWatchedItemsResults('', '');
    
    ?>
    
    </body>
    </html>
    
  9. Add code to display the getSingleItemResults return value when a most watched item is selected.

    The code in bold typeface replaces the code to print just the return value for the getMostWatchedItemsResults function. When an item selection is made with a button (i.e., if (isset($_POST['Selection']))), the return value of getSingleItemResults function, a table of item and seller details, is also printed.

      ...
      function pluralS($intIn) {
        // if $intIn > 1 return an 's', else return null string
        if ($intIn > 1) {
          return 's';
        } else {
          return '';
        }
      } // function
    
    
      // Display the response data
      // If button clicked for most watched item, display details and related category items
      if (isset($_POST['Selection']))  {
        $selectedItemID = $_POST['Selection'];
        print getMostWatchedItemsResults($selectedItemID, $cellColor);
        print getSingleItemResults($selectedItemID);
      } else {
      // If button not clicked, show only most watched items
        print getMostWatchedItemsResults('', '');
      }
    
    ?>
    
    </body>
    </html>
    
  10. Save the file as a PHP file and run it on your PHP-enabled server.

    At this point, the application displays the top three most watched items from the Consumer Electronics category. Each item has an associated button. When the button is clicked, additional details are displayed for that item.

    Running application (Step 2)

Back to top

Step 3: Create a Function for getRelatedCategoryItems

In this step, you will add a getRelatedCategoryItemsResults function, which makes a getRelatedCategoryItems call from the Merchandising API. Like the getSingleItemResults function, this function uses the item ID for the selected most watched item as input.

  1. Add a getRelatedCategoryItemsResults function.

    This function will contain the code that makes a getRelatedCategoryItems call, parses the response, and builds a return value in the form of an HTML table.

        ...
        return $retnb;
      } // End of getSingleItemResults function
    
    
      // Use itemId from selected most watched item as input for a getRelatedCategoryItems call
      function getRelatedCategoryItemsResults ($selectedItemID) {
    
      }  // End of getRelatedCategoryItemsResults function
    
      // Make returned eBay times pretty
      function getPrettyTimeFromEbayTime($eBayTimeString){
      ...
    
  2. Add the global variables used by the function.

    This function uses the same variables as the getMostWatchedItemsResults function.

        ...
        return $retnb;
      } // End of getSingleItemResults function
    
    
      // Use itemId from selected most watched item as input for a getRelatedCategoryItems call
      function getRelatedCategoryItemsResults ($selectedItemID) {
        global $m_endpoint;
        global $appid;
        global $responseEncoding;
    
      }  // End of getRelatedCategoryItemsResults function
    
    
      // Make returned eBay times pretty
      function getPrettyTimeFromEbayTime($eBayTimeString){
      ...
    
  3. Add the code to construct and load the getRelatedCategoryItems call.

    Again, the construction of the API call is similar to that used for the other two calls. Compared to the getMostWatchedItems call, this call has a different operation name and uses an item ID of the selected item ($selectedItemID) as input.

        ...
        return $retnb;
      } // End of getSingleItemResults function
    
    
      // Use itemId from selected most watched item as input for a getRelatedCategoryItems call
      function getRelatedCategoryItemsResults ($selectedItemID) {
        global $m_endpoint;
        global $appid;
        global $responseEncoding;
    
        // Construct the getRelatedCategoryItems call
        $apicallc  = "$m_endpoint";
        $apicallc .= "OPERATION-NAME=getRelatedCategoryItems";
        $apicallc .= "&SERVICE-VERSION=1.0.0";
        $apicallc .= "&CONSUMER-ID=$appid";
        $apicallc .= "&RESPONSE-DATA-FORMAT=$responseEncoding";
        $apicallc .= "&maxResults=3";
        $apicallc .= "&itemId=$selectedItemID";
    
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicallc);
    
        // Check to see if the response was loaded, else print an error
        if ($resp) {
    
          $retnc = '';
    
        } else {
          // If there was no response, print an error
          $retnc = "Dang! Must not have got the getRelatedCategoryItems response! <br> $apicallc";
        }  // if $resp
    
        return $retnc;
    
      }  // End of getRelatedCategoryItemsResults function
    
    
      // Make returned eBay times pretty
      function getPrettyTimeFromEbayTime($eBayTimeString){
    
  4. Add the code to parse the response and build the HTML to display the data.

    Again, this is very similar to what you did with the other two API calls.

        ...
        return $retnb;
    
      } // End of getSingleItemResults function
    
    
      // Use itemId from selected most watched item as input for a getRelatedCategoryItems call
      function getRelatedCategoryItemsResults ($selectedItemID) {
        global $m_endpoint;
        global $appid;
        global $responseEncoding;
    
        // Construct the getRelatedCategoryItems call
        $apicallc  = "$m_endpoint";
        $apicallc .= "OPERATION-NAME=getRelatedCategoryItems";
        $apicallc .= "&SERVICE-VERSION=1.0.0";
        $apicallc .= "&CONSUMER-ID=$appid";
        $apicallc .= "&RESPONSE-DATA-FORMAT=$responseEncoding";
        $apicallc .= "&maxResults=3";
        $apicallc .= "&itemId=$selectedItemID";
    
        // Load the call and capture the document returned by eBay API
        $resp = simplexml_load_file($apicallc);
    
        // Check to see if the response was loaded, else print an error
        if ($resp) {
    
          $retnc = '';
    
        // Verify whether call was successful
        if ($resp->ack == "Success") {
    
            // If there were no errors, build a table for the 3 related category items
            $retnc .= "<!-- start table in getRelatedCategoryItemsResults --> \n";
            $retnc .= "<table width=\"100%\" cellpadding=\"5\" border=\"0\" bgcolor=\"#FFFFA6\"><tr> \n";
            $retnc .= "<td colspan=\"3\"><b>eBay shoppers that liked items in the selected ";
            $retnc .= "item's category also liked items like the following from related categories:</b>";
            $retnc .= "</td></tr><tr> \n";
    
            // If the response was loaded, parse it and build links
            foreach($resp->itemRecommendations->item as $item)
            {
            // For each item node, build a link and append it to $retnc
            $retnc .= "<td valign=\"bottom\"> \n";
            $retnc .= "<div align=\"center\"> <!-- center align item details --> \n";
            $retnc .= "<img src=\"$item->imageURL\"> \n";
            $retnc .= "<p><a href=\"" . $item->viewItemURL . "\">" . $item->title . "</A></P> \n";
            $retnc .= "</div></td> \n";
            } // foreach
            $retnc .= "</tr></table> \n<!-- finish table in getRelatedCategoryItemsResults --> \n";
    
        } else {
          // If there were errors, print an error
          $retnc  = "The response contains errors<br>";
          $retnc .= "Call used was: $apicallc";
        }  // if errors
    
        } else {
          // If there was no response, print an error
          $retnc = "Dang! Must not have got the getRelatedCategoryItems response!<br>$apicallc";
        }  // if $resp
    
        return $retnc;
    
      }  // End of getRelatedCategoryItemsResults function
    
      // Make returned eBay times pretty
      function getPrettyTimeFromEbayTime($eBayTimeString){
      ...
    
  5. Add code to display the getRelatedCategoryItemsResults return value when a most watched item is selected.

    Now, when a button is clicked to select a most watched item, a table of three (if any) related category items appear.

          ...
          return '';
        }
      } // function
    
    
      // Display the response data
      // If button clicked for most watched item, display details and related category items
      if (isset($_POST['Selection']))  {
        $selectedItemID = $_POST['Selection'];
        $cellColor = "bgcolor=\"#dfefff\"";
        print getMostWatchedItemsResults($selectedItemID, $cellColor);
        print getSingleItemResults($selectedItemID);
        print getRelatedCategoryItemsResults($selectedItemID);
      } else {
      // If button not clicked, show only most watched items
        print getMostWatchedItemsResults('', '');
      }
    
    ?>
    
    </body>
    </html>
  6. Save the file as a PHP file and run it on your PHP-enabled server.

    Congratulations! Your application is complete! Now, when you click a button for a most watched item, three items from related categories are displayed below the item details.

    Running application (Step 3)

Congratulations! You have used the Merchandising and Shopping APIs to build an application to retrieve information about recommended items.

Back to top

Notes and Next Steps

This section contains observations about the tutorial and offers some alternate ways to achieve similar or better results. We've also provided some suggestions about where you can go from here.

eBay Partner Network (eBay Affiliate Program)

You can earn money with the eBay Partner Network (eBay Affiliate Program)! Send users to eBay, and earn money for new active users (ACRUs) and successful transactions. For more information, visit the eBay Partner Network.

For information about the call input parameters for affiliate tracking, see the description for affiliate for getDeals in the Merchandising API Call Reference.

About the Calls

This tutorial uses three calls. See

See getMostWatchedItems and getRelatedCategoryItems in the Merchandising API Call Reference and GetSingleItem in the Shopping API Call Reference for descriptions of all the input and output parameters and additional information. Try modifying the input or incorporating additional calls. You can also modify the application to display additional fields.

Additional Resources

Here's a list resources for more information about the eBay Merchandising API:

Here's a list resources for more information about the eBay Shopping API: