monetization

Setup the new Envato API to check Product Codes with PHP

I recently had to update our Support Centre application to use the new Envato API and I thought it would be a good time to make a post on how to use it to check for valid Product Codes.

Product Codes are used to verify purchases of your Buyers. When you get support requests/emails about your products, you don't want to be offering free support to people who are using your software which has been pirated. One way to verify a buyer is to ask them to give you their Product Code which comes in the download file. This code is unique to each and every purchase.

Also, Envato has changed it so that support for items now only last 6 months by default and require you to pay extra after that for extended support. Using the Envato API, you can check when the user's support status runs out.

So let's begin implementing a solution that checks for valid product codes and returns when the Support for the item runs outs. We are going to be using PHP's CURL library to send requests Envato's API, so you will need this enabled on your server.

Envato Personal Token

The first thing you need to obtain is an Envato Personal Token.

You can easily generate one here: https://build.envato.com/api/

You will need to sign into your Envato account. Once signed in, click on the My Apps link and scroll to the bottom to create a personal token. Select all the checkboxes for access needed and then copy the code into a text document to keep safe.

Once you have the token, let's write some code!

PHP Implementation

The API URL we need to send our request to is:

https://api.envato.com/v3/market/author/sale?code={$code}

To send a request, we can use PHP's CURL library. Let's initialise our code:

<?php
$product_code = "";
$url = "https://api.envato.com/v3/market/author/sale?code=".$product_code;
$curl = curl_init($url);
?>

The example above sets the URL to visit to the Envato API sale check. The $product_code variable needs to contain the purchase code of one of our items. You can usually ask one of your customers to give you one to help you test. Note that this is different from the Personal Token code we generated earlier (we'll need this next).

Now we need to set the headers for our request. The headers just tell the Envato API that we are a legitimate user looking to request data. In order to do this, we need to use our Personal Token to gain access. To set headers, we use the following code:

<?php

$personal_token = "idfhsdfs9d89fs89df89s8d9f89";
$header = array();
$header[] = 'Authorization: Bearer '.$personal_token;
$header[] = 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.11; rv:41.0) Gecko/20100101 Firefox/41.0';
$header[] = 'timeout: 20';
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HTTPHEADER,$header);

?>

The $personal_token variable needs to contain our Personal Token we generated. The other headers just help us appear as a legitimate user and don't need to be changed. Finally, we need to execute our CURL command and get the response back from Envato. It's simple enough to do:

<?php

$envatoRes = curl_exec($curl);
curl_close($curl);
$envatoRes = json_decode($envatoRes);

?>

  The response from Envato is also a JSON object. We need to use PHP's json_decode to turn it into a PHP object that we can use. If the request is successful, the $envatoRes result will contain an object with the data of the item that was purchased, including the support date.

<?php

$date = new DateTime($envatoRes->supported_until);
$result = $date->format('Y-m-d H:i:s');

if (isset($envatoRes->item->name)) {   
    $data = " - VERIFIED ({$envatoRes->item->name}) (Supported: {$result})";
} else {  
    $data= " FAILED";
} 

echo $data;

?>

The above code first takens the Supported_until arguement of the EnvatoRes result and turns it into a readable date format for easy viewing.

Next we check that the item name is present in the object, and if it is, we save the result to a string and also print the support result.

You can then use $data to check whether the result contains your Item name and the support date is valid.

Hope that helps :)



Enjoyed that? Check These Posts Out

Setup Amazon Affiliate API with PHP - Product Advertising API 5.0 (2020)

Fullcalendar with PHP and CodeIgniter - Database Events - Part 2

Sager NP8377 Gaming Laptop Review

Acer Aspire 5 Slim A515-43-R19L Laptop Product Review

...
monetization

Article Comments

Let us know your thoughts below by adding a quick comment!

14/06/2017

Tapan Kumer Das

Great! You describe everything so easily. Thanks. I was searching for this kind of explanation. I will test this tonight and hope everything will be ok.

Thanks :)

Reply

14/06/2017

Liam Foster

Hmm i'm still confused on how to use your code above! I would like to extract theme price and author

Reply

14/06/2017

Liam Foster

Update:
I have a WordPress theme directory on my site, and a envato affiliate. i would like to extract the theme author and theme price.

Reply

Leave A Comment