Search in the eBay Shopping API

Posted: February 2, 2010 in Uncategorized

In this tutorial you use PHP to write a FindItems call to search for eBay listings based on a keyword. You specify that the response to your call is in XML format.

This tutorial shows how easy it is to use the eBay Shopping API. For notes about the tutorial and the eBay Partner Network, please see Notes and Next Steps. For additional resources, please see Additional Resources.

When you complete the tutorial, you will have code that looks like this when it runs:

Simple search

There are three steps:

Step 1: Set up and make the FindItems call

Step 2: Add code to parse and display the response

Step 3: Run the code

The complete code in PHP_SearchGS_NV_XML.zip requires that you substitute your production appid for “MyAppID”. In this tutorial, the equivalent of the sample in that zip file is MySample.php.


Step 1: Set up the FindItems call

Please join the eBay Developers Program. Note your Production appid so you can substitute it in this tutorial where it says “MyAppID.” This tutorial uses production data.

Please install Apache HTTP Server and install PHP 5. PHP 5 includes the SimpleXML extension. In this tutorial, the PHP sample file will be stored at the following location: C:\Program Files\Apache Software Foundation\Apache2.2\htdocs.

This tutorial uses the following URL to make an eBay Shopping API call:
http://open.api.ebay.com/shopping?appid=MyAppID&version=517&siteid=0&callname=FindItems&QueryKeywords=ipod&responseencoding=XML.

To use this URL in the tutorial, you must substitute your production appid for “MyAppID” and remove all spaces and new-line characters.

To create the initial code for your Shopping API call:

  1. Create a new file with the following text, which includes the first line of the PHP code. Save the file at C:\Program Files\Apache Software Foundation\Apache2.2\htdocs\MySample.php:
    <?php
    
    ?>
    <html>
    <head>
    <title>
    eBay Search Results for <?php echo $query; ?>
    </title>
    </head>
    <body>
    <h1>eBay Search Results</h1>
    <?php echo $results;?>
    </body>
    </html>
  2. Plan the values you will use for the FindItems input parameters.
    Standard Parameter Sample value Description
    appid MyAppID The appid you obtain by joining the eBay Developers Program.
    version 517 The API version that your application supports.
    siteid 0 The numeric value for the eBay site with the items you want information about, e.g. the siteid of the US site is 0.
    callname FindItems The name of the call you are using, e.g. FindItems.
    responseencoding XML Specifies that the output format is XML.
    Call-Specific Parameter Sample value Description
    QueryKeywords ipod A query that specifies a search string.
  3. Add the following code directly after <?php. This code contains the FindItems input parameters from the step above, including the query keyword and your appid. This code is part of the code for a FindItems call:
    error_reporting(E_ALL);  // turn on all errors, warnings and notices for easier debugging
    
    $query = 'ipod';  // A query
    
    $SafeQuery = urlencode($query);
    
    $endpoint = 'http://open.api.ebay.com/shopping';  // URL to call
    $responseEncoding = 'XML';   // Format of the response
    
    // Construct the FindItems call
    $apicall = "$endpoint?callname=FindItems&version=517&siteid=0&appid=MyAppID&QueryKeywords=$SafeQuery&responseencoding=$responseEncoding";
    1. In MySample.php, after the last line above (where $apicall is set to a URL value), add the following code:
      // Load the call and capture the document returned by the Shopping API
      $resp = simplexml_load_file($apicall);
      
      // Check to see if the response was loaded, else print an error
      if ($resp) {
      	$results = '';
      
          // If the response was loaded, parse it and build links
          foreach($resp->Item as $item) {
              $link  = $item->ViewItemURLForNaturalSearch;
              $title = $item->Title;
      
      		// For each result node, build a link and append it to $results
      		$results .= "<a href=\"$link\">$title</a><br/>";
      	}
      }
      // If there was no response, print an error
      else {
      	$results = "Oops! Must not have gotten the response!";
      }
    2. Save the MySample.php file.

  4. Back to Top

    Step 2: Add code to make the call and display the response

    In this step you will add code to store and then display the items returned.

    As described in Step 1, the URL used for your Shopping API call is http://open.api.ebay.com/shopping?appid=MyAppID&version=517&siteid=0&callname=FindItems&QueryKeywords=ipod&responseencoding=XML.

    The responseencoding=XML parameter causes the response data to be in XML format.

    In this step you will add code to store and then display the items returned.


    Back to Top

    Step 3: Run the code

    The MySample.php file is complete. Open the file in a browser (http://localhost/MySample.php).

    The result should look similar to the following:

    Simple search

    Congratulations! You have used the eBay Shopping API to search for items on eBay and to display the search results to a user.

    Reference:  http://developer.ebay.com

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s