PHP code example of infobip / infobip-api-php-client

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

    

infobip / infobip-api-php-client example snippets


    use Infobip\Configuration;

    $configuration = new Configuration(
        host: 'your-base-url',
        apiKey: 'your-api-key'
    );

    use Infobip\ApiException;
    use Infobip\Model\SmsAdvancedTextualRequest;
    use Infobip\Model\SmsDestination;
    use Infobip\Model\SmsTextualMessage;

    $sendSmsApi = new SmsApi(config: $configuration);

    $message = new SmsTextualMessage(
        destinations: [
            new SmsDestination(to: '41793026727')
        ],
        from: 'InfoSMS',
        text: 'This is a dummy SMS message sent using infobip-api-php-client'
    );

    $request = new SmsAdvancedTextualRequest(messages: [$message]);

    try {
        $smsResponse = $sendSmsApi->sendSmsMessage($request);
    } catch (ApiException $apiException) {
        // HANDLE THE EXCEPTION
    }

    $apiException->getCode();
    $apiException->getResponseHeaders();
    $apiException->getResponseBody();
    $apiException->getResponseObject();

    $bulkId = $smsResponse->getBulkId();
    $messages = $smsResponse->getMessages();
    $messageId = (!empty($messages)) ? current($messages)->getMessageId() : null;

    use Infobip\Model\SmsReportResponse;
    use Infobip\ObjectSerializer;

    $objectSerializer = new ObjectSerializer();

    $data = \file_get_contents('php://input');

    /**
     * @var SmsReportResponse $deliveryResult
     */
    $deliveryResult = $objectSerializer->deserialize($data, SmsReportResponse::class);

    foreach ($deliveryResult->getResults() ?? [] as $report) {
        echo $report->getMessageId() . " - " . $report->getStatus()->getName() . "\n";
    }

    $deliveryReports = $sendSmsApi
        ->getOutboundSmsMessageDeliveryReports(
            bulkId: 'some-bulk-id',
            messageId: 'some-message-id',
            limit: 10
        );

    foreach ($deliveryReports->getResults() ?? [] as $report) {
        echo $report->getMessageId() . " - " . $report->getStatus()->getName() . "\n";
    }

    use Infobip\Model\SmsPreviewRequest;

    $previewResponse = $sendSmsApi
        ->previewSmsMessage(
            new SmsPreviewRequest(
                text: 'Let\'s see how many characters will remain unused in this message.'
            )
        );

    foreach ($previewResponse->getPreviews() ?? [] as $preview) {
        echo sprintf(
            'Characters remaining: %s, text preview: %s',
            $preview->getCharactersRemaining(),
            $preview->getTextPreview()
        ) . PHP_EOL;
    }

    use Infobip\ObjectSerializer;
    use Infobip\Model\SmsInboundMessageResult;

    $objectSerializer = new ObjectSerializer();

    $data = \file_get_contents('php://input');

    /**
     * @var SmsInboundMessageResult $messages
     */
    $messages = $objectSerializer->deserialize($data, SmsInboundMessageResult::class);

    foreach ($messages->getResults() ?? [] as $message) {
        echo $message-> getFrom() . " - " . $message-> getCleanText() . "\n";
    }