Syntax error, unexpected 'use' (T_USE) error with Twilio SMS

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 :slight_smile:

As luck would have it Twilio responded to my ticket just as I posted thi up here, I’ve copied their response below for info;

Looks like the issue is the placement of the use statement.

This needs to be at the top of your PHP file, not nested in a conditional:
require __DIR__ . '/twilio-php-master/Twilio/autoload.php';
use Twilio\Rest\Client;
3 Likes

As it happens, that’s not limited to Twilio:

http://php.net/manual/en/language.namespaces.importing.php

And thanks for posting the fix as well as the question - very often in forums people ask questions, then don’t come back and say they’ve sorted it, and how.

3 Likes

No worries, it fustrates me when I see questions that are not resolved when searching for help so I try to come back and close them out where I can :slight_smile:

5 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.