0

I'm using the Twilio PHP API on my site for sending and receive sms.i successfully send sms but i can't receive individual words from incoming sms.suppose a customer sent a sms in my twilio number like="i am going home" now i want to take two words from that line like i,home. can i use this code

<?php
    $msg1 = $_REQUEST['Body[0]'];
    $msg2 = $_REQUEST['Body[2]'];
header('Content-Type: text/xml');
?>

am i right? or what is the thinks i should change

Foisal Hossain
  • 129
  • 1
  • 8

2 Answers2

1

Twilio developer evangelist here.

The issue here is that you have the square brackets inside the string. Try this instead:

<?php
    $body = $_REQUEST['Body'];
    $msg1 = $body[0];
    $msg2 = $body[2];
header('Content-Type: text/xml');
?>
philnash
  • 70,667
  • 10
  • 60
  • 88
  • but when reply in number it wasnot take as a one word.suppose a client sent a sms with this format 456 y g and i want to take 456 to in a one string but it was save in three string. How to solve it – Foisal Hossain Jul 11 '19 at 06:55
  • Ah, my mistake. Using square brackets like that will pick individual letters, not words. Check Hasta Dhana's answer, that should be correct. – philnash Jul 11 '19 at 11:25
  • how can i get message date from twilio incoming sms? – Foisal Hossain Jul 14 '19 at 08:47
  • You can see all the parameters you receive for an incoming message here: https://www.twilio.com/docs/sms/twiml#request-parameters. The date isn’t sent as part of the webhook. You can fetch it by retrieving the message from the REST API, see the docs for that here: https://www.twilio.com/docs/sms/api/message-resource#fetch-a-message-resource – philnash Jul 14 '19 at 09:26
1

If you want to get the first and the last word from the received sms, since the body is just a string you could split the sms body into array, then get the first array and last array :

<?php
$array = explode(" ",$_POST['Body']);
$first_word = $array[0];
$last_word  = $array[count($array)-1];

header('Content-Type: text/xml');
?>
Hasta Dhana
  • 4,699
  • 7
  • 17
  • 26