Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7780875
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T19:02:47+00:00 2026-06-01T19:02:47+00:00

I’m having the following two classes: Class I: <?php /** * Wrapper class for

  • 0

I’m having the following two classes:

Class I:

<?php

/**
 * Wrapper class for the Envato marketplaces API
*/ 


class envato_api_wrapper {

  protected   $api_key;
  protected   $cache_dir = 'cache';
  public      $cache_expires = 2;
  protected   $public_url = 'http://marketplace.envato.com/api/edge/set.json';


  public function __construct($api_key = null) {
    if ( isset($api_key) ) $this->api_key = $api_key; // allow the user to pass the API key upon instantiation
  }


 /**
  * Attach your API key. 
  *
  * @param string $api_key Can be accessed on the marketplaces via My Account 
  * -> My Settings -> API Key
  */
  public function set_api_key($api_key)
  {
     $this->api_key = $api_key;
  }


 /**
  * Retrieve the value of your API KEY, if needed.
  *
  * @return string The requested API Key.
  */
  public function get_api_key()
  {
     if ( ! isset($this->api_key) ) return 'No API Key is set.';
     return $this->api_key;
  }


 /**
  * Sets the cache directory for all API calls. 
  *
  * @param string $cache_dir 
  */
  public function set_cache_dir($cache_dir)
  {
     $this->cache_dir = $cache_dir;
  }


 /**
  * Retrieve the value of your cache directory, if needed.
  *
  * @return string The requested cache directory.
  */
  public function get_cache_dir()
  {
     return $this->cache_dir;
  }   


  /**
   * Available sets => 'vitals', 'earnings-and-sales-by-month', 'statement', 'recent-sales', 'account', 'verify-purchase', 'download-purchase'
   * 
   */ 
  public function private_user_data($user_name, $set, $purchase_code = null)
  {
     if ( ! isset($this->api_key) ) exit('You have not set an api key yet. $class->set_api_key(key)');
     if (! isset($set) ) return 'Missing parameters';

     $url = "http://marketplace.envato.com/api/edge/$user_name/$this->api_key/$set";
     if ( !is_null($purchase_code) ) $url .= ":$purchase_code";
     $url .= '.json';

     $result = $this->fetch($url);

     if ( isset($result->error) ) return 'Username, API Key, or purchase code is invalid.';
     return $result->$set;
  }


 /**
  * Can be used to verify if a person did in fact purchase your item.
  *
  * @param $user_name Author's username.
  * @param $purchase_code - The buyer's purchase code. See Downloads page for 
  * receipt.
  * @return object|bool If purchased, returns an object containing the details.
  */ 
  public function verify_purchase($user_name, $purchase_code)
  {
     $validity = $this->private_user_data($user_name, 'verify-purchase', $purchase_code);
     return isset($validity->buyer) ? $validity : false;
  }


 /**
  * Retrieve details for your most recent sales.
  *
  * @param string $user_name The username attached to your API KEY.
  * @param int $limit The number of sales to return.
  * @return array A list of your recent sales.
  */
  public function recent_sales($user_name, $limit = null)
  {
     $sales = $this->private_user_data($user_name, 'recent-sales');
     return $this->apply_limit($sales, $limit);
  }


 /**
  * Retrieve your account information -- balance, location, name, etc.
  *
  * @param string $user_name The username attached to your API KEY.
  * @return array A list of account information for the user.
  */
  public function account_information($user_name)
  {  
     $private_data = $this->private_user_data($user_name, 'account');
     return json_decode(json_encode($private_data), true);
  }


 /**
  * Grab quick monthly stats - number of sales, income, etc.
  *
  * @param string $user_name The username attached to your API KEY.
  * @param int $limit The number of months to return.
  * @return array A list of sales figures, ordered by month.
  */
  public function earnings_by_month($user_name, $limit = null)
  {
     $earnings = $this->private_user_data($user_name, 'earnings-and-sales-by-month');
     return $this->apply_limit($earnings, $limit);
  }


 /**
  * Generic method, to be used in combination with the marketplace API docs.
  *
  * @param string $user_name The user name of the seller to track.
  * @return array The returned data wrapped in an array.
  */
  public function public_user_data($user_name)
  {
     $url = preg_replace('/set/i', 'user:' . $user_name, $this->public_url);
     return $this->fetch($url, 'user');
  }


 /**
  * Retrieve the details for a specific marketplace item.
  *
  * @param string $item_id The id of the item you need information for. 
  * @return object Details for the given item.
  */
  public function item_details($item_id)
  {
     $url = preg_replace('/set/i', 'item:' . $item_id, $this->public_url);
     return $this->fetch($url, 'item');
  }


