Hey there, fellow developer! Ready to supercharge your PHP project with respond.io's powerful API? You're in the right place. This guide will walk you through creating a seamless integration that'll have you managing conversations like a pro in no time.
Before we dive in, make sure you've got:
Got those? Great! Let's get cracking.
First things first, let's set up our project:
composer require guzzlehttp/guzzle
Alright, time to get cozy with the respond.io API. Grab your API key from your respond.io dashboard and let's authenticate:
$apiKey = 'your_api_key_here'; $client = new GuzzleHttp\Client([ 'base_uri' => 'https://api.respond.io/v1/', 'headers' => [ 'Authorization' => 'Bearer ' . $apiKey, 'Content-Type' => 'application/json' ] ]);
Now that we're all set up, let's make some requests:
$response = $client->get('contacts'); $contacts = json_decode($response->getBody(), true);
$response = $client->post('messages', [ 'json' => [ 'channelId' => 'channel_id_here', 'contactId' => 'contact_id_here', 'message' => 'Hello from our PHP integration!' ] ]);
Let's implement some core functionality:
function sendMessage($channelId, $contactId, $message) { global $client; return $client->post('messages', [ 'json' => [ 'channelId' => $channelId, 'contactId' => $contactId, 'message' => $message ] ]); }
function getConversationHistory($conversationId) { global $client; return $client->get("conversations/{$conversationId}/messages"); }
function createContact($name, $phoneNumber) { global $client; return $client->post('contacts', [ 'json' => [ 'name' => $name, 'phoneNumber' => $phoneNumber ] ]); }
Don't let those pesky errors catch you off guard:
try { $response = $client->get('contacts'); } catch (GuzzleHttp\Exception\RequestException $e) { error_log('API request failed: ' . $e->getMessage()); }
Time to set up those webhooks:
$webhookData = json_decode(file_get_contents('php://input'), true); // Process webhook data if ($webhookData['event'] === 'message.created') { // Handle new message }
Let's make sure everything's working smoothly:
function testSendMessage() { $response = sendMessage('channel_id', 'contact_id', 'Test message'); assert($response->getStatusCode() === 200, 'Failed to send message'); } testSendMessage();
Remember to:
And there you have it! You've just built a solid respond.io API integration in PHP. Pretty cool, right? Remember, this is just the beginning. There's a whole world of possibilities with the respond.io API, so don't be afraid to explore and experiment.
For more in-depth info, check out the respond.io API documentation. Now go forth and build something awesome!