jonmrich jonmrich - 3 months ago 23
PHP Question

Sending dynamic SMS message body using Twilio

I'm using Twilio to send an SMS message like so (relevant part of code shown):

require('Twilio.php');
$body ="some example message body text";
$account_sid = 'XXXXXXX';
$auth_token = 'YYYYY';
$client = new Services_Twilio($account_sid, $auth_token);
$client->account->messages->create(array(
'To' => $phone,
'From' => "+15132592073",
'Body' => $body,
));


The above works fine, however, if I change this to:

'Body' => "some example "+$test+" message body text";


The message body returns as "0".

I've also tried:

$myMessage ="some example "+$test+" message body text";


and then:

$client->account->messages->create(array(
'To' => $phone,
'From' => "+15132592073",
'Body' => $myMessage,
));


But this yields the same result ("0"). Note that:

$myMessage ="some example message body text";


this works fine also (i.e., same thing without the variable)

What am I missing here?

Answer

You should use:

$body = "some example ". $test . " message body text";

Notice the concatenation operator '.' and not '+'.