Docs / PHP SDK

PHP SDK

Official PHP SDK for APIMW

PHP SDK

Official PHP SDK for APIMW API integration.

Requirements

  • PHP 8.1 or higher
  • Composer
  • ext-json
  • ext-curl

Installation

composer require apimw/apimw-php

Quick Start

<?php

require 'vendor/autoload.php';

use APIMW\Client;
use APIMW\WhatsApp\Message;

// Initialize client
$client = new Client([
    'api_key' => 'your_api_key',
    'api_secret' => 'your_api_secret'
]);

// Send a WhatsApp message
$response = $client->whatsapp->sendMessage([
    'to' => '+1234567890',
    'message' => 'Hello from PHP!'
]);

echo $response->messageId;

Configuration

$client = new Client([
    'api_key' => getenv('APIMW_API_KEY'),
    'api_secret' => getenv('APIMW_API_SECRET'),
    'timeout' => 30,
    'retry' => [
        'max_attempts' => 3,
        'delay' => 1000
    ]
]);

Sending Messages

Text Message

$response = $client->whatsapp->sendMessage([
    'to' => '+1234567890',
    'message' => 'Hello World!'
]);

Template Message

$response = $client->whatsapp->sendTemplate([
    'to' => '+1234567890',
    'template' => 'order_confirmation',
    'language' => 'en',
    'components' => [
        [
            'type' => 'body',
            'parameters' => [
                ['type' => 'text', 'text' => 'John'],
                ['type' => 'text', 'text' => 'ORD-123']
            ]
        ]
    ]
]);

Webhooks

Handling Webhooks

use APIMW\Webhook\Handler;

$handler = new Handler('your_webhook_secret');

$handler->on('message.received', function($event) {
    $from = $event->data->from;
    $text = $event->data->text;

    // Process incoming message
    echo "Message from {$from}: {$text}";
});

$handler->handle();

Error Handling

use APIMW\Exceptions\ApiException;
use APIMW\Exceptions\RateLimitException;

try {
    $response = $client->whatsapp->sendMessage([...]);
} catch (RateLimitException $e) {
    $retryAfter = $e->getRetryAfter();
    sleep($retryAfter);
    // Retry...
} catch (ApiException $e) {
    echo "Error: " . $e->getMessage();
    echo "Code: " . $e->getErrorCode();
}

Laravel Integration

// config/services.php
'apimw' => [
    'key' => env('APIMW_API_KEY'),
    'secret' => env('APIMW_API_SECRET'),
],

// AppServiceProvider.php
$this->app->singleton(Client::class, function () {
    return new Client([
        'api_key' => config('services.apimw.key'),
        'api_secret' => config('services.apimw.secret')
    ]);
});