Created
January 28, 2015 07:41
-
-
Save Zenger/cce901dab8eb173c9d70 to your computer and use it in GitHub Desktop.
A small wrapper for Envato's API. You can use this to check if a user bought an item from you.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
class Envato | |
{ | |
protected static $key = ""; | |
protected static $username = ""; | |
public static function verify( $code ) | |
{ | |
if (empty(self::$key)) | |
{ | |
throw new Exception("No key was set"); | |
} | |
if (empty(self::$username)) | |
{ | |
throw new Exception("No username was set"); | |
} | |
$url = sprintf( "http://marketplace.envato.com/api/v3/%s/%s/verify-purchase:%s.json", self::$username, self::$key, $code ); | |
$ch = curl_init( $url ); | |
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5); | |
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); | |
$ch_data = curl_exec($ch); | |
curl_close($ch); | |
if (empty($ch_data)) | |
{ | |
throw new Exception("Couldn't connect to the server"); | |
} | |
$data = json_decode( $ch_data , true ); | |
if (empty($data['verify-purchase'])) | |
{ | |
throw new Exception("Invalid item purchase code"); | |
return false; | |
} | |
return true; | |
} | |
public static function set_key( $key ) | |
{ | |
self::$key = $key; | |
} | |
public static function set_username($username) | |
{ | |
self::$username = $username; | |
} | |
} | |
/* Example of use | |
Envato::set_key("yourapikey"); | |
Envato::set_username("YOUR_ENVATO_USERNAME"); | |
try { | |
$t = Envato::verify("4efd2738-02fd-4e2f-92f0-2b07149203d4"); | |
var_dump($t); | |
} | |
catch(Exception $e) | |
{ | |
echo $e->getMessage(); | |
} | |
*/ | |
?> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment