SMS Integration
SMS Integration
Endterm Completion
SMS Integration
Note: Download the SMS Library.
- Download the SMS library in the provide download link at the top.
- Copy the sms and extract library zip file to your project
<?php
/**
* Send an SMS message by using Infobip API PHP Client.
*
* For your convenience, environment variables are already pre-populated with your account data
* like authentication, base URL and phone number.
*
* Please find detailed information in the readme file.
*/
// This line includes the autoload file, which loads all necessary classes and functions automatically.
require 'vendor/autoload.php';
// These lines import classes from the Infobip library, allowing the script to interact with Infobip's SMS API.
use Infobip\Api\SmsApi;
use Infobip\Configuration;
use Infobip\Model\SmsAdvancedTextualRequest;
use Infobip\Model\SmsDestination;
use Infobip\Model\SmsTextualMessage;
// These lines define the base URL and API key required to authenticate and interact with the Infobip API.
$BASE_URL = "yourAPIUrlBaseHere";
$API_KEY = " yourAPIKeyHere ";
// These lines define variables for the sender name, recipient phone number, and message text.
$SENDER = "InfoSMS";
$RECIPIENT = "recipient";
$MESSAGE_TEXT = "Text body!";
// This line creates a new instance of the Configuration class, passing the base URL and API key as
parameters.
$configuration = new Configuration(
host: $BASE_URL,
apiKey: $API_KEY
);
// This line creates a new instance of the SmsApi class, passing the configuration object as a parameter.
$sendSmsApi = new SmsApi(
config: $configuration
);
// This line creates a new instance of the SmsDestination class, specifying the recipient phone number.
$destination = new SmsDestination(
to: $RECIPIENT
);
// This line creates a new instance of the SmsTextualMessage class, specifying the destination(s), sender,
and message text.
$message = new SmsTextualMessage(
destinations: [ $destination],
from: $SENDER,
text: $MESSAGE_TEXT
);
// This line sends the SMS message(s) using the sendSmsMessage method of the SmsApi class and stores the
response in the $smsResponse variable.
$smsResponse = $sendSmsApi->sendSmsMessage($request);
// This line echoes JavaScript code to display an alert box with the message "Notification sent!". This
provides feedback to the user that the SMS notification has been sent successfully.
echo"<script>alert('Notification sent!')</script>";
?>