Is there a way to programmatically create a Twimlbin?

坚强是说给别人听的谎言 提交于 2019-12-11 03:28:20

问题


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?


回答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
 */
?>


来源:https://stackoverflow.com/questions/24494782/is-there-a-way-to-programmatically-create-a-twimlbin

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!