 /**
  * Similar to new_files, but focuses on a specific author's files.
  *
  * @param string $user_name The desired username.
  * @param string $marketplace_name The desired marketplace name.
  * @param int $limit The number of files to return.
  * @return array A list of recently added files by one user.
  */
  public function new_files_from_user($user_name, $marketplace_name = 'themeforest', $limit = null)
  {
     $cache_path = "$this->cache_dir/$user_name-$marketplace_name-new_files";

     $url = preg_replace('/set/i', 'new-files-from-user:' . $user_name . ',' . $marketplace_name, $this->public_url);

     return $this->apply_limit( $this->fetch($url, 'new-files-from-user'), $limit );
  }


  /**
  * Display the number of items the author has for sale by marketplace
  *
  * @param string $user_name The desired username.
  */
  public function user_items_by_site($user_name)
  {
     $cache_path = "$this->cache_dir/$user_name";

     $url = preg_replace('/set/i', 'user-items-by-site:' . $user_name, $this->public_url);

     return $this->fetch($url, 'user-items-by-site');
  }


 /**
  * Retrieves general marketplace member information.
  *
  * @param string $user_name The username to query.
  * @return object Contains the requested user information.
  */
  public function user_information($user_name)
  {
     $url = preg_replace('/set/i', 'user:' . $user_name, $this->public_url);
     return $this->fetch($url, 'user');
  }


  /*
  * Either fetches the desired data from the API and caches it, or fetches the cached version
  *
  * @param string $url The url to the API call
  * @param string $set (optional) The name of the set to retrieve. 
  */
  protected function fetch($url, $set = null) 
  {
     // Use the API url to generate the cache file name. 
     // So: http://marketplace.envato.com/api/edge/collection:739793.json
     // Becomes: collection-739793.json
     $cache_path = $this->cache_dir . '/' . str_replace(':', '-', substr(strrchr($url, '/'), 1));

     if ( $this->has_expired($cache_path) ) {
        // get fresh copy
        $data = $this->curl($url);

        if ($data) {
           $data = isset($set) ? $data->{$set} : $data; // if a set is needed, update
        } else exit('Could not retrieve data.');

        $this->cache_it($cache_path, $data);

        return $data;
     } else {
        // if available in cache, use that
        return json_decode(file_get_contents($cache_path));
     }
  }


 /**
  * Filters returned result, according to the supplied $limit.
  *
  * @param string $orig_arr The original array to work on.
  * @param int $limit Specifies the number of array items in the result.
  * @return array A new array with a count equal to the passed $limit.
  */
  public function apply_limit($orig_arr, $limit)
  {
     if ( !is_int($limit) ) return $orig_arr;

     // Make sure that there are enough items to filter through... 
     if ( $limit > count($orig_arr) ) $limit = count($orig_arr);

     $new_arr = array();
     for ( $i = 0; $i <= $limit - 1; $i++ ) {
        $new_arr[] = $orig_arr[$i];
     }
     return $new_arr;
  }


 /**
  * General purpose function to query the marketplace API.
  *
  * @param string $url The url to access, via curl.
  * @return object The results of the curl request.
  */
  protected function curl($url) 
  {
     if ( empty($url) ) return false;

     $ch = curl_init($url);
     curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

     $data = curl_exec($ch);
     curl_close($ch);

     $data = json_decode($data);

     return $data; // string or null
  }


  /*
  * Caches the results request to keep from hammering the API
  *
  * @param string $cache_path - A path to the cache file
  * @param string $data - The results from the API call - should be encoded
  */
  protected function cache_it($cache_path, $data)
  {
     if ( !isset($data) ) return;
     !file_exists($this->cache_dir) && mkdir($this->cache_dir);
     file_put_contents( $cache_path, json_encode($data) );

     return $cache_path;
  }


  /*
  * Determines whether the provided file has expired yet 
  *
  * @param string $cache_path The path to the cached file
  * @param string $expires - In hours, how long the file should cache for.
  */
  protected function has_expired($cache_path, $expires = null) 
  {
     if ( !isset($expires) ) $expires = $this->cache_expires;

     if ( file_exists($cache_path) ) {
        return time() - $expires * 60 * 60 > filemtime($cache_path);
     }

     return true;
  }


  /*
  * Helper function that deletes all of the files in your cache directory.
  */
  public function clear_cache(){
     array_map('unlink', glob("$this->cache_dir/*"));
   }
}

Class II:

<?php

/**
* Operation Class for Envato API Wrapper
*/

class envato_api_class {

    protected   $api_key;
    protected   $username;
    public      $envato_api_wrapper;
    public      $path_to_envato_api_wrapper;


    public function __construct($api_key = null, $username = null) {
        if ( isset($api_key) ) {
            $this->api_key = $api_key;
        } elseif ( isset($username) ) {
            $this->username = $username;
        }
        $this->path_to_envato_api_wrapper = 'envato_api_wrapper.php';
        require($this->path_to_envato_api_wrapper);
        $this->envato_api_wrapper = new envato_api_wrapper($api_key);
    }

