eBay Shopping API - OAUTH getBasicToken Example for PHP with cURL
The code below is a working example of creating and using an Application token (Client Credentials grant flow),
typically implemented by programs that do not require access to a user account.
For the Shopping API, the header will be 'X-EBAY-API-IAF-TOKEN' (there is no query parameter alternative),
while both the earlier 'appid' query parameter and 'X-EBAY-API-APP-ID' HTTP header were deprecated June 2021 and can no longer be used with the Shopping API.
The alternative, not used by the Shopping API, and not shown here, is to create a User token (Authorization Code grant flow)
that requires the user to log into their eBay account to grant permission.
The Code is presented in two parts:
- The first PHP code section is an "include" module that will get a token and will reset an expired token.
The cURL code is written in longhand for easier understanding.
You will need to place three text files in a folder behind your server's public firewall (we call the folder ebay_oauth).
The files are:
The code will then work without modification if you name it "getBasicToken.php".
- ebay_apiuser.txt containing AppId.
- ebay_apisecret.txt containing CertId.
- ebay_basic_token.txt an empty Token file.
- The second code section is a skeleton PHP example that would be integrated into the application to call the token through the 'include' file.
To run, the application code would need the additional input of $siteID, $selectorArr, and $itemNumArr that are used to build the URL call.
Processing the $apiResponse data returned from eBay, and handling errors, would be your obligation.
Include file to create and renew token
Application that uses the above include file