Not too long ago, I whipped up a PHP class that makes the process of working with the Envato Marketplaces API as easy as possible. For instance, rather than researching how to pull in your latest items, query the API, and cache the results, this class does the work for you. I’ll show you how to use it.
Step 1 – Download the Class
Naturally, you’ll need the class before you can move forward. It can be download or cloned from GitHub. Get it here.
git clone git@github.com:JeffreyWay/Envato-Marketplace-API-Wrapper-in-PHP.git
Step 2 – Reference the Class
Within your PHP project, include
the class. For simple projects, the top of your HTML file will do fine.
<php include 'Envato_marketplaces.php';
Step 3 – Instantiate the Class
Next, because we’re working with a class
, we should create a new instance of the class, so that we can gain access to its methods.
<php include 'Envato_marketplaces.php'; $Envato = new Envato_marketplaces();
Step 4 – Have Fun
All the prep work is finished, you now have access to a bunch of fun methods – everything from retrieving popular items, to pulling in your balance, to verifying a purchase.
1. Quickly Display Item Thumbnails
<php include 'Envato_marketplaces.php'; $Envato = new Envato_marketplaces(); // username, marketplace to pull from, the number to grab $Envato->display_thumbs('Collis', 'themeforest', 5);
display_thumbs
is a helper method that will automatically generate an unordered list of links + images to the requested items.
Upon viewing the source, in the location where display_thumbs
is called, the following HTML will be generated.
<ul class='envato-marketplace-thumbs'> <li> <a href="http://themeforest.net/item/manilla-photoshop-design/22803?ref=Collis" title="Manilla Photoshop Design"> <img src="http://1.s3.envato.com/files/60560.jpg" alt="Manilla Photoshop Design"> </a> </li> <li> <a href="http://themeforest.net/item/black-white-simple-theme/22705?ref=Collis" title="Black + White Simple Theme"> <img src="http://0.s3.envato.com/files/60223.jpg" alt="Black + White Simple Theme"> </a> </li> <li> <a href="http://themeforest.net/item/quik-v1-admin-skin/17314?ref=Collis" title="Quik v1 Admin Skin"> <img src="http://0.s3.envato.com/files/44556.jpg" alt="Quik v1 Admin Skin"> </a> </li> <li> <a href="http://themeforest.net/item/freshcorp-business-template/17528?ref=Collis" title="FreshCorp - Business Template"> <img src="http://1.s3.envato.com/files/45212.jpg" alt="FreshCorp - Business Template"> </a> </li> <li> <a href="http://themeforest.net/item/real-estate-html-template/17732?ref=Collis" title="Real Estate HTML Template"> <img src="http://2.s3.envato.com/files/45739.jpg" alt="Real Estate HTML Template"> </a> </li> </ul>
Please note that, by default, the results of each unique API call will be cached for three hours. Should you need to clear the cache folder, call the
clear_cache()
method:$Envato->clear_cache()
.
2. Get Trending Items
Let’s say you want to earn some referral income, and pull in the ten most popular items on CodeCanyon. Easy.
<?php include 'Envato_marketplaces.php'; $Envato = new Envato_marketplaces(); $trending = $Envato->most_popular('codecanyon'); // object of popular authors (3 mo), and items (3mo and last week) print_r($trending);
In response, an object
, which contains three array
s will be returned: popular authors in the last three months, popular items in the last three months, and the most popular items last week. To display the latter information on the page…
<body> <ul> <?php foreach( $trending->items_last_week as $item ) : ?> <li> <h3><?php echo $item->item; ?></h3> <a href="<?php echo $item->url; ?>?ref=YOUR-USERNAME" title="<?php echo $item->item;?>"> <img src="<?php echo $item->thumbnail;?>" alt="<?php echo $item->item;?>"> </a> </li> <?php endforeach; ?> </ul> </body>
At this point, style the unordered list how you wish, and you’re good to go!
3. Retrieve Your Account Information
You can also pull in private information from the API, such as your account balance, the ability to verify a purchase, etc.
<?php require 'Envato_marketplaces.php'; $Envato = new Envato_marketplaces('YOUR API KEY'); // "My Settings" in marketplace author section $account_info = $Envato->account_information('USERNAME ASSOCIATED WITH API KEY'); # See what we got back.... print_r($account_info);
For obvious security reasons, the username that you pass to the
account_information
method must match the account associated with the API key.
An object
will be returned, which contains various information related to your account. Here’s mine:
stdClass Object ( [balance] => 362.85 // for the current month [country] => United States [image] => "http://0.s3.envato.com/files/1585957/me.jpg" [total_deposits] => 0.00 [firstname] => Jeffrey [total_earnings] => 362.85 [current_commission_rate] => 0.59 [surname] => Way )
This data can be displayed in the admin section of your website, or maybe saved to a database for the purposes of displaying a sales graph for the last year.
Available Methods
Now that you understand the basic process of retrieving data from the API with this wrapper class, here’s a list of all available public methods:
-
set_api_key(key) – Only necessary if you require your private user information. Alternatively, the API key can be passed upon instantiating the class.
-
featured(marketplaceName) – Returns the featured item, author, and free file for a given marketplace.
- item_details(item_id) – Retrieve the details for a specific marketplace item. Grab the id from the URL of the marketplace item page.
- new_files(marketplaceName, category, limit) – Returns new files from a specific marketplaces and category. Note that the category much exist for the marketplace for a result to be returned.
- new_files_from_user(username, marketplaceName, limit) – Similar to new_files, but focuses on a specific author’s files.
- display_thumbs(username, marketplaceName, limit) – Helper function, which automatically echos a list of thumbnails links. Use new_files_from_user for more control.
- most_popular(marketplaceName) – Retrieve the most popular files of the previous week.
- search(searchingFor, marketplaceName, type/category, limit) – Perform search queries on all of the marketplaces, or a specific one.
- user_information(username) – Retrieves general marketplace member information.
- collection(collection_id) – Retrieve an array of all the items in a particular collection.
- balance(username) – Helper method to retrieve your current balance for the month.
- recent_sales(username, limit) – Retrieve details for your most recent sales.
- account_information(username) – Retrieve your account information — balance, location, name, etc.
- earnings_by_month(username, limit) – Grab quick monthly stats – number of sales, income, etc.
- verify_purchase(username, purchase_code) – Can be used to verify if a person did in fact purchase your item.
- clear_cache() – Removes all files in the cache directory
Alternatively, view the class source; it is heavily documented, and should serve as a useful reference.