Envato API PHP Code Samples

Here’s some code samples for working with the new Envato API

These will help people migrate as the old API is getting shut down.

Simple verify-purchase through the new API

https://gist.github.com/dtbaker/67b2e969f24d4c18b80b

WordPress oAuth PHP Class Example

This is a simple PHP class for interacting with the Envato API within a WordPress plugin. It uses some of the WordPress functions like wp_remote_get()

Simple usage example:

$envato_api = envato_api_basic::getInstance();
$envato_api->set_client_id('YOUR_CLIENT_ID_HERE');
$envato_api->set_client_secret('YOUR_CLIENT_SECRET_HERE');
$envato_api->set_redirect_url('http://yourwebsite.com/path/to/this/php/script.php');
$code = !empty($_GET['code']) ? $_GET['code'] : false;
if($code && $token = $envato_api->get_authentication($code) && $envato_api_result = $envato_api->api('v1/market/private/user/username.json')){
   // user is logged in with oAuth
   if(!empty($envato_api_result['username'])){
      echo "Welcome ".htmlspecialchars($envato_api_result['username']);
   }else{
      echo "API Error, unable to get username.";
   }
}else{
   $login_url = $envato_api->get_authorization_url();
   echo '<a href="'.esc_url($login_url).'" target="_blank"> Login with Envato </a>';
}

 

https://gist.github.com/dtbaker/474376c2147937c748be

 

Drupal oAuth PHP Class Example

This was created by Envato user sowailem.  It uses some special drupal code so probably best to only be used inside a drupal environment. However it’s a great example of a class and can be pulled apart to adapt to other platforms.

https://gist.github.com/dtbaker/a9da680fec68d429f65e

 

Leave a Reply

Your email address will not be published. Required fields are marked *