PHP 使用Envato API的示例

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP 使用Envato API的示例相关的知识,希望对你有一定的参考价值。

$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);
	}

}

以上是关于PHP 使用Envato API的示例的主要内容,如果未能解决你的问题,请参考以下文章

如何使用 PHP 和 GD 制作水印图像,如 envato photodune 预览图像?

Envato Theme check插件显示有关add_meta_box的警告

Envato主题检查

如何制作像 envato 这样的浮动侧边栏?

程序猿常用网站大全

如何将 Curl api 示例写入 PHP curl