Skip to content

Commit

Permalink
Initial commit of version 2
Browse files Browse the repository at this point in the history
  • Loading branch information
Kyle Conroy committed Sep 6, 2009
0 parents commit 96c3079
Show file tree
Hide file tree
Showing 6 changed files with 764 additions and 0 deletions.
22 changes: 22 additions & 0 deletions LICENSE
@@ -0,0 +1,22 @@
Copyright (c) 2009 Twilio, Inc.

Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
23 changes: 23 additions & 0 deletions README
@@ -0,0 +1,23 @@
## PHP Twilio Helper Library

### DESCRIPTION
The Twilio REST SDK simplifies the process of makes calls to the Twilio REST.
The Twilio REST API lets to you initiate outgoing calls, list previous call,
and much more. See http://www.twilio.com/docs for more information.

### USAGE
To use the Twilio library, just include
twilio.php in the your current php file. As shown in example-rest.php, you will need to specify the ACCOUNT_ID and ACCOUNT_TOKEN given to you by Twilio before you can make REST requests. In
addition, you will need to choose a 'Caller' and 'Called' before making
outgoing calls. See http://www.twilio.com/docs for more information.

### GEM

### FILES
twilio.php -- include this library in your code
example-rest.php -- example usage of REST
example-twiml.php -- example usage of the TwiML generator
example-utils.php -- example usage of utilities

### LICENSE
The Twilio PHP Helper Library is distributed under the MIT License
93 changes: 93 additions & 0 deletions example-rest.php
@@ -0,0 +1,93 @@
<?php
// Include the PHP TwilioRest library
require "twilio.php";

// Twilio REST API version
$ApiVersion = "2008-08-01";

// Set our AccountSid and AuthToken
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";

// Outgoing Caller ID you have previously validated with Twilio
$CallerID = 'NNNNNNNNNN';

// Instantiate a new Twilio Rest Client
$client = new TwilioRestClient($AccountSid, $AuthToken);

// ========================================================================
// 1. Initiate a new outbound call to 415-555-1212
// uses a HTTP POST
$data = array(
"Caller" => $CallerID, // Outgoing Caller ID
"Called" => "415-555-1212", // The phone number you wish to dial
"Url" => "http://demo.twilio.com/welcome"
);

$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Calls",
"POST", $data);

// check response for success or error
if($response->IsError)
echo "Error starting phone call: {$response->ErrorMessage}\n";
else
echo "Started call: {$response->ResponseXml->Call->Sid}\n";

// ========================================================================
// 2. Get a list of recent calls
// uses a HTTP GET
$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Calls",
"GET");

if($response->IsError)
echo "Error fetching recent calls: {$response->ErrorMessage}";
else {
foreach($response->ResponseXml->Calls->Call AS $call)
echo "Call from {$call->Caller} to {$call->Called}";
echo " at {$call->StartTime} of length: {$call->Duration}\n";
}

// ========================================================================
// 3. Get Recent Developer Notifications
// uses a HTTP GET
$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Notifications");

if($response->IsError)
echo "Error fetching recent notifications: {$response->ErrorMessage}";
else {
foreach($response->ResponseXml->Notifications->Notification AS $notification)
echo "Log entry (level {$notification->Log}) on ";
echo "{$notification->MessageDate}: {$notification->MessageText}\n";
}

// ========================================================================
// 4. Get Recordings for a certain Call
// uses a HTTP GET

$callSid = "CA123456789123456789";
$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Recordings",
"GET", array("CallSid" => $callSid));

if($response->IsError){
echo "Error fetching recordings for call $callSid:";
echo " {$response->ErrorMessage}";
} else {

// iterate over recordings found
foreach($response->ResponseXml->Recordings->Recording AS $recording)
echo "Recording of duration {$recording->Duration} seconds made ";
echo "on:{$recording->DateCreated} at URL: ";
echo "/Accounts/$AccountSid/Recordings/{$recording->Sid}\n";
}

// ========================================================================
// 5. Delete a Recording
// uses a HTTP DELETE
$recordingSid = "RE12345678901234567890";
$response = $client->request("/$ApiVersion/Accounts/$AccountSid/Recordings/$recordingSid", "DELETE");
if($response->IsError)
echo "Error deleting recording $recordingSid: {$response->ErrorMessage}\n";
else
echo "Successfully deleted recording $recordingSid\n";

