Using the Data Unison API
Data Unison Provide the following Ebay statistics for free.
- Total Sales figure for an Ebay item over a period of time
- Total Number of item sold on Ebay over a period of time
- Average price for item on Ebay over a period of time
- Maximum price item was sold for on Ebay over a period of time
- Minimum price item was sold for on Ebay over a period of time
That means we're still missing some important statistics but lets see how this API works in practice and how we can solve some of Martha's problems.
General Procedure
- Create a URL with Query String
- Post the URL and capture the Query's results as XML
- Process the XML extracting the required data
- Display the formatted data to the user
Data Unison's system involves taking a URL , the same kind of URL you type into a web browser window and attaching query parameters to the end of it.
This is the basic URL:
http://open.api.dataunison.com/rest/
To it we add our query parameters. Query parameters are added in a name=value format. Here are the names and values we can use with the GetPriceResearch Query.
| Field | Description | Expected Value |
| CallName * | The name of the call you're trying to make. | string |
| Version * | The version of the call you're schema is based upon. The only valid value for this field is currently "2". | integer |
| AppID * | The eBay Shopping API Application ID you wish to authenticate with. | string |
| QueryKeywords * | The keywords you're searching for. | string |
| EndTimeTo | The last day in the period you wish to search. | date |
| ResearchPeriod | The length (in days) of the period you wish to search. | integer |
| Currency | The currency you wish to view results in. | currency |
| IncludeDailyStatistics | Whether or not you wish to view daily statistic breakdowns. | boolean |
| ChildCategoryID | Search only specific category ids. | integer |
| ListingLimit | Limit the number of listings used in the statistics. | integer |
| MinPrice | Search only listings over a certain price. | float |
| MaxPrice | Search only listings under a certain price. | float |
Data Unison allow clients to run different types of queries but all except GetPriceResearch have to be paid for. We therefore have to tell the web service which kind of query we want to run.
?CallName=GetPriceResearch
http://open.api.dataunison.com/rest/?CallName=GetPriceResearch
To use the Data Unison web service you have to have Ebay Access Keys*.
- To get these go to the Ebay Site Map then to the ebay developers programme.
- Join the ebay developers program which automatically provides Access Keys.
OK we've got our Ebay Access keys. The only one of these we need for this example is the AppID.
The next thing we need to do is write a program using the URL we created to get some XML and then parse the XML, retrieving the fields we need and display them to the user.
We'll write the code in these languages php, java and C#.net
in PHP
<?php
$AppID = '<Your Ebay APPID >'; // APPID, needed for Pricing calls
$SiteId = '0'; // Site used. Site ID 0 = US
$Version = '2'; // API version
$Query = 'ipod mp3 player'; // Keyword query
$SafeQuery = urlencode($Query); // Make the query URL-friendly
// Construct the GetPriceResearch REST call
$apicall = "http://rest.api.ebay.com/restapi?CallName=GetPriceResearch&AppID=$AppID&SiteId=$SiteId&Version=$Version&researchPeriod=30&Query=$SafeQuery";
// Load the call and capture the XML document returned by eBay API as an object
$xml = simplexml_load_file($apicall);
// Check to see if the XML response was loaded, else print an error
if ($xml) {
$results = '';
if( $xml->Errors )
{
// If there was an error in the response, print a warning.
$results = "Uh oh, there was an error making the query!";
var_dump($xml->Errors);
}
else
{
// Gather our statistics
$Statistics = $xml->Statistics;
$TotalSales = $Statistics->TotalSales;
$TotalSoldItems = $Statistics->TotalSoldItems;
$AverageSoldPrice = $Statistics->AverageSoldPrice;
$MinSoldPrice = $Statistics->MinSoldPrice;
$MaxSoldPrice = $Statistics->MaxSoldPrice;
$results .= "<b>Total Sales:</b> $TotalSales<br/>";
$results .= "<b>Total Sold Items:</b>
$TotalSoldItems<br/>";
$results .= "<b>Average Sold Price:</b>
$AverageSoldPrice<br/>";
$results .= "<b>Minimum Sold Price:</b>
$MinSoldPrice<br/>";
$results .= "<b>Maximum Sold Price:</b>
$MaxSoldPrice<br/>";
}
}
// If there was no XML response, print an error
else {
$results = "Dang! Must not have got the XML response!";
}
?>
<!-- Build the HTML page with values from the call response -->
<html>
<head>
<title>eBay Pricing Results for <?php echo $Query; ?></title>
</head>
<body>
<h1>eBay Pricing Results for <?php echo $Query; ?></h1>
<?php echo $results; echo "<br />". var_dump($xml);?>
</body>
</html>
import java.net.*;
import java.io.*;
import org.w3c.dom.*;
import javax.xml.parsers.*;
import org.xml.sax.SAXException;
import java.text.*;
public class webservices
{
public static void main(String[] args) throws MalformedURLException, IOException, SAXException, ParserConfigurationException
{
String appID = "<Your Ebay APPID> ";// APPID, needed for Pricing calls
String siteID ="0";
String version = "2";
String restWebUrl ="http://open.api.dataunison.com/rest/?CallName=GetPriceResearch&AppID=appID&Version=2&ResearchPeriod=30&QueryKeywords=ipod%20mp3%20player";
String TotalSales,TotalItems,AverageSoldPrice,MaxSoldPrice,MinSoldPrice;
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuild = factory.newDocumentBuilder();
Document xmlDoc = docBuild.parse(restWebUrl);
if(xmlDoc==null)
{
System.out.println("No xml returned");
}
else
{
//using NumberFormat means the default locale of the system is used
//to format the numbers so that we get things like thousand separators
//in the UK this means a comma is used for thousand separators
//if the program was run in france a space would be used instead.
NumberFormat myFormat = NumberFormat.getInstance();//Default format for the system
myFormat.setMinimumIntegerDigits(3);
myFormat.setMinimumFractionDigits(2);
myFormat.setMaximumFractionDigits(2);
if(myFormat.isGroupingUsed()){System.out.println("Grouping is used");}
TotalSales = myFormat.format(Double.parseDouble(xmlDoc.getElementsByTagName("TotalSales").item(0).getFirstChild().getNodeValue()));
TotalItems = myFormat.format(Double.parseDouble(xmlDoc.getElementsByTagName("TotalSoldItems").item(0).getFirstChild().getNodeValue()));
AverageSoldPrice = myFormat.format(Double.parseDouble(xmlDoc.getElementsByTagName("AverageSoldPrice").item(0).getFirstChild().getNodeValue()));
MaxSoldPrice = myFormat.format(Double.parseDouble(xmlDoc.getElementsByTagName("MaxSoldPrice").item(0).getFirstChild().getNodeValue()));
MinSoldPrice = myFormat.format(Double.parseDouble(xmlDoc.getElementsByTagName("MinSoldPrice").item(0).getFirstChild().getNodeValue()));
System.out.println(TotalSales);
System.out.println("Total Items"+" "+ "Average Price"+" "+"Maximum Price" +" "+"Minimum Price");
System.out.println(TotalItems + " " + AverageSoldPrice + " " + MaxSoldPrice + " " + MinSoldPrice);
}
}
}
using System;
using System.Xml;
//using System.Net;
namespace DataUnison
{
class Program
{
static void Main(string[] args)
{
try{
//private didn't work inside this method
String restWebUrl = "http://open.api.dataunison.com/rest/?CallName=GetPriceResearch"
+"&AppID=YourAppID&Version=2&ResearchPeriod=30&QueryKeywords=ipod%20mp3%20player";
XmlNodeList TotalSales, TotalItems;// AverageSoldPrice, MaxSoldPrice, MinSoldPrice;
XmlDocument doc = new XmlDocument();
doc.PreserveWhitespace =true;
doc.Load(restWebUrl);
Console.WriteLine(doc.DocumentElement.OuterXml);
String TotalSalesString;
TotalSales = doc.GetElementsByTagName("TotalSales");
TotalItems = doc.GetElementsByTagName("TotalItems");
Console.WriteLine("TotalSales" + TotalSales[0].InnerXml);
TotalSalesString = doc.GetElementsByTagName("TotalSales").Item(0).ChildNodes[0].Value;
double DoubleTotalSales=Convert.ToDouble(TotalSalesString);
Console.WriteLine("TotalSalesString" + DoubleTotalSales.ToString("N") + doc.GetElementsByTagName("TotalSales").Item(0).ChildNodes[0].NodeType);
}
catch (Exception e)
{
Console.WriteLine("Exception on loading document" + e.Message);
}
}
}
}
