PHP code example of baileyherbert / envato

1. Go to this page and download the library: Download baileyherbert/envato library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

baileyherbert / envato example snippets


$token = new Herbert\Envato\Auth\Token('Your Personal Token');
$client = new Herbert\EnvatoClient($token);

// Generate the OAuth object
$oauth = new Herbert\Envato\Auth\OAuth([
    'client_id' => 'Your client id',
    'client_secret' => 'Your client secret',
    'redirect_uri' => 'https://your-redirect-uri.com/',
    'store' => function(string $session) {
        // Example: Save the OAuth credentials to a PHP session
        // This is just a JSON-encoded string that looks like below:
        // {"access_token": "...", "refresh_token": "...", "expires": 1710526960}

        $_SESSION['envato_oauth'] = $session;
    }
]);

// Example: Restore existing OAuth credentials from a PHP session
if (isset($_SESSION['envato_oauth'])) {
    $oauth->load($_SESSION['envato_oauth']);
}

// Get the client (will return NULL if not authenticated)
// This will auto-detect the "code" query parameter on the current request
// If found, it will attempt to create a token and instantiate the client
$client = $oauth->getClient();

// Redirect the user if they're not authorized yet
if (!$client) {
    header("Location: " . $oauth->getAuthorizationUri());
    die;
}

// Example: Get the user's unique Envato Account ID
// This sends a request to the Envato API, so don't call it often!
$userId = $client->getUserId(); // int(1908998)

// Example: Request the user's username
// This sends a request to the Envato API, so don't call it often!
$response = $client->user->username();
$username = $response->username; // string(13) "baileyherbert"

$response = $client->user->username();

if (!$response->error) {
    $username = $response->results['username'];

    echo "Logged in as {$username}";
}
else {
    echo "Error: {$response->error}";
}

$response = $client->profile->portfolio([
    'username' => 'baileyherbert'
]);

use Herbert\Envato\Exceptions\TooManyRequestsException;

try {
    $item = $client->catalog->item(['id' => 1234567]);
}
catch (TooManyRequestsException $e) {
    // Get the number of seconds remaining (float)
    $secondsRemaining = $e->getSecondsRemaining();

    // Get the timestamp for when we can make our next request
    $timestamp = $e->getRetryTime();

    // Sleep until the rate limiting has ended
    $e->wait();
}

$client->request->get('/v1/market/user:{username}.json', [
  'username' => 'collis'
]);

$client->catalog->collection(['id' => 12345]);

$client->catalog->item(['id' => 12345]);

$client->catalog->item_version(['id' => 12345]);

$client->catalog->items(['site' => 'codecanyon.net', 'term' => '']);

$client->catalog->comments(['item_id' => 12345]);

$client->catalog->popular(['site' => 'codecanyon']);

$client->catalog->categories(['site' => 'codecanyon']);

$client->catalog->prices(['item_id' => 12345]);

$client->catalog->newest(['site' => 'codecanyon', 'category' => 'php-scripts']);

$client->catalog->featured(['site' => 'codecanyon']);

$client->catalog->random(['site' => 'codecanyon']);

$client->profile->collections();

$client->profile->collection(['id' => 12345]);

$client->profile->details(['username' => 'baileyherbert']);

$client->profile->badges(['username' => 'baileyherbert']);

$client->profile->portfolio(['username' => 'baileyherbert']);

$client->profile->newest(['username' => 'baileyherbert', 'site' => 'codecanyon']);

$client->user->sales();

$client->user->sale(['code' => '*****']);

$client->user->purchases();

$client->user->purchase(['code' => '*****']);

$client->user->download(['purchase_code' => '*****']);
$client->user->download(['item_id' => '123456']);

$client->user->details();

$client->user->username();

$client->user->email();

$client->user->earnings();

$client->user->statement([
  'page' => 1,
  'from_date' => '2021-02-01',
  'to_date' => '2022-06-21',
  'type' => 'Sale',
  'site' => 'codecanyon.net'
]);

$client->market->users();

$client->market->items();

$client->market->site(['site' => 'codecanyon']);

$identity = $client->getIdentity();

object(stdClass)#1 (4) {
  ["clientId"]=> NULL
  ["userId"]=> int(1908998)
  ["scopes"]=> array(18) {
    [0]=> string(7) "default"
    [1]=> string(13) "user:username"
    [2]=> string(10) "user:email"
    [3]=> string(12) "user:account"
    [4]=> string(14) "user:financial"
    [5]=> string(17) "purchase:download"
    [6]=> string(12) "sale:history"
    [7]=> string(11) "sale:verify"
  }
  ["ttl"]=> int(315360000)
}

$userId = $client->getUserId(); // int(1908998)

$purchase_code = 'purchase code here';
$token = new Herbert\Envato\Auth\Token('Your Personal Token');
$client = new Herbert\EnvatoClient($token);

$response = $client->user->sale(['code' => $purchase_code]);

if (!$response->error) {
    $sale = $response->results;

    $sold_at = $sale['sold_at']; // "2013-04-16T01:59:35+10:00"
    $license = $sale['license']; // "Regular License"
    $supported_until = $sale['supported_until']; // "2013-10-16T00:00:00+10:00"

    $item_id = $sale['item']['id']; // 1252984
    $item_name = $sale['item']['name']; // "Item Name"
    $author_username = $sale['item']['author_username']; // "baileyherbert"

    $num_licenses = $sale['purchase_count']; // 3
    $buyer_username = $sale['buyer']; // "bestbuyerever"

    echo "I got information!";
}
else {
    echo "The code produced an error:\n";
    echo $response->error;
}