PHP code example of cmtelecom / messaging-php

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

    

cmtelecom / messaging-php example snippets




use GuzzleHttp\Client as GuzzleClient;
use Http\Adapter\Guzzle6\Client as GuzzleAdapter;

$message = (new \CM\Messaging\Message())
    ->setFrom('Your name/company name')
    ->setTo(['0031612345678', '0031623456789', '0031634567890'])
    ->setBody('Your message');

try {
    $adapter = new GuzzleAdapter(new GuzzleClient());
    $client  = new \CM\Messaging\Client($adapter, 'your-product-token');
    $result  = $client->send($message);
    
    if($result->isAccepted()){
        // All messages were accepted
    } else {
        // Not all messages were accepted
    }
} catch (\CM\Messaging\Exception\BadRequestException $e) {
    // The request failed because of an invalid value, all messages has not been send
} catch (\Http\Client\Exception\TransferException $e) {
    // Something unexpected happened
}

 $client = new \CM\Messaging\Client('your-product-token');
 

use \CM\Messaging\Settings\AllowedChannel;

 $message = (new \CM\Messaging\Message())
        ->setFrom('Your name/company name')
        ->setTo(['0031612345678', '0031623456789', '0031634567890'])
        ->setBody('Message')
        ->setReference('Your message')
        ->setAllowedChannels([AllowedChannel::SMS, AllowedChannel::PUSH, AllowedChannel::VOICE])
        ->setAppKey('your-app-key')
        ->setMinimumNumberOfMessageParts(1)
        ->setMaximumNumberOfMessageParts(8)
        ->setDcs(8);
 

 $message = (new \CM\Messaging\Message())
         ->setFrom('Your name/company name')
         ->setTo(['0031612345678'])
         ->setBody('Message body');

 try {
    $adapter = new GuzzleAdapter(new GuzzleClient());
    $client  = new \CM\Messaging\Client($adapter, 'your-product-token');
    $result  = $client->send($message);
} catch (\CM\Messaging\Exception\BadRequestException $e) {
    // The request failed because of an invalid value, all messages has not been send
} catch (\Http\Client\Exception\TransferException $e) {
    // Something unexpected happened
}
 

$message_1 = (new \CM\Messaging\Message())
        ->setFrom('Your name/company name')
        ->setTo(['0031612345678'])
        ->setBody('Message one');
$messages[] = $message_1;

$message_2 = (new \CM\Messaging\Message())
        ->setFrom('Your name/company name')
        ->setTo(['0031623456789', '0031634567890'])
        ->setBody('Message two');
$messages[] = $message_2; 

try {
    $adapter = new GuzzleAdapter(new GuzzleClient());
    $client  = new \CM\Messaging\Client($adapter, 'your-product-token');
    $result  = $client->send($messages);
} catch (\CM\Messaging\Exception\BadRequestException $e) {
    // The request failed because of an invalid value, all messages has not been send
} catch (\Http\Client\Exception\TransferException $e) {
    // Something unexpected happened
}
 

try {
    $result = $client->send($messages);
    
    // returns true if all messages are accepted
    $result->isAccepted() 
    
    // returns true if all messages are failed
    $result->isFailed()
    
    // returns an array with the accepted message responses
    $result->getAccepted() 
    
    // returns an array with the failed message responses 
    $result->getFailed()
    
} catch (\CM\Messaging\Exception\BadRequestException $e) {
    // The request failed because of an invalid value, all messages has not been send
    
    // Returns body contents of the response with detailed error message(s)
    $contents = $e->getResponse()->getBody()->getContents());
} catch (\Http\Client\Exception\TransferException $e) {
    // Something unexpected happened
}
 

$client = (new \CM\Messaging\Client('your-product-token'))
        ->setReference('Your reference')
        ->setAllowedChannels([AllowedChannel::SMS, AllowedChannel::PUSH, AllowedChannel::VOICE])
        ->setAppKey('your-app-key')
        ->setMinimumNumberOfMessageParts(1)
        ->setMaximumNumberOfMessageParts(8)
        ->setDcs(8);
 

$client->send($messages, ['strategy' => ['keep_duplicate_phone_numbers']]);