I have a PHP script that is called via a Twilio webhook for incoming SMS. Messages come in with post data that I capture with:
$Message = $_POST["Body"];
Then, using the Twilio PHP SDK, that message is forwarded back out to a mobile phone via:
$relay = new Client($sid, $token);
try {
$relay->messages->create(
'+15558675310',
array(
'from' => '+15555555555',
'body' => $Message
)
);
}
catch (Exception $e) {
$TwilioError = "Error: " . $e->getMessage();
}
Simple enough and works fine. The problem is emojis aren't translated properly and are sent out garbled. What should be a smiley face goes out as: 😀
The Twilio logs will show the incoming message with the smiley face, but that's just an HTML representation. The log doesn't show what the encoding is (assuming UTF-8). The Twilio log for the outgoing message says the encoding is UCS2.
The web server default encoding is UTF-8.
What needs to be done, using PHP 5.6, with the message coming in from Twilio with possible emojis in the content before sending back out via the Twilio PHP SDK?