├── .gitignore
├── Envato_marketplaces.php
├── index.php
└── readme.md
/.gitignore:
--------------------------------------------------------------------------------
1 | *.swp
2 |
--------------------------------------------------------------------------------
/Envato_marketplaces.php:
--------------------------------------------------------------------------------
1 |
7 | * @created January, 2012
8 | * @license Do-whateva-ya-want-with-it
9 | */
10 |
11 |
12 | class Envato_marketplaces {
13 | protected $api_key;
14 | protected $cache_dir = 'cache';
15 | public $cache_expires = 24;
16 | protected $public_url = 'http://marketplace.envato.com/api/edge/set.json';
17 |
18 |
19 | function __construct($api_key = null) {
20 | if ( isset($api_key) ) $this->api_key = $api_key; // allow the user to pass the API key upon instantiation
21 | }
22 |
23 |
24 | /**
25 | * Attach your API key.
26 | *
27 | * @param string $api_key Can be accessed on the marketplaces via My Account
28 | * -> My Settings -> API Key
29 | */
30 | public function set_api_key($api_key)
31 | {
32 | $this->api_key = $api_key;
33 | }
34 |
35 | /**
36 | * Retrieve the value of your API KEY, if needed.
37 | *
38 | * @return string The requested API Key.
39 | */
40 | public function get_api_key()
41 | {
42 | if ( ! isset($this->api_key) ) return 'No API Key is set.';
43 | return $this->api_key;
44 | }
45 |
46 |
47 | /**
48 | * Sets the cache directory for all API calls.
49 | *
50 | * @param string $cache_dir
51 | */
52 | public function set_cache_dir($cache_dir)
53 | {
54 | $this->cache_dir = $cache_dir;
55 | }
56 |
57 |
58 | /**
59 | * Retrieve the value of your cache directory, if needed.
60 | *
61 | * @return string The requested cache directory.
62 | */
63 | public function get_cache_dir()
64 | {
65 | return $this->cache_dir;
66 | }
67 |
68 |
69 | /**
70 | * Available sets => 'vitals', 'earnings-and-sales-by-month', 'statement', 'recent-sales', 'account', 'verify-purchase', 'download-purchase'
71 | *
72 | */
73 | public function private_user_data($user_name, $set, $purchase_code = null)
74 | {
75 | if ( ! isset($this->api_key) ) exit('You have not set an api key yet. $class->set_api_key(key)');
76 | if (! isset($set) ) return 'Missing parameters';
77 |
78 | $url = "http://marketplace.envato.com/api/edge/$user_name/$this->api_key/$set";
79 | if ( !is_null($purchase_code) ) $url .= ":$purchase_code";
80 | $url .= '.json';
81 |
82 | $result = $this->fetch($url);
83 |
84 | if ( isset($result->error) ) return 'Username, API Key, or purchase code is invalid.';
85 | return $result->$set;
86 | }
87 |
88 | /**
89 | * Can be used to verify if a person did in fact purchase your item.
90 | *
91 | * @param $user_name Author's username.
92 | * @param $purchase_code - The buyer's purchase code. See Downloads page for
93 | * receipt.
94 | * @return object|bool If purchased, returns an object containing the details.
95 | */
96 | public function verify_purchase($user_name, $purchase_code)
97 | {
98 | $validity = $this->private_user_data($user_name, 'verify-purchase', $purchase_code);
99 | return isset($validity->buyer) ? $validity : false;
100 | }
101 |
102 | /**
103 | * Can be used to retrieve the download URL for a purchased item.
104 | *
105 | * @param $user_name Purchaser's username.
106 | * @param $purchase_code - The item purchase code. See Downloads page for
107 | * receipt.
108 | * @return string If purchased, returns a string containing the download URL.
109 | */
110 | public function download_purchase($user_name, $purchase_code)
111 | {
112 | $download_url = $this->private_user_data($user_name, 'download-purchase', $purchase_code);
113 | return isset($download_url->download_url) ? $download_url->download_url : false;
114 | }
115 |
116 | /**
117 | * Helper method to retrieve the balance on your account.
118 | *
119 | * @param string $user_name The username attached to your API KEY.
120 | * @return string The balance in your account.
121 | */
122 | public function balance($user_name)
123 | {
124 | $vitals = $this->private_user_data($user_name, 'vitals');
125 | return $vitals->balance;
126 | }
127 |
128 | /**
129 | * Retrieve details for your most recent sales.
130 | *
131 | * @param string $user_name The username attached to your API KEY.
132 | * @param int $limit The number of sales to return.
133 | * @return array A list of your recent sales.
134 | */
135 | public function recent_sales($user_name, $limit = null)
136 | {
137 | $sales = $this->private_user_data($user_name, 'recent-sales');
138 | return $this->apply_limit($sales, $limit);
139 | }
140 |
141 | /**
142 | * Retrieve your account information -- balance, location, name, etc.
143 | *
144 | * @param string $user_name The username attached to your API KEY.
145 | * @return array A list of account information for the user.
146 | */
147 | public function account_information($user_name)
148 | {
149 | return $this->private_user_data($user_name, 'account');
150 | }
151 |
152 | /**
153 | * Grab quick monthly stats - number of sales, income, etc.
154 | *
155 | * @param string $user_name The username attached to your API KEY.
156 | * @param int $limit The number of months to return.
157 | * @return array A list of sales figures, ordered by month.
158 | */
159 | public function earnings_by_month($user_name, $limit = null)
160 | {
161 | $earnings = $this->private_user_data($user_name, 'earnings-and-sales-by-month');
162 | return $this->apply_limit($earnings, $limit);
163 | }
164 |
165 | /**
166 | * Generic method, to be used in combination with the marketplace API docs.
167 | *
168 | * @param string $user_name The user name of the seller to track.
169 | * @return array The returned data wrapped in an array.
170 | */
171 | public function public_user_data($user_name)
172 | {
173 | $url = preg_replace('/set/i', 'user:' . $user_name, $this->public_url);
174 | return $this->fetch($url, 'user');
175 | }
176 |
177 | /**
178 | * Returns the featured item, author, and free file for a given marketplace.
179 | *
180 | * @param string $marketplace_name The desired marketplace name.
181 | * @return array The featured file, free file, and featured author for the
182 | * given site.
183 | */
184 | public function featured($marketplace_name = 'themeforest')
185 | {
186 | $url = preg_replace('/set/i', 'features:' . $marketplace_name, $this->public_url);
187 | return $this->fetch($url, 'features');
188 | }
189 |
190 | /**
191 | * Retrieve the details for a specific marketplace item.
192 | *
193 | * @param string $item_id The id of the item you need information for.
194 | * @return object Details for the given item.
195 | */
196 | public function item_details($item_id)
197 | {
198 | $url = preg_replace('/set/i', 'item:' . $item_id, $this->public_url);
199 | return $this->fetch($url, 'item');
200 | }
201 |
202 | /**
203 | * Returns new files from a specific marketplaces and category.
204 | *
205 | * @param string $marketplace_name The desired marketplace name.
206 | * @param string $category The name of the category you'd like to search.
207 | * @param int $limit The number of files to return.
208 | * @return array A list of ALL recent files.
209 | */
210 | public function new_files($marketplace_name = 'themeforest', $category = 'wordpress', $limit = null)
211 | {
212 | $url = preg_replace('/set/i', 'new-files:' . $marketplace_name . ','. $category, $this->public_url);
213 | $results = $this->fetch($url, 'new-files');
214 |
215 | if ( $results ) {
216 | return $this->apply_limit($results, $limit);
217 | }
218 | }
219 |
220 | /**
221 | * Similar to new_files, but focuses on a specific author's files.
222 | *
223 | * @param string $user_name The desired username.
224 | * @param string $marketplace_name The desired marketplace name.
225 | * @param int $limit The number of files to return.
226 | * @return array A list of recently added files by one user.
227 | */
228 | public function new_files_from_user($user_name, $marketplace_name = 'themeforest', $limit = null)
229 | {
230 | $cache_path = "$this->cache_dir/$user_name-$marketplace_name-new_files";
231 |
232 | $url = preg_replace('/set/i', 'new-files-from-user:' . $user_name . ',' . $marketplace_name, $this->public_url);
233 |
234 | return $this->apply_limit( $this->fetch($url, 'new-files-from-user'), $limit );
235 | }
236 |
237 | /**
238 | * Helper function which automatically echos out a list of thumbnails
239 | * + links. Use new_files_from_user for more control.
240 | *
241 | * @param string $user_name The username of the account you want to display
242 | * thumbnails from.
243 | * @param string $marketplace_name The desired marketplace name.
244 | * @param int $limit The number of thumbnails to display.
245 | * @return string Helper function immediately echos out thumbnails.
246 | * Careful...
247 | */
248 | public function display_thumbs($user_name, $marketplace_name, $limit = null)
249 | {
250 | $results = $this->new_files_from_user($user_name, $marketplace_name, $limit);
251 |
252 | echo "
\n";
253 | foreach($results as $item) : ?>
254 |
255 | -
256 | " title="item;?>">
257 |
258 |
259 |
260 | ";
262 | }
263 |
264 | /**
265 | * Retrieve the most popular files of the previous week.
266 | *
267 | * @param string $marketplace_name Desired marketplace name.
268 | * @param int $limit The number of items to return [optional].
269 | * @return array A list of the most sold items in the given marketplace last
270 | * week.
271 | */
272 | public function most_popular($marketplace_name = 'themeforest')
273 | {
274 | $url = preg_replace('/set/i', 'popular:' . $marketplace_name, $this->public_url);
275 | return $this->fetch($url, 'popular');
276 | }
277 |
278 | /**
279 | * Retrieve the random list of newly uploaded files.
280 | *
281 | * @param string $marketplace_name Desired marketplace name.
282 | * @param int $limit The number of items to return [optional].
283 | * @return array A list of random new files in the given marketplace.
284 | */
285 | public function random_new_files($marketplace_name = 'themeforest', $limit = null)
286 | {
287 | $url = preg_replace('/set/i', 'random-new-files:' . $marketplace_name, $this->public_url);
288 | $random = $this->curl($url)->{'random-new-files'};
289 | return $this->apply_limit($random, $limit);
290 | }
291 | /**
292 | * Perform search queries on all of the marketplaces, or a specific one.
293 | *
294 | * @param string $search_expression What are you searching for?
295 | * @param string $marketplace_name The name of the marketplace you want to
296 | * search. [optional]
297 | * @param string $type The item type (category). See search options on
298 | * marketplace for list. [optional]
299 | * @param integer $limit The number of items to return [optional]
300 | * @return array A list of the search results.
301 | */
302 | public function search($search_expression, $marketplace_name = '', $type = '', $limit = null)
303 | {
304 | if ( empty($search_expression) ) return false;
305 | # Can't use spaces. Need to replace them with pipes.
306 | else $search_expression = preg_replace('/\s/', '|', $search_expression);
307 |
308 | $url = preg_replace('/set/i', 'search:' . $marketplace_name . ',' . $type . ',' . $search_expression, $this->public_url );
309 | $search_results = $this->curl($url)->search;
310 | return $this->apply_limit($search_results, $limit);
311 | }
312 |
313 | /**
314 | * Retrieves general marketplace member information.
315 | *
316 | * @param string $user_name The username to query.
317 | * @return object Contains the requested user information.
318 | */
319 | public function user_information($user_name)
320 | {
321 | $url = preg_replace('/set/i', 'user:' . $user_name, $this->public_url);
322 | return $this->fetch($url, 'user');
323 | }
324 |
325 | /**
326 | * Retrieve an array of all the items in a particular collection.
327 | *
328 | * @param string $collection_id The id of the requested collection. See url
329 | * of collection page for id.
330 | * @return array A list of all the items in the collection.
331 | */
332 | public function collection($collection_id)
333 | {
334 | $url = preg_replace('/set/i', 'collection:' . $collection_id, $this->public_url);
335 | return $this->fetch($url, 'collection');
336 | }
337 |
338 |
339 | /*
340 | * Either fetches the desired data from the API and caches it, or fetches the cached version
341 | *
342 | * @param string $url The url to the API call
343 | * @param string $set (optional) The name of the set to retrieve.
344 | */
345 | protected function fetch($url, $set = null)
346 | {
347 | // Use the API url to generate the cache file name.
348 | // So: http://marketplace.envato.com/api/edge/collection:739793.json
349 | // Becomes: collection-739793.json
350 | $cache_path = $this->cache_dir . '/' . str_replace(':', '-', substr(strrchr($url, '/'), 1));
351 |
352 | if ( $this->has_expired($cache_path) ) {
353 | // get fresh copy
354 | $data = $this->curl($url);
355 |
356 | if ($data) {
357 | $data = isset($set) ? $data->{$set} : $data; // if a set is needed, update
358 | } else exit('Could not retrieve data.');
359 |
360 | $this->cache_it($cache_path, $data);
361 |
362 | return $data;
363 | } else {
364 | // if available in cache, use that
365 | return json_decode(file_get_contents($cache_path));
366 | }
367 | }
368 |
369 | /**
370 | * Filters returned result, according to the supplied $limit.
371 | *
372 | * @param string $orig_arr The original array to work on.
373 | * @param int $limit Specifies the number of array items in the result.
374 | * @return array A new array with a count equal to the passed $limit.
375 | */
376 | public function apply_limit($orig_arr, $limit)
377 | {
378 | if ( !is_int($limit) ) return $orig_arr;
379 |
380 | // Make sure that there are enough items to filter through...
381 | if ( $limit > count($orig_arr) ) $limit = count($orig_arr);
382 |
383 | $new_arr = array();
384 | for ( $i = 0; $i <= $limit - 1; $i++ ) {
385 | $new_arr[] = $orig_arr[$i];
386 | }
387 | return $new_arr;
388 | }
389 |
390 |
391 | /**
392 | * General purpose function to query the marketplace API.
393 | *
394 | * @param string $url The url to access, via curl.
395 | * @return object The results of the curl request.
396 | */
397 | protected function curl($url)
398 | {
399 | if ( empty($url) ) return false;
400 |
401 | $ch = curl_init($url);
402 | curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
403 |
404 | $data = curl_exec($ch);
405 | curl_close($ch);
406 |
407 | $data = json_decode($data);
408 |
409 | return $data; // string or null
410 | }
411 |
412 |
413 | /*
414 | * Caches the results request to keep from hammering the API
415 | *
416 | * @param string $cache_path - A path to the cache file
417 | * @param string $data - The results from the API call - should be encoded
418 | */
419 | protected function cache_it($cache_path, $data)
420 | {
421 | if ( !isset($data) ) return;
422 | !file_exists($this->cache_dir) && mkdir($this->cache_dir);
423 | file_put_contents( $cache_path, json_encode($data) );
424 |
425 | return $cache_path;
426 | }
427 |
428 |
429 | /*
430 | * Determines whether the provided file has expired yet
431 | *
432 | * @param string $cache_path The path to the cached file
433 | * @param string $expires - In hours, how long the file should cache for.
434 | */
435 | protected function has_expired($cache_path, $expires = null)
436 | {
437 | if ( !isset($expires) ) $expires = $this->cache_expires;
438 |
439 | if ( file_exists($cache_path) ) {
440 | return time() - $expires * 60 * 60 > filemtime($cache_path);
441 | }
442 |
443 | return true;
444 | }
445 |
446 | /*
447 | * Helper function that deletes all of the files in your cache directory.
448 | */
449 | public function clear_cache(){
450 | array_map('unlink', glob("$this->cache_dir/*"));
451 | }
452 |
453 |
454 | /**
455 | * A simple convenience function to save a few seconds during development.
456 | *
457 | * @param $data The array or object to display on the page, for testing.
458 | */
459 | public function prettyPrint($data)
460 | {
461 | echo "
";
462 | print_r($data);
463 | echo "
";
464 | }
465 | }
466 |
--------------------------------------------------------------------------------
/index.php:
--------------------------------------------------------------------------------
1 | new_files('codecanyon', 'plugins');
10 |
11 | # See what we got back....
12 | $Envato->prettyPrint($account_info);
13 |
14 | ?>
15 |
16 |
17 |
18 |
19 | Popular Items Last Week
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
--------------------------------------------------------------------------------
/readme.md:
--------------------------------------------------------------------------------
1 | This class is a wrapper for the Envato Marketplace API. It provides easy-to-remember methods to view your items, search the marketplace,
2 | get personal, private data, view collections, etc. It also will automatically caches the results - so that you don't hammer the API needlessly!
3 |
4 | ### Usage
5 | First, include the class in your project.
6 |
7 | `require 'Envato_marketplaces.php';`
8 |
9 | Next, create a new instance of the class:
10 |
11 | `$Envato = new Envato_marketplaces();`
12 |
13 | And that's really it. You now have access to all of the available functions.
14 |
15 | ### Examples
16 |
17 | #### Quickly Echo Thumbnails of a User's Items
18 | $Envato = new Envato_marketplaces();
19 | $Envato->display_thumbs('your username', 'desired marketplace name', 'number to display');
20 |
21 | #### Search for Sliders Across all Marketplaces
22 | $Envato = new Envato_marketplaces();
23 | $sliders = $Envato->search('sliders');
24 | $Envato->prettyPrint($sliders);
25 |
26 | ##### Limit results to a particular site...
27 | $Envato = new Envato_marketplaces();
28 | $sliders = $Envato->search('sliders', 'codecanyon');
29 | $Envato->prettyPrint($sliders);
30 |
31 | ##### Limit further to a marketplace category...
32 | $Envato = new Envato_marketplaces();
33 | $sliders = $Envato->search('sliders', 'codecanyon', 'plugins');
34 | $Envato->prettyPrint($sliders);
35 |
36 | #### Get Account Balance
37 | require 'Envato_marketplaces.php';
38 | $Envato = new Envato_marketplaces( 'YOUR_API_KEY');
39 |
40 | # Echo out the balance.
41 | echo $Envato->balance('your username');
42 |
43 | #### Get Featured Item
44 | require 'Envato_marketplaces.php';
45 | $Envato = new Envato_marketplaces();
46 | $featured = $Envato->featured('codecanyon');
47 |
48 | # Just for reviewing available props
49 | $Envato->prettyPrint($featured);
50 |
51 | # Featured author
52 | echo $featured->featured_author->user;
53 |
54 | # Featured file
55 | echo $featured->featured_file->url;
56 |
57 | #Free file
58 | echo $featured->free_file->url;
59 |
60 | #### Get Specific Item Details
61 | require 'Envato_marketplaces.php';
62 | $Envato = new Envato_marketplaces();
63 |
64 | # Pass in item id -- see url on item page on the marketplace.
65 | $item = $Envato->item_details('232428');
66 |
67 | # Only for development purposes. Review available options.
68 | $Envato->prettyPrint($item);
69 | ?>
70 |
71 | item; ?>
72 |
73 |
74 |
75 |
76 | #### Display Your Own Recent Items
77 | require 'Envato_marketplaces.php';
78 | $Envato = new Envato_marketplaces();
79 |
80 | # marketplace name, desired category, optional limit (number of items to return)
81 | $files = $Envato->new_files_from_user('Your Username', 'desired marketplace name', 5);
82 |
83 | #See what we got back...
84 | $Envato->prettyPrint($files);
85 |
86 | # Display thumbnails and links to the item pages.
87 | foreach($files as $item) : ?>
88 |
89 |
90 |
91 |
92 |
93 | #### Get Your Recent Sales Data
94 | require 'Envato_marketplaces.php';
95 | $Envato = new Envato_marketplaces();
96 | $Envato->set_api_key('your api key');
97 | $sales = $Envato->recent_sales('your username', 'optional limit');
98 |
99 | # Review what we got back
100 | $Envato->prettyPrint($sales);
101 |
102 | #### Get Earnings By Month
103 | require 'Envato_marketplaces.php';
104 | $Envato = new Envato_marketplaces();
105 | $Envato->set_api_key('your api key');
106 |
107 | $monthly_earnings = $Envato->earnings_by_month('your username', 'optional limit');
108 |
109 | # For review...
110 | $Envato->prettyPrint($monthly_earnings);
111 |
112 | #### Get Your Account Info
113 | require 'Envato_marketplaces.php';
114 | $Envato = new Envato_marketplaces();
115 | $Envato->set_api_key('your api key');
116 |
117 | $account_info = $Envato->account_information('your username');
118 |
119 | # See what we got back....
120 | $Envato->prettyPrint($account_info);
121 |
122 | #### Get Trending Items on Marketplace
123 | require 'Envato_marketplaces.php';
124 | $Envato = new Envato_marketplaces();
125 |
126 | $pop = $Envato->most_popular_last_week('marketplace name');
127 |
128 | # Result Array
129 | $Envato->prettyPrint($pop);
130 |
131 | #### Verify a Purchase
132 | require 'Envato_marketplaces.php';
133 | $Envato = new Envato_marketplaces();
134 | $Envato->set_api_key('your api key');
135 |
136 | // Ensure that somebody bought your item.
137 | // If successful, $verify will be an object which
138 | // contains all of the purchase information.
139 | $verify = $Envato->verify_purchase('your username', 'buyer purchase code');
140 |
141 | // Quickie test.
142 | if ( isset($verify->buyer) ) echo 'bought';
143 | else echo 'did not buy';
144 |
145 | #### Set the Cache Directory
146 | require 'Envato_marketplaces.php';
147 | $Envato = new Envato_marketplaces();
148 |
149 | $Envato->cache_dir = 'path/to/directory'; // defaults to 'cache'
150 |
151 | #### Delete All Cached Files
152 | require 'Envato_marketplaces.php';
153 | $Envato = new Envato_marketplaces();
154 |
155 | // ...
156 |
157 | $Envato->clear_cache();
158 |
--------------------------------------------------------------------------------