I am attempting to integrate Twilio with my web app following an insightful call with Sabrina yesterday afternoon.
I have hit upon an error when attempting to create a dynamic SMS message and send that out to a phone number
The error I’m getting is syntax error, unexpected 'use' (T_USE)
.
The PHP to compile and send the message;
// Send SMS via Twilio ////////////////////////////////////////////////////////////////////////////////////////////////////////
if ($do === 'send-sms') {
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
include 'twilio.config.php';
$sms_to = $_POST['sms_recipient'];
$sms_body = $_POST['sms_messagebody'];
$client = new Client($sid, $token);
$client->messages->create(
$sms_to,
array(
'from' => $twilio_number,
'body' => $sms_messagebody
)
);
header('Location: ' . $_SERVER['HTTP_REFERER']);
}
The form that is posting the data to the PHP script (please note I have hardcoded my mobile number as the recipient whilst I am testing - however in production this will change to a value pulled from the database);
<form class="signup-form" action="includes/process.php?do=send-sms" id="sms_submit" method="POST">
<fieldset>
<input type="hidden" name="sms_recipient" id="sms_recipient" value="0123456789">
<textarea name="sms_messagebody" id="sms_messagebody" placeholder="Your message..." style="height: 60px; background-image: none; background-position: 0px 50%; background-repeat: repeat;"></textarea>
</fieldset>
<input type="submit" class="btn-colored submit-send-sms" value="Send SMS Message" name="email_submit" />
</form>
The Twilio files are stored at http://localhost/domain.com/includes/twilio-php-master/Twilio/.
The process script is stored at http://localhost/domain.com/includes/ and includes other site functions and scripts that are only ran based on the value of the ‘do’ parameter of the URL passed to ‘includes/process.php’.
The form is displayed within a modal, in a PHP page located at http://localhost/domain.com/ - the public root of the web app.
Note that I am using an included PHP file (twilio.config.php) to store my Twilio credentials and number from which SMS messages are sent from.
Any help would be greatly appreciated