How to Search the Ebay API By UPC using PHP

PHP







Search for Books by UPC on Ebay




In this article, we go over how to access ebay's API using PHP and search ebay by UPC.

There are many ways of searching ebay. You can search by keywords entered in to the search bar. You can search by ISBN number for books. You can search by UPC number for movies and CDs and other multimedia devices.

In this article, we show specifically how to search by UPC number.

Searching by UPC number is more specific, of course if known, because it's unique to a product. Therefore, it guarantees that you are looking only for that specific item and will get only that item in the search results. It's just like the ISBN number. It's a unique identifier only for one specific item.

Ebay's API is written in XML, so you should have some basic knowledge of XML in order to work with it. But even if you don't, you may still be able to figure it out, especially if you know HTML already. We'll try to break this down as simply as possible, so hopefully you can follow.

So before you can work with ebay's API, you have to sign up to be an ebay developer. You can do this at the following link: Ebay Developers Program.

When you sign up, you'll get an app ID. In order to use ebay's API, you will need an app ID.

So after you've signed up as an ebay developer and have an app ID, you can make ebay API calls. This means you're using ebay's API. A call just means that you're using the API. As of the current time, ebay has a maximum limit of 5000 calls a day. However, since you are just getting started now, you won't need to worry about this right away until you get very big.

So now let's get to the code.

If you want to see the full code, see the following link: Code for Ebay API Search by UPC. The only thing you have to add is the form seen right below under HTML code.

HTML Code

Below is the HTML code needed to create the form that has the text box where a user enters in



So we create a form with the action attribute set to "Ebay-search-by-UPC.php". This is the file that we send the form data to that the user has entered. So we create this separate PHP file. The method of the form is set to POST because we don't want the data attached to the URL. Instead we want it available for data processing in the code.

This form simply has one text box, where a user searches for any item by UPC on ebay. The name attribute of the input text box is "UPC_entered". We will need this later in the separate PHP file that we create.

We then have a submit button so that we can submit the form data to the server.

We then end the form.

PHP Code

Below is the contents of the separate PHP file that we create which processes the request made by the user in the form.

The only thing you have to change in this PHP is to put your own unique app ID in the PHP variable $appid.

If you are changing the country from US to something else, you'll need to change the global ID.



So the code above, taken mostly from ebay's site but modified by me, is shown above.

It's highly commented, so it's most self-explanatory, but I'll go through it quickly.

So we create the PHP variable $itemupc to extract the value that the user has entered into the text box of the HTML form.

The next set of data is all the data needed to create the ebay URL.

The complete URL you will create is shown below:

http://svcs.ebay.com/services/search/FindingService/v1?
OPERATION-NAME=findItemsByProduct&SERVICE-VERSION=1.0.0
&SECURITY-APPNAME=myAppID&GLOBAL-ID=EBAY-US
&productId.@type=UPC&productId=0024543617822
&paginationInput.entriesPerPage=10


If you put in your appID into this URL and paste this URL to the address bar of the web browser, you will get to the ebay API page for the UPC number 0024543617822 (this represents the movie Home Alone 2 on Blu ray disc).

So all the PHP variables that you see below is used to construct the parts of the URL that gets you to the ebay API page for the search terms you are looking for. This includes the base URL, the API version being used, the app ID (unique to you), the global ID (country of use), and the UPC number.

Since we are searching for books by UPC, the operation name is set to findItemsByProduct.

We then use all this data to construct the $apicall variable, which constructs the full URL.

We then load in the XML document that is returned by our query.

If the document is returned successfully, we get the data that makes up the items on the page. This includes the picture, link to the ebay page, the title of that item, the price, and the shipping price of the itme.

We type cast all the numbers, the price and the shipping price, to a float, because when PHP extracts it from the XML page, it is not a number. You have to type cast it into a number. PHP initially sees it as a string, so it must be type cast into a float.

We then create another variable named $totalprice, which calculates the total price of an item by adding the regular price plus the shipping price.

We then create a table that has the picture, link to the ebay page, the price of the item, the shipping price of the item, and the total price of the item. Again, you can customize this according to what you want in the table.

If the response is not successful, we output that it was not a success.

All this above was the majority of the PHP code.

We now move onto the HTML.

So we create HTML tags and we output the results based on the query entered.

And this all that is required to search the ebay API by UPC using PHP.


Related Resources

How to Search the Ebay API by Keywords Using PHP

How to Search the Ebay API by ISBN using PHP




HTML Comment Box is loading comments...