Hey there, fellow developer! Ready to dive into the world of Hotmart API integration? You're in for a treat. Hotmart's API is a powerful tool that'll let you tap into their vast ecosystem of digital products and affiliate marketing. In this guide, we'll walk through building a robust PHP integration that'll have you managing products, sales, and commissions like a pro.
Before we jump in, make sure you've got:
Got all that? Great! Let's roll.
First things first: let's get you authenticated.
function getAccessToken($clientId, $clientSecret) { $ch = curl_init('https://api-hot-connect.hotmart.com/oauth/token'); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query([ 'grant_type' => 'client_credentials', 'client_id' => $clientId, 'client_secret' => $clientSecret ])); $response = curl_exec($ch); curl_close($ch); return json_decode($response, true); }
This function will fetch your access token. Remember to implement a refresh mechanism – these tokens don't last forever!
Now that we're authenticated, let's set up our HTTP client:
function makeApiRequest($endpoint, $accessToken, $method = 'GET', $data = null) { $ch = curl_init("https://api-hot-connect.hotmart.com$endpoint"); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, [ "Authorization: Bearer $accessToken", 'Content-Type: application/json' ]); if ($method === 'POST') { curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_POSTFIELDS, json_encode($data)); } $response = curl_exec($ch); $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE); curl_close($ch); if ($httpCode >= 400) { throw new Exception("API request failed with code $httpCode: $response"); } return json_decode($response, true); }
This function will handle our API calls, complete with error handling. Nice and clean!
Let's tackle some key features:
function getProductInfo($accessToken, $productId) { return makeApiRequest("/product/rest/v2/$productId", $accessToken); }
function getSales($accessToken, $startDate, $endDate) { return makeApiRequest("/sales/rest/v2?start_date=$startDate&end_date=$endDate", $accessToken); }
function handleWebhook() { $payload = file_get_contents('php://input'); $signature = $_SERVER['HTTP_X_HOTMART_SIGNATURE']; if (!verifySignature($payload, $signature)) { http_response_code(403); exit('Invalid signature'); } $data = json_decode($payload, true); // Process the webhook data } function verifySignature($payload, $signature) { // Implement signature verification logic here }
Want to level up? Here are some pro tips:
Always test your integration thoroughly. Set up unit tests for your API calls and don't forget to test error scenarios. When debugging, keep an eye on the API response codes and messages – they're your best friends when things go sideways.
Security is crucial. Here's what you need to do:
To keep your integration speedy:
And there you have it! You're now equipped to build a robust Hotmart API integration in PHP. Remember, the key to a great integration is continuous improvement. Keep an eye on Hotmart's documentation for updates, and don't be afraid to experiment with new features as they roll out.
Happy coding, and may your conversions be ever in your favor!