    public function get_all_items_by_site() {
        $items_by_marketplace = $this->envato_api_wrapper->user_items_by_site($this->username);
        $marketplaces = array();
        foreach ($items_by_marketplace as $key) {
            $marketplaces[] = array(
                'Marketplace'  => $key->site,
                'Items'        => $key->items
            );
        }
        foreach ($marketplaces as $key) {
            $items[$key['Marketplace']] = $this->envato_api_wrapper->new_files_from_user($this->username, $key['Marketplace']);
        }
        return json_decode(json_encode($items), true);
    }

    public function get_all_items() {
        $items_by_marketplace = $this->envato_api_wrapper->user_items_by_site($this->username);
        $marketplaces = array();
        $items = array();
        $item = array();
        foreach ($items_by_marketplace as $key) {
            $marketplaces[] = array(
                'Marketplace'  => $key->site,
                'Items'        => $key->items
            );
        }
        foreach ($marketplaces as $key) {
            $items[$key['Marketplace']] = $this->envato_api_wrapper->new_files_from_user($this->username, $key['Marketplace']);
        }
        foreach ($items as $main_key) {
            foreach ($main_key as $secondary_key) {
                $item[] = array(
                    'ID'        => $secondary_key->id,
                    'Item'      => $secondary_key->item,
                    'URL'       => $secondary_key->url,
                    'User'      => $secondary_key->user,
                    'Thumb'     => $secondary_key->thumbnail,
                    'Sales'     => $secondary_key->sales,
                    'Rating'    => $secondary_key->rating,
                    'Cost'      => $secondary_key->cost,
                    'Uploaded'  => $secondary_key->uploaded_on,
                    'Tags'      => $secondary_key->tags,
                    'Category'  => $secondary_key->category,
                    'Preview'   => $secondary_key->live_preview_url
                );
            }
        }
        return $item;
    }

}

?>

And I’m trying to execute the following code in another file, three levels above where the classes are located:

<?php

                require 'assets/class/envato/envato_api_class.php';

                $api_key = 'some_key_here';

                $username = 'some_username_here';

                $envato = new envato_api_class($api_key, $username);

                $items = $envato->get_all_items();

                function rating($cluster) {
                    for ($i = 1; $i <= $cluster; $i++) {
                        echo '<img src="assets/gfx/filled-star.png" alt="Rating" />';
                    }
                    $empty = 5 - $cluster;
                    if ($empty > 0) {
                        for ($i = 0; $i < $empty; $i++) {
                            echo '<img src="assets/gfx/empty-star.png" alt="Rating" />';
                        }
                    }
                }

                // Adding Rows instead of altogether...
                $halfway = floor(count($items)/2);
                $count=0;

                echo '<div class="item-row">';

                foreach ($items as $key) {

                    // Check if we can add the second row
                    if($count==$halfway){
                        echo '</div><div class="item-row">';
                    }
                    $count++;

                    echo '<div class="product id-'.$key['ID'].'">';
                        echo '<div class="description">';
                            echo '<div class="thumb">';
                                echo '<i class="icon"><img src="'.$key['Thumb'].'" alt="Thumb"/><span class="preview" data-image-link="'.$key['Preview'].'"><img src="assets/gfx/zoom-icon.png" alt="Zoom"/></span></i>';
                            echo '</div>';
                            echo '<div class="info">';
                                echo '<div class="sales">';
                                    echo '<div class="icon">';
                                        echo '<img src="assets/gfx/sales-icon.png" alt="Sales Icon"/>';
                                    echo '</div>';
                                    echo '<div class="text">';
                                        echo '<p>'.$key['Sales'].'</p>';
                                    echo '</div>';
                                echo '</div>';
                                echo '<div class="rating">';
                                    rating($key['Rating']);
                                echo '</div>';
                            echo '</div>';
                        echo '</div>';

                        echo '<div class="purchase">';
                            echo '<div class="info">';
                                echo '<i class="icon"><span class="tooltip">$ '.$key['Cost'].'</span></i>';
                            echo '</div>';
                            echo '<div class="proceed">';
                                echo '<a class="button" href="'.$key['URL'].'?ref='.$username.'">Purchase</a>';
                            echo '</div>';
                        echo '</div>';
                    echo '</div>';
                }

                echo '</div>'; // End Item Row
            ?>

But when testing on localhost I get the following error:

Notice: Undefined property: stdClass::$user-items-by-site in C:\xampp\htdocs\Envato\assets\class\envato\envato_api_wrapper.php on line 233

Can someone tell me why am I getting that error ? What am I doing wrong there ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T19:02:49+00:00Added an answer on June 1, 2026 at 7:02 pm

    You get that error because of this line (198):

    return $this->fetch($url, 'user-items-by-site');
    

    In combination with line 233:

    $data = isset($set) ? $data->{$set} : $data; // if a set is needed, update
    

    You’re apparently trying to access a member that does not exist.

    Because the data comes from a remote source and is then parsed as JSON, we probably can’t help much more.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I'm having trouble keeping the paragraph square between the quote marks. In firefox the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.