?>
103 changes: 103 additions & 0 deletions example-twiml.php
@@ -0,0 +1,103 @@
<?php

/*
The TwiML PHP Response Library makes it easy to write TwiML without having
to touch XML. Error checking is built in to help preventing invalid markup.
USAGE:
To create TwiML, you will make new TwiML verbs and nest them inside another
TwiML verb. Convenience methods are provided to simplify TwiML creation.
SUPPORTED VERBS:
Response
Say
Play
Dial
Gather
Hangup
Redirect
Record
Pause
Number
*/

include ('twilio.php');

// ========================================================================
// Using Say, Dial, and Play
$r = new Response();
$r->append(new Say("Hello World", array("voice" => "man",
"language" => "fr", "loop" => "10")));
$r->append(new Dial("4155551212", array("timeLimit" => "45")));
$r->append(new Play("http://www.mp3.com"));
//$r-> Respond();

/* outputs:
<Response>
<Say voice="man" language="fr" loop="10">Hello World</Say>
<Play>http://www.mp3.com</Play>
<Dial timeLimit="45">4155551212</Dial>
</Response>
*/

// The same XML can be created above using the convencience methods
$r = new Response();
$r->addSay("Hello World", array("voice" => "man", "language" => "fr",
"loop" => "10"));
$r->addDial("4155551212", array("timeLimit" => "45"));
$r->addPlay("http://www.mp3.com");
//$r-> Respond();

// ========================================================================
// Gather, Redirect
$r = new Response();
$g = $r->append(new Gather(array("numDigits" => "1")));
$g->append(new Say("Press 1"));
$r->append(new Redirect());
//$r-> Respond();


/* outputs:
<Response>
<Gather numDigits="1">
<Say>Press 1</Say>
</Gather>
<Redirect/>
</Response>
*/

// ========================================================================
// Add a Say verb multiple times
$r = new Response();
$say = new Say("Press 1");
$r->append($say);
$r->append($say);
//$r-> Respond();


/*
<Response>
<Say>Press 1</Say>
<Say>Press 1</Say>
</Response>
*/


// ========================================================================
// Set any attribute / value pair
// You may want to add an attribute to a verb that the library does not
// support. This can be accomplished by putting __ in front o the
// attribute name
$r = new Response();
$redirect = new Redirect();
$redirect->set("crazy","delicious");
$r->append($redirect);
$r-> Respond();

/*
<Response>
<Redirect crazy="delicious"/>
</Response>
*/

?>
47 changes: 47 additions & 0 deletions example-utils.php
@@ -0,0 +1,47 @@
<?php
// Include the PHP TwilioRest library
require "twilio.php";

// Twilio REST API version
$ApiVersion = "2008-08-01";

// Set our AccountSid and AuthToken
$AccountSid = "ACXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
$AuthToken = "YYYYYYYYYYYYYYYYYYYYYYYYYYYYYYYY";

// Create a new TwilioUtils object
$utils = new TwilioUtils($AccountSid, $AuthToken);

// Note, that if your URL uses an implied "index" document
// (index.php), then apache often adds a slash to the SCRIPT_URI
// while Twilio's original request will not have a slash
// Example: if Twilio requested http://mycompany.com/twilio
// and that url is handled by an index.php script
// Apache/PHP will report the URI as being:
// http://mycompany.com/twilio/
// But the hash should be calculated without the trailing slash

// Also note, if you're using URL rewriting, then you should check
// to see that PHP is reporting your SCRIPT_URI and
// QUERY_STRING correctly.

if($_SERVER['HTTPS'])
$http = "http://";
else
$http = "https://";

$url = $http.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'];

if(isset($_POST)) {
// copy the post data
$data = $_POST;
}

$expected_signature = $_SERVER["HTTP_X_TWILIO_SIGNATURE"];

echo "The request from Twilio";
if($utils->validateRequest($expected_signature, $url, $data))
echo "was confirmed to have come from Twilio.";
else
echo "was NOT VALID. It might have been spoofed!";
?>

0 comments on commit 96c3079

Please sign in to comment.