PHP eBay Connection Functions
View the example & source code.
I was playing around with a website idea that would make use of the eBay developers program. If you havn't heard of the eBay developers program it pretty much gives software developers complete access to eBay. This includes things from accessing eBay items (searching, filtering, etc), to selling and monitoring auctions. Long story short - it's quite powerful, and pretty fun to play around with.
Note: Because this uses the function simplexml_load_string() these methods require PHP5.
What this is
This page is meant to share some of the functions I use to connect to eBay, request information, and parse that information. The parsed information is placed into variables so the main parse() function is very easy to customize for your needs.
There are three main functions that I use to bring in eBay auctions.
function getXML($xml, $callname)
This function connects to eBay, POST's the given $xml, and calls the call supplied with $callname. You can see all the available callnames and available xml (this xml is what allows you to search, filter, etc.) at the official eBay function reference guide. An example of suppling xml as shown in this projects example is described below.
<?php $xml = "<finditemsadvancedrequest xmlns="">"; $xml .= "<querykeywords>computer</querykeywords>"; $xml .= "<maxentries>10</maxentries>"; $xml .= "</finditemsadvancedrequest>"; $response = getXML($xml, "FindItemsAdvanced"); ?>
As you can see, this builds a string of xml that defines a FindItemsAdvancedRequest. This request searches auctions for the query "computer," and limits the number of auctions returned to 10. It's pretty basic but can be very powerful as eBay provides many different options.
function parse($xml)
The parse function parses the xml result from the getXML function. This function contains a loop that outputs the details of each auction returned in the xml. It is very easy to modify so you can add html+css to make it look much nicer than in the examples below. Some of the variables available:
- $title - The title of the eBay auction
- $link - The link to the eBay auction
- $gallery - The main "gallery" image
- $price - The current bid or buy-it-now price
- $shipping cost - The cost for shipping on the item
- $bin - Whether or not the auction has buy-it-now available
As you can see, with those 6 variables you can supply a pretty good amount of information about each auction to web page viewers.
function parseEbayTime($eTime)
In the XML returned from getXML() there is a field called <timeleft>. This is a field that says how long a auction has left. I have no idea what format it is in (does anyone know? let me know in the comments!), so I assumed that it is an eBay proprietary format. An example of the format is: PT5D4H1M23S. If you look closely you will notice that there are 5 days, 4 hours, 1 minute, and 23 seconds left. So basically you throw that funky string into parseEbayTime and it will return you an associative array of values. The array is defined as follows:
array( "days"=>$days, "hours"=>$hours, "minutes"=>$minutes, "seconds"=>$seconds );
It is then really easy to get the time left by calling:
<?php $timeleft = parseEbayTime("PT4H1M1S"); echo $timeleft["hours"]; ?>
for example would return you the hours left (which is 4).
Examples/Code
There is a basic example of this here, which shows these functions being used to pull in live eBay data. The full source is available here, but you will likely have to edit the parse function inside the loop to fit your needs. As always, use this code for whatever you want, and leave comments, suggestions, or bugs in the comments section.
