How to make phone calls and send SMS with PHP

PHP against a dark background indicating how to make calls and send text via PHP using bandwidth APIs

Bandwidth has a full suite of messaging API and voice API solutions to power your business needs. Whether you want to send MMS or SMS messages from toll-free numbers, local numbers, or short codes, Bandwidth’s APIs make it easy to enable in your software or platform, while Bandwidth’s relationships with the carriers give you reliable message delivery and insights. 

Introduction

In this article, you will see a quick overview of what sending your first message and creating your first call looks like when using Bandwidth’s Voice and Messaging APIs along with Bandwidth’s PHP SDK. First, you will see some code examples and then at the end of this article is an explanation of the variables you would need to insert into the code to make the SDK work with your Bandwidth account.

Additional resources:

Packages and Client Initialization

The Bandwidth PHP SDK package is available through composer. So the first step is to require it.

composer require bandwidth/sdk

Then you need to initialize the configuration for the SDK client.

<?php

require "vendor/autoload.php";

$config = new BandwidthLib\Configuration(
    array(
        'messagingBasicAuthUserName' => '{username}',
        'messagingBasicAuthPassword' => '{password}',
        'voiceBasicAuthUserName' => '{username}',
        'voiceBasicAuthPassword' => '{password}',
    )
);
$client = new BandwidthLib\BandwidthClient($config);
$messagingClient = $client->getMessaging()->getClient();

How to send a text message with PHP

Sending a SMS Message with PHP from a Bandwidth number looks like this. This must be in the same file as your initiated client or the client must be imported here.

$messagingBody = new BandwidthLib\Messaging\Models\MessageRequest();
$messagingBody->applicationId = '{app_id}';
$messagingBody->to = array('{to_number}');
$messagingBody->from = '{from_number}';
$messagingBody->text = 'Hello, I am sending a message! How fun!';

$messagingClient->createMessage('{account_id}', $messagingBody);

You will need to set up a server that can receive a callback/webhooks with a JSON body that will tell you if the message was successfully delivered or not. Some simple PHP server code that can process Bandwidth messaging callbacks could look like this.

<?php

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    $data = json_decode(file_get_contents('php://input'), true);
    if ($data[0]['type'] == 'message-delivered')
        //successful delivery action
        http_response_code(200);
    if ($data[0]['type'] == 'message-failed')
        $failure_reason = $data[0]['description'];
        //failed delivery action
        http_response_code(200);
    http_response_code(200);

How to make phone calls with PHP

Making a phone call with PHP from a Bandwidth number looks like this. This must be in the same file as your initiated client or the client must be imported here.

$voiceBody = new BandwidthLib\Voice\Models\CreateCallRequest();
$voiceBody->from = '{from_number}';
$voiceBody->to = '{to_number}';
$voiceBody->answerUrl = '{url}';
$voiceBody->applicationId = '{app_id}';

$voiceClient = $client->getVoice()->getClient();
$voiceClient->createCall('{account_id}', $voiceBody)

When the call is answered by whoever you sent the call to, Bandwidth will send a callback/webhook with a JSON body to the URL you specified when you created the call. You can send BXML verbs back to Bandwidth in response to the callback/webhook in order to create new actions on the call. Some simple server code that would make the call play some text-to-speech audio and then hang up could look like this:

<?php

require "vendor/autoload.php";

if ($_SERVER['REQUEST_METHOD'] == 'POST')
    $data = json_decode(file_get_contents('php://input'), true);
    if ($data['eventType'] == 'answer')
        $speakSentence = new BandwidthLibVoiceBxmlSpeakSentence('I am saying something and now will hang up.');
        $speakSentence->voice('julie');

        $hangup = new BandwidthLibVoiceBxmlHangup();

        $response = new BandwidthLibVoiceBxmlResponse();
        $response->addVerb($speakSentence);
        $response->addVerb($hangup);

        echo $response->toBxml();
    http_response_code(200);

And that’s it! That’s how simple it is to create your first message and call with a Bandwidth phone number, Bandwidth’s API, and Bandwidth’s PHP SDK.

Variable Reference

Here is an explanation of the variables used in the above code that will be unique to your account and use case.

Variable Name Explanation
username your username for the Bandwidth Dashboard
password your password for the Bandwidth Dashboard
app_id the ID of your messaging or voice application; applications are set within the Bandwidth Dashboard and are used to associate with your Bandwidth phone numbers with URLs for callback/webhooks
from_number when creating outgoing messages or calls, the number you are sending to; this can be your cell phone number for testing or the number of one of your customers
to_number when creating outgoing messages or calls, this is a number you have been assigned by Bandwidth that lives on your Bandwidth Dashboard account
account_id your Bandwidth Dashboard account ID
url a URL you own that to which Bandwidth can send callbacks/webhooks related to call and messaging events; note that for messaging, this URL is not set at the time of message creation and is instead defined by the Bandwidth Dashboard application used to send the message

24/7 human support for Bandwidth’s APIs

Not just enterprise-grade APIs, get enterprise-scale support too, 24/7.