Wondering what actually happens when you connect using your app, and why you keep hearing that same welcome message? The key to this mystery is in these lines in your PHP file:
$appSid = "APabe7650f654fc34655fc81ae71caa3ff"; // ... $capability->allowClientOutgoing($appSid, array(), $clientName);
This creates a capability token that tells your device which Twilio Application it will connect to. Applications are just references to TwiML URLs that are fetched when a connection is established from a device, a browser, or a phone. If you are not familar with TwiML, we suggest you head over to the Twilio Markup Language Quickstart.
So the string starting with 'AP' is an Application SID. The default one above
is a demo app created by Twilio that serves up a very simple TwiML document
that uses <Say> to read a greeting.
In this section, we'll:
Let's build a simple application in PHP to serve TwiML from your web host.
Create a new file called hello-client-monkey-twiml.php on your server and
copy-paste the following code into that file:
<?php
header('Content-type: text/xml');
?>
<Response>
<Say>Welcome to Twilio Client!</Say>
</Response>
Assuming your web server is at companyfoo.com, try loading up your new PHP script (http://companyfoo.com/hello-client-monkey-twiml.php) in your browser. All set?
Now we're ready to create the new application in your Account Dashboard, so let's navigate to the tab called 'Apps'. This is where you can see all your current Twilio Applications and create new ones. Select 'Add Application' and complete the form with the following information:
Click SAVE to create your app. Now, copy the newly created Application SID and paste it into the token generation code at the top of the client script we created before:
<?php // Found in the 'helper-libs' folder, or download twilio-php from http://twilio.com/docs/libraries require "Services/Twilio/Capability.php"; $accountSid = "ACXXXXXXXXXXXXXXXX"; $authToken = "secret"; // The app outgoing connections will use: $appSid = "APXXXXXXXXXXXXXXXXXXXXXXXXX"; // YOUR APPLICATION SID! // The client name for incoming connections: $clientName = "monkey"; $capability = new Services_Twilio_Capability($accountSid, $authToken); // This allows incoming connections as $clientName: $capability->allowClientIncoming($clientName); // This allows outgoing connections to $appSid with the "From" // parameter being the value of $clientName $capability->allowClientOutgoing($appSid, array(), $clientName); // This returns a token to use with Twilio based on // the account and capabilities defined above $token = $capability->generateToken(); echo $token; ?>
If you restart your Android app (this will cause a new Capability Token to be fetched which will point to your new Twilio app) and press the Dial button again, you should now be hearing your very own welcome message.
Sweet. Now, what if you want to pass variables from your Android app to the script running on the server?