PHP code example of directsaz / sdk

1. Go to this page and download the library: Download directsaz/sdk 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/ */

    

directsaz / sdk example snippets


use Directsaz\Sdk\Client;

$ds = Client::createDefault(
    apiKey: getenv('DIRECTSAZ_API_KEY'),
    baseUrl: 'https://developers.directsaz.com/api/v1',
);

$ds->sendDm($recipientId, 'Hello');
$ds->sendImage($recipientId, 'https://cdn.example.com/a.jpg', [
    ['title' => 'Open', 'url' => 'https://example.com'],
]);
$ds->sendCarousel($recipientId, [/* cards */]);
$ds->privateReply($commentId, 'Thanks!');
$ds->commentReply($commentId, 'Public reply');
$ds->like($recipientId, $messageId);
$ds->listPosts(limit: 30);
$ds->followStatus($userId);

use Directsaz\Sdk\Exception\ApiException;

try {
    $ds->sendDm($recipientId, str_repeat('x', 1500));
} catch (ApiException $e) {
    $e->statusCode;   // 422
    $e->errors();     // [['field' => 'text', 'rule' => 'max_bytes', 'msg' => '…got 1500']]
    $e->rawBody;      // the response body, useful when it was not valid JSON
}

$ds->sendDm($recipientId, 'Hello');
$ds->lastStatusCode();   // 202 = accepted for delivery

// routes/api.php
Route::match(['get', 'post'], '/hooks/directsaz', [\App\Http\Controllers\DirectsazHook::class, 'handle']);

// app/Http/Controllers/DirectsazHook.php
use Directsaz\Sdk\Laravel\WebhookController;
use Directsaz\Sdk\Client;

class DirectsazHook extends WebhookController
{
    protected function onEvents(array $events, $request): void
    {
        $client = Client::createDefault(env('DIRECTSAZ_API_KEY'));
        foreach ($events as $event) {
            if (($event['type'] ?? '') === 'dm') {
                $rid = $event['actions']['dm']['recipient_id'] ?? null;
                if ($rid) {
                    $client->sendDm($rid, 'Got it');
                }
            }
        }
    }
}

use Directsaz\Sdk\Webhook;

$token = getenv('DIRECTSAZ_WEBHOOK_TOKEN');

if ($_SERVER['REQUEST_METHOD'] === 'GET') {
    $r = Webhook::handleChallenge($_SERVER['HTTP_X_WEBHOOK_TOKEN'] ?? null, $token);
    http_response_code($r['status']);
    echo $r['body'];
    exit;
}

$raw = file_get_contents('php://input');
if (!Webhook::verifySignature($token, $raw, $_SERVER['HTTP_X_DIRECTSAZ_TIMESTAMP'] ?? null, $_SERVER['HTTP_X_DIRECTSAZ_SIGNATURE'] ?? null)) {
    http_response_code(401);
    exit('invalid signature');
}
$events = Webhook::parseEvents($raw);
http_response_code(200);
echo json_encode(['ok' => true]);