DevSnips.com Code Snippet Repository
search:    


Navigation
  Home
About
Library
Contact
 
Snippet Library
  ColdFusion   338  
  ASP   201  
  PHP   101  
  HTML   11  
  JavaScript   77  
  XML   2  
  CSS   5  
  SQL   13  
  JSP   2  
  C#   1  
  ASP.NET   0  
  Submit a Code Snippet
 
Blog Archive
  September 2007
August 2007
July 2007
June 2007
May 2007
November 2006
October 2006
Search Archives
 
Random Affiliates
  BioMetric Base
Uno-Code
ReviewMe!
Tom Morris

Want to become an affiliate?
Read more...


Privacy Policy
© 2010

Blog Archive

 
Connecting to eBay's API to return real-time auction information for your web site.

eBay offers multiple ways of gathering and displaying auction information for your web site. For example they offer the following methods in their affiliate program:
Editor Kit - javascript based content that pulls auction data (very simple to use)
RSS Feed Generator - eBay offers RSS feeds, where you would be able to pull the feed and parse the XML for your page.
Flexible Destination Tool - Simple re-write of a link to attach your affiliate information. My example will use this as well.
API Developer Kit - Communicating with eBay's web service to return XML results.

The first step is to join eBay's affiliate program (You must have a valid eBay account first).
http://affiliates.ebay.com/

By joining (eBay uses CommissionJunction), you'll be able to monetize your site by presenting auctions and with every click you could cash in on commissions.

The next step is to join the eBay developers program. You will need to do this to begin working with their API tools/services.
http://developer.ebay.com/join/

After joining the developer program, create development keys. These keys are required for communicating with their web service and they are also used to generate the user authentication token.
http://developer.ebay.com/DevZone/account/keys.asp

Once keys are made it's time to create your authentication token.
http://developer.ebay.com/tokentool/

Once you've joined all the programs, have your keys and authentication token, it's time to write some code!

First, we'll assign some local variables with the keys and token we created earlier. eBay also provides a 'sandbox' server to test with. Please do your testing here first, and after all things are working, change it to the production server.

$devID = 'THISISANEXAMPLEDEVIDW47OW7Z46Y';
$appID = 'THISISANEXAMPLEAPPID43A39DY82F';
$certID = 'THISISANEXAMPLECERTID876D72DKQ';
//set the Server to use (Sandbox or Production)
//$serverUrl = 'https://api.sandbox.ebay.com/ws/api.dll';
$serverUrl = 'https://api.ebay.com/ws/api.dll';
//the token representing the eBay user to assign the call with
$userToken = 'AgAAAA**AQAAAA**aAAAAA**Bukb....thisWillBeAGiantString.....3+';

There are many methods to use in the API, including submitting auctions to eBay. They have plenty of documentation you can get here:
http://developer.ebay.com/support/docs/ (must be logged in)

They also have code snippets for JSP, PHP, ColdFusion, .NET, etc.
https://codesamples.codebase.ebay.com/servlets/ProjectDocumentList?folderID=20 (must be logged in)

The PHP code snippets have the following method examples:

GetCategories
GetCategory2CS
GetCategoryListings
GeteBayOfficialTime
GetItem
GetItemShipping
GetPopularKeywords
GetSearchResults
GetSuggestedCategories

My example will query using the search term of 'cookbooks' in a specific category (#11700 = Home & Garden) and return the results. This is using the GetSearchResultsRequest method.

<?
$siteID 			= 0;
$verb 				= 'GetSearchResults';
$compatabilityLevel = 433;
$query				= "cookbooks";
$headers 			= buildEbayHeaders($devID, $appID, $certID, $compatabilityLevel, $siteID, $verb);
$requestXmlBody 	= '<?xml version="1.0" encoding="utf-8" ?>';
$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);
if(stristr($responseXml, 'HTTP 404') || $responseXml == '')
	$setOneError	= true;
