2

I'd like to make an application where the user can enter a phone number and a message and I can have Twilio send that phone number a message with that text synthesized. An example of the TwiML code I'm using is something simple like

<?xml version="1.0" encoding="UTF-8"?>
    <Response>
        <Say>Hello World</Say>
    <Response>

In order to make this work I've tried setting up a PHP function that takes in the message and echoes the above TwiML but the phone calls I get from the test report an application failure. For the record, here's the PHP code I tried.

echo "<?xml version='1.0' encoding='UTF-8'?>";
echo "<Response>";
echo "<Say>" . $_GET['message'] . "</Say>";
echo "</Response>";

However, I was easily able to get a TwiMLBin to work with this (that being its purpose after all). After looking around however I was unable to find anything regarding an API for the site. Does anyone know if there's a way to programmatically create a TwiMLBin so that I can create the appropriate TwiMLBin for the message that my user enters and then direct my Twilio function calls to the appropriate URL?

Megan Speir
  • 3,745
  • 1
  • 15
  • 25
avorum
  • 2,243
  • 10
  • 39
  • 49
  • don't ever dump raw text into an xml context. it is VERY easy to introduce an xml syntax error and kill whatever's expecting valid xml. at bare minimum you should be doing `htmlspecialchars($_GET['message'])`. – Marc B Jun 30 '14 at 16:46
  • Hey! Twilio evangelist here, as Marc B mentioned, have you checked the output? It is likely that an odd character is messing up the XML formatting. Also - I can't tell if you're doing SMS or voice, but the tag will not work with SMS messages. – phalt Jul 02 '14 at 13:22

1 Answers1

1

Something like this should work in PHP. cURL is available in many programming languages so you can reverse it the same.

<?php

/* 
 * Create TWimL Bin
 */
$url = "http://twimlbin.com/create";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
$html = curl_exec($ch);
$status_code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

if($status_code=302 or $status_code=301){
  $TwimLBinURL = curl_getinfo($ch, CURLINFO_EFFECTIVE_URL);
}
curl_close($ch);
/*
 * Update TwimL with XML
 */
$ExplodeTwimLBinURL = explode("/", $TwimLBinURL);
$TwimLBinID = $ExplodeTwimLBinURL[3];

$TwimLBinUpdateUrl = "http://twimlbin.com/".$TwimLBinID."/update";
$data = "twiml=<PUT TWIML XML HERE>";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL,$TwimLBinUpdateUrl);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch,CURLOPT_POST, 1);
curl_setopt($ch,CURLOPT_POSTFIELDS, $data);
$result = curl_exec($ch);
curl_close($ch);
echo $TwimLBinURL;
/*
 * End of TwimL Bin Creation
 */
?>