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\SmsRequest;
        use Infobip\Model\SmsDestination;
        use Infobip\Model\SmsMessage;
        use Infobip\Api\SmsApi;
        use Infobip\Model\SmsTextContent;

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

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

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

        try {
            $smsResponse = $sendSmsApi->sendSmsMessages($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\SmsDeliveryResult;
        use Infobip\ObjectSerializer;

        $objectSerializer = new ObjectSerializer();

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

        /**
        * @var SmsDeliveryResult $deliveryResult
        */
        $deliveryResult = $objectSerializer->deserialize($data, SmsDeliveryResult::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";
    }
json
"fobip/infobip-api-php-client": "6.2.1"
}