Before you start
In June 2004, O'Reilly published a white paper analyzing eBay's Web service deployment. The paper stated that eBay was serving approximately one billion requests each month through its Web service API and that this figure had grown by more than an order of magnitude since 2002. Additionally, one year ago, 40% of all listings on eBay were accomplished through the company's Web services.
I don't know the current figures, but I do know that the developer program is growing by leaps and bounds. eBay continues to refine, adapt, and extend the services it offers through its API. eBay has exposed its entire business model through its API in the hopes that developers will use it to create the next breed of e-commerce applications. This has led to a rich and complex API that requires a fair bit of savvy to master. But eBay works hard to give third-party developers the tools they need to build commercial software offerings.
This tutorial focuses on developing eBay applications using the XML API with PHP5. The emphasis is on the interface between the application and eBay's Web services, because developing a complete application would be beyond the scope of this tutorial. The tutorial discusses the structure of the request and response XML and the transport mechanism for delivering the requests and retrieving the responses from eBay. It also illustrates in detail how to use the Services_Ebay PHP framework.
The techniques discussed fit equally well in a stand-alone application or a Web application. That may seem strange in a PHP tutorial, because PHP is almost always used for Web applications. However, PHP5 has made inroads toward becoming a general scripting language as well as a Web development language. Developing large Web-based e-commerce applications with PHP offers considerable benefit, but there's also merit in developing small, personal scripts that can run outside a Web server. For example, you might write a script that polls eBay, retrieves all your current auctions, gets the current maximum bid for each one, and e-mails you the total every morning. If this were an Amazon Web services tutorial, the obvious next step would be to have the script automatically order the top items on your wish list, up to the value of the total.
This tutorial assumes that you're comfortable with programming in PHP and have at least a basic understanding of the object model in PHP5. If you need an introduction to object-oriented (OO) programming in PHP, some resources are listed in the Resources section. Don't be intimidated if you don't have strong OO skills. Although Services_Ebay is built in a very strong OO way, using it doesn't require a lot of OO knowledge.
You should also have a basic understanding of XML; you'll be wrangling a lot of it in this tutorial. You should understand the criteria for a well-formed XML document, be comfortable with the terminology used to describe XML, and so on. As before, check the Resources section if you need a refresher.
Part 2 of the eBay tutorial series explains how to build a book-distribution application with the eBay Java SDK. I highly recommend that you read at least the first half of this tutorial, which provides detailed information about the eBay authentication and authorization mechanism. I'm going to assume you have that knowledge for this tutorial.
In order to execute the examples, you need to have the following software installed:
Depending on how you built your PHP, you might already have CURL and OpenSSL installed. The easiest way to check is to look at the results of the
$ php -r 'phpinfo();' | grep CURL CURL support => enabled CURL Information => libcurl/7.13.1 OpenSSL/0.9.7e zlib/1.2.2
Or, you can create a file in a directory accessible to your Web server with the following code:
<?php phpinfo(); ?>
Most of the examples have been developed to run in PHP's CLI mode (as opposed to CGI). This means execution results are echoed to standard out as text instead of being wrapped up in HTML. The benefit of this approach is that you can put the example in a directory and execute it by typing the following:
php -f <filename>
You don't need a running Web server. Running the examples in CGI mode is simply a matter of wrapping the output in HTML. For the examples that display XML, you'll also need to escape the output using the
htmlentities function. This is discussed in the appropriate section.
The final prerequisite for this tutorial is a pretty good understanding of the basic patterns of working with the eBay Web services, especially registering your application with eBay and using the authentication and authorization mechanisms. This subject is discussed at length in the Java SDK tutorial. The application registration and authentication and authorization process is platform agnostic. Rather than cover the same material in this tutorial, I recommend that you read the Java tutorial. The first half will make sense to someone with experience in any programming language. The second half discusses implementing Librivore in Java. It may be helpful to skim those sections, if only to see how much simpler PHP users have it.
If you do nothing else, however, at least use the eBay Token Generation Tool (see Resources) to generate a token you can use to execute the code in the examples.
Telling you why you should use PHP is probably unnecessary. Nonetheless, here are a few reasons why using PHP for developing eBay applications is a Good Thing:
- PHP is good for rapid application development. It's easy to prototype and incrementally develop applications in PHP. Many of the shortcomings of Java for Rational® Application Developer, such as the static typing and the verbosity of the language, don't exist in PHP.
- PHP has an enormous number of libraries and extensions. Hundreds of functions are built into the language for developers to use. And the PHP Extension and Application Repository (PEAR), while not CPAN, has lots of extensions that developers can use instead of reimplementing common functionality.
- PHP is very well documented. The PHP documentation really shines. The manual is structured so that developers can comment on the individual sections with clarifications and code examples. Additionally, many good PHP books let developers get a good understanding of PHP easily. I've listed some of my favorites in the Resources section.
- PHP is mature. It's been around since 1994, and it's been in constant development and refinement.
- PHP scales. PHP means Personal Home Page. It was originally designed by Rasmus Lerdorf as a Perl script to make simple dynamic home pages. Now, PHP is being used to build high-availability distributed applications. IBM has partnered with Zend Technologies to build enterprise applications using PHP and Derby or DB2® UDB.
And there's a subjective reason why you should use PHP: I enjoy programming in PHP. If you're reading this tutorial, I'm guessing that you do, too. I hope this tutorial expands your knowledge of the things you can do with PHP and is enjoyable, as well.