PHP code example of honkio / honkio-php

1. Go to this page and download the library: Download honkio/honkio-php library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

honkio / honkio-php example snippets




use Honkio\Exception\ApiException;
use Honkio\Honkio;

$honkio = new Honkio(getenv('HONKIO_API_KEY'));

try {
    $message = $honkio->messages->send([
        'from' => '+1416XXXXXXX', // one of your HonkIO numbers
        'to' => '+1613XXXXXXX',   // a Canadian number you hold consent for
        'body' => 'Hello from HonkIO!',
    ], idempotencyKey: 'order-1042');

    echo $message['id'], ' ', $message['status'], "\n";
} catch (ApiException $e) {
    echo $e->errorCode, ': ', $e->getMessage(), "\n"; // e.g. NO_CONSENT
}

$email = $honkio->emails->send([
    'from' => 'Acme <[email protected]>',
    'to' => '[email protected]',
    'subject' => 'Hello from HonkIO',
    'html' => '<p>It works.</p>',
]);

try {
    $honkio->verify->check($id, ['code' => '123456']);
} catch (Honkio\Exception\ApiException $e) {
    if ($e->errorCode === 'VERIFICATION_INVALID_CODE') {
        echo $e->details['attempts_remaining'];
    }
}

$honkio = new Honkio($key, ['max_retries' => 4, 'max_retry_delay' => 30]);
$noRetries = new Honkio($key, ['max_retries' => 0]);

$limits = $honkio->sendLimit->get();
echo $limits['remaining'], ' of ', $limits['daily_limit'], " left today\n";
foreach ($limits['number_queues'] as $queue) {
    // parts_per_minute is 10 for a local number; clears_at is null when nothing is waiting.
    echo $queue['phone_number'], ' ', $queue['queued_parts'], ' ', $queue['parts_per_minute'], ' ', $queue['clears_at'] ?? '-', "\n";
}

$honkio->emails->send($params, idempotencyKey: 'welcome-user-42');

$honkio->consents->create(['phone_number' => '+1613XXXXXXX', 'consent_type' => 'express', 'source_description' => 'Booking form']);
$honkio->consents->check(['phone_number' => '+1613XXXXXXX']);
$honkio->consents->revoke(['email_address' => '[email protected]']); // returns null

$address = $honkio->emails->received->address();
$page = $honkio->emails->received->list(['limit' => 10]);
$mime = $honkio->emails->received->raw($page['data'][0]['id']); // string

use Honkio\Exception\SignatureVerificationException;
use Honkio\Webhook;

$rawBody = file_get_contents('php://input');

try {
    $event = Webhook::verify($rawBody, getallheaders(), getenv('HONKIO_WEBHOOK_SECRET'));
} catch (SignatureVerificationException $e) {
    http_response_code(400);
    exit;
}

if ($event['type'] === 'message.delivered') {
    // ...
}

use GuzzleHttp\Client;
use GuzzleHttp\Handler\MockHandler;
use GuzzleHttp\HandlerStack;
use GuzzleHttp\Psr7\Response;

$mock = new MockHandler([new Response(200, ['Content-Type' => 'application/json'], '{"id":"msg_1"}')]);
$honkio = new Honkio\Honkio('test-key', ['http_client' => new Client(['handler' => HandlerStack::create($mock)])]);