How to Search the Ebay API By Keywords using PHP

PHP







Search for Items on Ebay




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

So ebay is one of those important sites whose API is used by many sites.

This includes many sites that find the cheapest price of items and searches many online sites, including ebay, to find out which offer the most competitive. So knowing how to use ebay's API is huge, since ebay is a high-traffic, valuable website.

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 Keywords. 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-keywords.php". This is the file that we send the form data 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 any item wanted on ebay. The name attribute of the input text box is "search_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. See the following link if you want to change the country: Global IDs.



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 $search 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=
findItemsByKeywords&SERVICE-VERSION=1.0.0&SECURITY-APPNAME=
MyAppID&GLOBAL-ID=EBAY-US&keywords=home alone&paginationInput.entriesPerPage=3


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 search term 'home alone'.

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 query.

Since we are searching for items by keywords, the operation name is set to findItemsByKeywords. Later we'll see in other pages how we can change this to finding items by ISBN or finding items by UPC.

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 keywords the ebay API using PHP.


Related Resources

How to Search the Ebay API by ISBN using PHP

How to Search the Ebay API by UPC using PHP




HTML Comment Box is loading comments...