Hey there, fellow code wranglers! Ready to dive into the world of Interact API integration? You're in for a treat. We'll be walking through the process of building a robust PHP integration that'll have you interacting with Interact like a pro in no time.
Before we jump in, let's make sure we've got our ducks in a row:
First things first, let's get our environment ship-shape:
composer require guzzlehttp/guzzle
Now, let's stash those API credentials somewhere safe. Create a .env
file and add:
INTERACT_API_KEY=your_api_key_here
INTERACT_API_SECRET=your_api_secret_here
Time to get our hands dirty! Let's create an API client:
use GuzzleHttp\Client; $client = new Client([ 'base_uri' => 'https://api.interact.io/v2/', 'headers' => [ 'Authorization' => 'Bearer ' . getenv('INTERACT_API_KEY'), 'Content-Type' => 'application/json', ], ]);
Let's grab some data, shall we?
$response = $client->get('contacts'); $contacts = json_decode($response->getBody(), true);
Time to add a new contact:
$newContact = [ 'name' => 'Jane Doe', 'email' => '[email protected]', ]; $response = $client->post('contacts', ['json' => $newContact]);
Oops, Jane got married. Let's update her name:
$updatedContact = ['name' => 'Jane Smith']; $response = $client->put('contacts/123', ['json' => $updatedContact]);
Farewell, Jane:
$response = $client->delete('contacts/123');
Let's not let those pesky errors slip through the cracks:
try { $response = $client->get('contacts'); } catch (\GuzzleHttp\Exception\RequestException $e) { error_log('API request failed: ' . $e->getMessage()); }
Interact's got your back with pagination. Here's how to handle it:
$page = 1; do { $response = $client->get('contacts', ['query' => ['page' => $page]]); $contacts = json_decode($response->getBody(), true); // Process $contacts... $page++; } while (!empty($contacts));
As for rate limiting, be a good API citizen:
$response = $client->get('contacts'); $remainingRequests = $response->getHeader('X-RateLimit-Remaining')[0]; if ($remainingRequests < 10) { sleep(60); // Take a breather }
Interact's webhooks are like a bat-signal for your app. Here's how to catch 'em:
$payload = file_get_contents('php://input'); $event = json_decode($payload, true); switch ($event['type']) { case 'contact.created': // Handle new contact break; // Handle other event types... }
Don't forget to test! Here's a quick unit test example:
public function testGetContacts() { $client = $this->createMock(Client::class); $client->expects($this->once()) ->method('get') ->with('contacts') ->willReturn(new Response(200, [], json_encode(['data' => []]))); $api = new InteractApi($client); $contacts = $api->getContacts(); $this->assertIsArray($contacts); }
Remember, caching is your friend:
$cacheKey = 'contacts_list'; if ($cache->has($cacheKey)) { $contacts = $cache->get($cacheKey); } else { $contacts = $api->getContacts(); $cache->set($cacheKey, $contacts, 3600); // Cache for an hour }
And there you have it, folks! You're now armed and dangerous with Interact API integration skills. Remember, the API is your oyster - so get out there and build something awesome!
For more in-depth info, check out the Interact API docs. Now go forth and integrate!