Skip to content

Instantly share code, notes, and snippets.

@daveh
Last active February 28, 2024 14:58
Show Gist options
  • Save daveh/6a31d0d28d9aef8c161c6ff1b6d29fae to your computer and use it in GitHub Desktop.
Save daveh/6a31d0d28d9aef8c161c6ff1b6d29fae to your computer and use it in GitHub Desktop.
Send SMS Messages with PHP (code to accompany https://youtu.be/obolAwbx388)
<!DOCTYPE html>
<html>
<head>
<title>PHP SMS</title>
<meta charset="UTF-8" />
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/water.css@2/out/water.css" />
</head>
<body>
<h1>PHP SMS</h1>
<form method="post" action="send.php">
<label for="number">Number</label>
<input type="text" name="number" id="number" />
<label for="message">Message</label>
<textarea name="message" id="message"></textarea>
<fieldset>
<legend>Provider</legend>
<label>
<input type="radio" name="provider" value="infobip" checked /> Infobip
</label>
<br />
<label>
<input type="radio" name="provider" value="twilio" /> Twilio
</label>
</fieldset>
<button>Send</button>
</form>
</body>
</html>
<?php
use Infobip\Configuration;
use Infobip\Api\SmsApi;
use Infobip\Model\SmsDestination;
use Infobip\Model\SmsTextualMessage;
use Infobip\Model\SmsAdvancedTextualRequest;
use Twilio\Rest\Client;
require __DIR__ . "/vendor/autoload.php";
$number = $_POST["number"];
$message = $_POST["message"];
if ($_POST["provider"] === "infobip") {
$base_url = "https://your-base-url.api.infobip.com";
$api_key = "your API key";
$configuration = new Configuration(host: $base_url, apiKey: $api_key);
$api = new SmsApi(config: $configuration);
$destination = new SmsDestination(to: $number);
$message = new SmsTextualMessage(
destinations: [$destination],
text: $message,
from: "daveh"
);
$request = new SmsAdvancedTextualRequest(messages: [$message]);
$response = $api->sendSmsMessage($request);
} else { // Twilio
$account_id = "your account SID";
$auth_token = "your auth token";
$client = new Client($account_id, $auth_token);
$twilio_number = "+ your outgoing Twilio phone number";
$client->messages->create(
$number,
[
"from" => $twilio_number,
"body" => $message
]
);
}
echo "Message sent.";
@Arifhacker
Copy link

Hii

@Piro1992
Copy link

How do i make my name display when receiver receives the sms

@daveh
Copy link
Author

daveh commented Nov 11, 2023

@Piro1992
Copy link

When i send rather than getting Mutual i got InfoSMS
 $message = new SmsTextualMessage(
destinations: [$destination],
text: $message,
from: "Mutual"

@daveh
Copy link
Author

daveh commented Nov 14, 2023

@Piro1992 It works for me - may I suggest contacting Infobip support here

@edwardklem
Copy link

Its saying message sent but not getting any message😕🤦🏻

@daveh
Copy link
Author

daveh commented Nov 20, 2023

@edwardklem Check the provider's dashboard for the sending logs

@munna-401
Copy link

its not working right now

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment