Revision: 37550
Initial Code
Initial URL
Initial Description
Initial Title
Initial Tags
Initial Language
at December 11, 2010 16:18 by dtbaker
Initial Code
$envato = new envato(); $files = $envato->get_user_items('dtbaker','codecanyon'); $x=1; echo '<ul>'; foreach($files as $file){ if($x++>9)break; echo '<li>'; echo '<a href="'.$file['url'].'?ref=dtbaker" title="download ' . urlencode($file['item']) . '" alt="download ' . urlencode($file['item']) . '">'; echo $file['item']; echo '</a>'; echo ' <span class="small">($'.$file['cost'].')</span>'; echo '</li>'; } echo '</ul>'; class envato{ private $_temp_folder; private $_ch; public function __construct(){ $this->_temp_folder = '/tmp/envato_'.md5($_SERVER['HTTP_HOST']).'/'; if(!is_dir($this->_temp_folder)){ mkdir($this->_temp_folder); } $this->_ch = curl_init(); curl_setopt($this->_ch, CURLOPT_HEADER, false); curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true); curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, 5); curl_setopt($this->_ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)"); } public function grab_url($url,$post_data=array(),$clean=false,$force=false){ $cache_id = md5($url.serialize($post_data)); $data = false; if(!$force){ // check if the cache item exists. $filename = $this->_temp_folder.$cache_id; if(is_file($filename)){ // if cache entry exists if(filemtime($filename) > (time() - 172800)){ // check if it's older than 2 days $data = file_get_contents($filename); // grab the cached content. } } } if(!$data){ // no cache item found, so we grab it via curl. curl_setopt($this->_ch, CURLOPT_URL, $url); if(count($post_data)){ curl_setopt($this->_ch,CURLOPT_POST,true); curl_setopt($this->_ch,CURLOPT_POSTFIELDS,$post_data); }else{ curl_setopt($this->_ch,CURLOPT_POST,false); } $data = curl_exec($this->_ch); } if(!$force){ // save cache item. file_put_contents($filename,$data); } if($clean)$data = $this->_clean($data); return $data; } public function get_item($item_id){ $url = "http://themeforest.net/item/daves_api/$item_id"; $data = $this->grab_url($url); $return = array(); if(!empty($data)) { if(preg_match('#<a href=[^>]+><img alt="[^"]*" src="(http://[^"]+)" /></a>#msU',$data,$matches)){ $return['large_thumb'] = $matches[1]; } if(preg_match('#<div class="text">(.*)<div class="more-work"#msU',$data,$matches)){ if(preg_match('#<div class="clear"><!--\s+--></div>\s+</div>(.*)</div>#msU',$matches[1],$text)){ $return['content'] = $text[1]; $return['content'] = preg_replace('#<img[^>]+>#imsU','',$return['content']); $return['content'] = preg_replace('#<h1>[^<]*</h1>#imsU','',$return['content']); } } // we also have to get the theme screenshot thumbnails $url = "http://themeforest.net/theme_previews/$item_id-daves_api"; $data = $this->grab_url($url,array(),true,false); if(preg_match_all('#index=(\d+)\'>\s*<img src="(http://[^"]+)" /></a#',$data,$matches)){ //print_r($matches); $return['thumbs'] = array(); foreach($matches[2] as $thumbnail){ $return['thumbs'][$thumbnail] = str_replace('.__thumbnail','',$thumbnail); } } } return $return; } public function get_user_items($user,$marketplace='themeforest'){ $url = "http://marketplace.envato.com/api/v2/new-files-from-user:$user,$marketplace.json"; $data = $this->grab_url($url); if(!empty($data)) { $json_data = json_decode($data, true); $files = $json_data['new-files-from-user']; // sort files? meh. return $files; } return array(); } /** private functions **/ private function posterize($post){ $postv = ''; foreach($post as $key=>$val){ $postv .= $key .'='.$val.'&'; } return $postv; } private function _clean($data){ return preg_replace('/\s+/',' ',$data); } }
Initial URL
http://dtbaker.com.au/random-bits/basic-usage-of-the-envato-api-plus-curl-.html
Initial Description
Script used to access the Envato API, and get extra details using Curl. This script will only work on PHP5 on a Linux hosting account (with safe mode / openbasedir disabled). Will need slight adjustment if trying to run it otherwise. You are welcome to use or modify this script, feel free to share your implementation of it.
Initial Title
Example Using the Envato API
Initial Tags
api
Initial Language
PHP