if(!$setOneError){
	$responseDoc 		= domxml_open_mem($responseXml);
	$errors 			= $responseDoc->get_elements_by_tagname('Errors');
	if(count($errors) > 0){
		echo '<P><B>eBay returned the following error(s):</B>';
		$code 			= $errors[0]->get_elements_by_tagname('ErrorCode');
		$shortMsg 		= $errors[0]->get_elements_by_tagname('ShortMessage');
		$longMsg 		= $errors[0]->get_elements_by_tagname('LongMessage');
		echo '<P>', $code[0]->get_content(), ' : ', str_replace(">", "&gt;", str_replace("<", "&lt;", $shortMsg[0]->get_content()));
		if(count($longMsg) > 0)
			echo '<BR>', str_replace(">", "&gt;", str_replace("<", "&lt;", $longMsg[0]->get_content()));
	}else{
		$itemNodes		= $responseDoc->get_elements_by_tagname('Item');
		if(count($itemNodes) > 0){
			$setOneEmpty	= false;?>
			<br><br>
			<table cellpadding=6 cellspacing=0 border=0 valign="top">
			<tr bgcolor="#eaeaea">
				<td valign="top" align="center"><strong>Image</strong></td>
				<td valign="top"><strong>Title</strong></td>
				<td valign="top"><strong>Price</strong></td>
				<td valign="top"><strong>Ends</strong></td>
			</tr><?
			$bgColor = "#FFFFFF";
			foreach($itemNodes as $item){
				$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');
				$endTime = $item->get_elements_by_tagname('EndTime');
				$link = $item->get_elements_by_tagname('ViewItemURL');
				$imageUrl = "";
				$image = $item->get_elements_by_tagname('GalleryType');
				if($image[0]->get_content() == "Gallery"){
					$tmp = $item->get_elements_by_tagname('GalleryURL');
					$imageUrl = $tmp[0]->get_content();
				}?>
				<tr bgcolor="<?=$bgColor?>">
					<td valign="top" align="center">
					<?if(strlen(trim($imageUrl)) > 0){?>
						<a href="http://rover.ebay.com/rover/1/711-1751-2978-71/1? AID=xxxxxx&PID=xxxxxxx&mpre=<?echo urlencode($link[0]->get_content()); ?>" target="_blank"><img src="<?=$imageUrl?>" border=0></a><br>
					<?}?>
					</td>
					<td valign="top"><a href="http://rover.ebay.com/rover/1/711-1751-2978-71/1? AID=xxxxxx&PID=xxxxxxx&mpre=<?echo urlencode($link[0]->get_content()); ?>" target="_blank">
					<? echo $title[0]->get_content(); ?>
					</a>
					</td>
					<td valign="top">
					$<? echo number_format($price[0]->get_content(),2); ?>
					</td>
					<td valign="top" nowrap>
					<? echo ebayDate($endTime[0]->get_content()); ?>
					</td>
					<td width=10>&nbsp;</td>
					<td valign="top" nowrap>
					<img src="<?=$imagesroot?>btn_comment.gif" border=0 alt="Submit comment" align="absmiddle">&nbsp;<a href="#" class="small">submit comment</a>
					</td>
				</tr>
				<?$bgColor = $bgColor == "#FFFFFF" ? "#EEEEEE" : "#FFFFFF";
			}
			echo "</table>";
		}
	}
}
?>

You'll notice that there are some function references:

buildEbayHeaders()
sendHttpRequest()

These are provided in the code examples at eBay but will provide them below:

<?php
	/****************************************
	  * AUTHOR: Michael Hawthornthwaite - Acid Computer Services (www.acidcs.co.uk) *
	  ****************************************/


	/**	sendHttpRequest
		Sends a HTTP request to the specified server with the body and headers passed
		Input:	$requestBody
				$serverUrl
				$headers
		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
		curl_close($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
				$applicationID
				$certificateID
				$compatLevel
				$siteID
				$verb
		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;
	}

?>

As you can see it's using the cURL/PHP4 method with domxml_open_mem() to navigate the response data. http://us2.php.net/manual/en/function.domxml-open-mem.php

To benefit from clicks to eBay, implement the Flexible Destination handling. You'll need to change the URL to point to eBay's tracking server and pass a urlencoded() value of the auction location. Don't forget your CJ ID and AdID!

API handling is great for pulling that real time data, but if you have a store and want to submit auction, this might be a great tool to have as well.


Submitted on 11/11/06 at 10:51AM
Post Comment | Read | Comments: 7
Bookmark to:
Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Del.icio.us Add 'Connecting to eBay's API to return real-time auction information for your web site.' to digg Add 'Connecting to eBay's API to return real-time auction information for your web site.' to FURL Add 'Connecting to eBay's API to return real-time auction information for your web site.' to blinklist Add 'Connecting to eBay's API to return real-time auction information for your web site.' to reddit Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Feed Me Links Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Technorati Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Yahoo My Web Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Newsvine Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Socializer Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Ma.gnolia Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Stumble Upon Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Google Bookmarks Add 'Connecting to eBay's API to return real-time auction information for your web site.' to RawSugar Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Squidoo Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Spurl Add 'Connecting to eBay's API to return real-time auction information for your web site.' to BlinkBits Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Netvouz Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Rojo Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Blogmarks Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Shadows Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Simpy Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Co.mments Add 'Connecting to eBay's API to return real-time auction information for your web site.' to Scuttle

Go Back



Do you get this problem?
PHP-nut | 12/11/06 at 09:01AM

I'm sometimes receiving an empty response from eBay. Are you experiencing the same problem? It's intermittent.

PHP-nut
RE: Do you get this problem?
devsnips | 12/11/06 at 2:08PM

Yes. I have experienced similar problems. This is all related to eBay's API. I've read similar complaints in their developer forum. I thought this was addressed.

When did you last experience the problem? If you continue to experience the problem (might be query related.. something with many records, etc), you might want to let eBay developers know about this, since it might be another hiccup in their system.

Hopefully you'll get it resolved. If not, you might want to try the eBay RSS feeds as an alternative depending on what you're doing.

DevSnips
Ogg?
Templatei | 01/16/07 at 06:39AM

Hi all!


G'night
Black
star | 02/21/07 at 8:53PM

The excellent site!!! Want you good luck!!!
Jordan
christina | 02/25/07 at 00:08AM

The excellent site!!! I wish you good luck!!!
cYgChjRYXDTltMYU
Abel | 05/25/07 at 09:41AM

a great job. How do you keep spammers from spamming your comments??
Thanks
Joe | 07/28/07 at 05:11AM

Thanks for the tip!







Advertisements

Gate.com VPS Banner