Posted By

dtbaker on 12/11/10


Tagged

api envato themeforest


Versions (?)

Who likes this?

1 person have marked this snippet as a favorite

elquemero


Example Using the Envato API


 / Published in: PHP
 

URL: http://dtbaker.com.au/random-bits/basic-usage-of-the-envato-api-plus-curl-.html

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.

  1. $envato = new envato();
  2. $files = $envato->get_user_items('dtbaker','codecanyon');
  3. $x=1;
  4. echo '<ul>';
  5. foreach($files as $file){
  6. if($x++>9)break;
  7. echo '<li>';
  8. echo '<a href="'.$file['url'].'?ref=dtbaker" title="download ' . urlencode($file['item']) . '" alt="download ' . urlencode($file['item']) . '">';
  9. echo $file['item'];
  10. echo '</a>';
  11. echo ' <span class="small">($'.$file['cost'].')</span>';
  12. echo '</li>';
  13. }
  14. echo '</ul>';
  15.  
  16.  
  17.  
  18. class envato{
  19.  
  20. private $_temp_folder;
  21. private $_ch;
  22.  
  23. public function __construct(){
  24. $this->_temp_folder = '/tmp/envato_'.md5($_SERVER['HTTP_HOST']).'/';
  25. if(!is_dir($this->_temp_folder)){
  26. mkdir($this->_temp_folder);
  27. }
  28. $this->_ch = curl_init();
  29. curl_setopt($this->_ch, CURLOPT_HEADER, false);
  30. curl_setopt($this->_ch, CURLOPT_RETURNTRANSFER, true);
  31. curl_setopt($this->_ch, CURLOPT_FOLLOWLOCATION, true);
  32. curl_setopt($this->_ch, CURLOPT_CONNECTTIMEOUT, 5);
  33. curl_setopt($this->_ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 5.01; Windows NT 5.0)");
  34. }
  35.  
  36. public function grab_url($url,$post_data=array(),$clean=false,$force=false){
  37. $cache_id = md5($url.serialize($post_data));
  38. $data = false;
  39. if(!$force){
  40. // check if the cache item exists.
  41. $filename = $this->_temp_folder.$cache_id;
  42. if(is_file($filename)){ // if cache entry exists
  43. if(filemtime($filename) > (time() - 172800)){ // check if it's older than 2 days
  44. $data = file_get_contents($filename); // grab the cached content.
  45. }
  46. }
  47. }
  48. if(!$data){
  49. // no cache item found, so we grab it via curl.
  50. curl_setopt($this->_ch, CURLOPT_URL, $url);
  51. if(count($post_data)){
  52. curl_setopt($this->_ch,CURLOPT_POST,true);
  53. curl_setopt($this->_ch,CURLOPT_POSTFIELDS,$post_data);
  54. }else{
  55. curl_setopt($this->_ch,CURLOPT_POST,false);
  56. }
  57. $data = curl_exec($this->_ch);
  58. }
  59. if(!$force){
  60. // save cache item.
  61. file_put_contents($filename,$data);
  62. }
  63. if($clean)$data = $this->_clean($data);
  64. return $data;
  65. }
  66.  
  67. public function get_item($item_id){
  68. $url = "http://themeforest.net/item/daves_api/$item_id";
  69. $data = $this->grab_url($url);
  70. $return = array();
  71. if(!empty($data)) {
  72. if(preg_match('#<a href=[^>]+><img alt="[^"]*" src="(http://[^"]+)" /></a>#msU',$data,$matches)){
  73. $return['large_thumb'] = $matches[1];
  74. }
  75. if(preg_match('#<div class="text">(.*)<div class="more-work"#msU',$data,$matches)){
  76. if(preg_match('#<div class="clear"><!--\s+--></div>\s+</div>(.*)</div>#msU',$matches[1],$text)){
  77. $return['content'] = $text[1];
  78. $return['content'] = preg_replace('#<img[^>]+>#imsU','',$return['content']);
  79. $return['content'] = preg_replace('#<h1>[^<]*</h1>#imsU','',$return['content']);
  80. }
  81. }
  82.  
  83. // we also have to get the theme screenshot thumbnails
  84. $url = "http://themeforest.net/theme_previews/$item_id-daves_api";
  85. $data = $this->grab_url($url,array(),true,false);
  86. if(preg_match_all('#index=(\d+)\'>\s*<img src="(http://[^"]+)" /></a#',$data,$matches)){
  87. //print_r($matches);
  88. $return['thumbs'] = array();
  89. foreach($matches[2] as $thumbnail){
  90. $return['thumbs'][$thumbnail] = str_replace('.__thumbnail','',$thumbnail);
  91. }
  92. }
  93.  
  94. }
  95. return $return;
  96. }
  97.  
  98. public function get_user_items($user,$marketplace='themeforest'){
  99. $url = "http://marketplace.envato.com/api/v2/new-files-from-user:$user,$marketplace.json";
  100. $data = $this->grab_url($url);
  101. if(!empty($data)) {
  102. $json_data = json_decode($data, true);
  103. $files = $json_data['new-files-from-user'];
  104. // sort files? meh.
  105. return $files;
  106. }
  107. return array();
  108. }
  109.  
  110.  
  111.  
  112. /** private functions **/
  113. private function posterize($post){
  114. $postv = '';
  115. foreach($post as $key=>$val){
  116. $postv .= $key .'='.$val.'&';
  117. }
  118. return $postv;
  119. }
  120. private function _clean($data){
  121. return preg_replace('/\s+/',' ',$data);
  122. }
  123.  
  124. }

Report this snippet  

You need to login to post a comment.