PHP code example of crunchzapp / crunchzapp-php-sdk

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

    

crunchzapp / crunchzapp-php-sdk example snippets




use CrunchzApp\CrunchzApp;

$token = 'your-api-token';
$crunchz = new CrunchzApp($token);

// You can now use the $crunchz object to interact with the API
$response = $crunchz->channel()->health();

use CrunchzApp\CrunchzApp;

// Initialize the service
$crunchz = app(CrunchzApp::class);

// Send a text message
$response = $crunchz->channel()
    ->contact('[email protected]')
    ->text('Hello, World!')
    ->send();

// Send an image with caption
$response = $crunchz->channel()
    ->contact('[email protected]')
    ->image('https://example.com/image.jpg', 'Check this out!')
    ->send();

// Send a location
$response = $crunchz->channel()
    ->contact('[email protected]')
    ->location(-6.2088, 106.8456, 'Jakarta, Indonesia')
    ->send();

// Send a video with caption
$response = $crunchz->channel()
    ->contact('[email protected]')
    ->video('https://example.com/video.mp4', 'Amazing video!')
    ->send();

// Send multiple messages in parallel
$responses = $crunchz->channel()
    ->contact('[email protected]')
    ->text('First message')
    ->contact('[email protected]')
    ->text('Second message')
    ->sendPool();

foreach ($responses as $response) {
    echo "Path: {$response['path']}\n";
    echo "Result: " . json_encode($response['result']) . "\n";
}

// Generate and send OTP code
$otpService = $crunchz->otp('code');

$response = $otpService
    ->contact('[email protected]')
    ->send();

// Validate OTP code
$validationResponse = $otpService
    ->contact('[email protected]')
    ->validate('123456');

// Alternative: Set code first, then validate
$validationResponse = $otpService
    ->contact('[email protected]')
    ->validate('123456');

// Generate and send OTP link
$otpService = $crunchz->otp('link');

$response = $otpService
    ->contact('[email protected]')
    ->send();

// Get all contacts
$contacts = $crunchz->channel()
    ->allContact()
    ->send();

// Get contact details
$contactDetails = $crunchz->channel()
    ->detail('[email protected]')
    ->send();

// Get contact picture
$contactPicture = $crunchz->channel()
    ->picture('[email protected]')
    ->send();

// Get all chats
$chats = $crunchz->channel()
    ->allChat()
    ->send();

// Get chat details
$chatDetails = $crunchz->channel()
    ->chatDetail('[email protected]')
    ->send();

// Archive a chat
$crunchz->channel()
    ->archiveChat('[email protected]')
    ->send();

// Unarchive a chat
$crunchz->channel()
    ->unArchiveChat('[email protected]')
    ->send();

// Get all groups
$groups = $crunchz->channel()
    ->allGroup()
    ->send();

// Create a new group
$newGroup = $crunchz->channel()
    ->createGroup('My Group', [
        '[email protected]',
        '[email protected]'
    ])
    ->send();

// Get group participants
$participants = $crunchz->channel()
    ->participants('group_id_here')
    ->send();

// React to a message
$crunchz->channel()
    ->contact('[email protected]')
    ->react('message_id_here', '👍')
    ->send();

// Send a poll
$crunchz->channel()
    ->contact('[email protected]')
    ->polling('What\'s your favorite color?', [
        'Red', 'Blue', 'Green', 'Yellow'
    ], false) // false = single answer, true = multiple answers
    ->send();

// Start/stop typing indicator
$crunchz->channel()
    ->contact('[email protected]')
    ->startTyping()
    ->send();

$crunchz->channel()
    ->contact('[email protected]')
    ->stopTyping()
    ->send();

use CrunchzApp\CrunchzApp;
use RuntimeException;
use InvalidArgumentException;

try {
    $response = $crunchz->channel()
        ->contact('invalid_contact')
        ->text('Hello')
        ->send();
} catch (InvalidArgumentException $e) {
    // Handle validation errors (empty messages, invalid contact IDs, etc.)
    echo "Validation Error: " . $e->getMessage();
} catch (RuntimeException $e) {
    // Handle API errors (network issues, authentication, etc.)
    echo "API Error: " . $e->getMessage();
}
bash
composer 
bash
    php artisan vendor:publish --tag=crunchzapp-config