├── .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 "