Skip to content

Instantly share code, notes, and snippets.

@bmoren
Last active August 29, 2015 14:06
Show Gist options
  • Save bmoren/c64f6308a0036a8e39a0 to your computer and use it in GitHub Desktop.
Save bmoren/c64f6308a0036a8e39a0 to your computer and use it in GitHub Desktop.
Twilio Rest SMS reply
<?php>
//~~~~~~~~~~~~~~~BASIC RESPONSE EXAMPLE ~~~~~~~~~~~~~//
$client = new Services_Twilio($AccountSid, $AuthToken);
$body = $inbody . "" . ":and the PHP added ME!" ;
$message = $client->account->sms_messages->create($from, $to, $body);
<?>
<?php>
//~~~~~~~~~~~~~~~EXAMPLE OF TIMING WITH A FOR LOOP~~~~~~~~~~~~~//
$quantity = 1 ; // 1 will send 2-SMS (remember the for loop starts at 0)
$delayAmount = 5 ; // in seconds
for ($i=0; $i<=$quantity; $i++)
{
// Send a new outgoing SMS
$client = new Services_Twilio($AccountSid, $AuthToken);
$body = $i . "/" . $quantity . " " . $inbody . " " . ": and the PHP added ME!" ;
$client->account->sms_messages->create($from, $to, $body);
// http://php.net/manual/en/function.sleep.php
sleep($delayAmount);
}
?>
<?php
/*
*Simple Twilio REST API reply so that you can time text messages and have more control than a simple Twiml response
*Simply point your Twilio number to this script in your ACCOUNT>NUMBERS>SMS Request URL
* @author Ben Moren <ben@benmoren.com>
* @copyright 2013
* @license Creative Commons Attribution 3.0 Unported License.
* @version 0.13
* @link http://benmoren.com/
*/
// Include the PHP Twilio library. You need to download the library from
// twilio.com/docs/libraries, and move it into the folder containing this file.
require('Services/Twilio.php');
// PAID CRADENTIALS ~~~~~ API
// Set our AccountSid and AuthToken from twilio.com/user/account
$AccountSid = "Your Accunt SID";
$AuthToken = "Your Account Token";
//Your Twilio Number or Outgoing Caller ID
$from = ''; //your PAID twilio #
$to = ""; // Mobile # to send to / This will be reset to the incoming #
/*
// TEST CRADENTIALS ~~~~~ API
$AccountSid = "Your Accunt SID";
$AuthToken = "Your Account Token";
$from = '+15005550006'; // Twilio Magic Number
$to = ""; // Mobile # to send to
*/
//Get requests from the incoming PHP POST
//$sid = $_REQUEST['SmsSid'];
//$acct_id = $_REQUEST['AccountSid'];
//$from = $_REQUEST['From'];
//$to = $_REQUEST['To'];
$inbody = $_REQUEST['Body'];
// This gets the incoming # and assigns it as the new place to send messages to (aka. setting up a reply)
$to = $_REQUEST['From'];
?>
<?php>
//~~~~~~~~~~~~~~~EXAMPLE OF SIMPLE TIMING ~~~~~~~~~~~~~//
$delayAmount = 30 ; // in seconds
$client = new Services_Twilio($AccountSid, $AuthToken);
$body = "1/3" . $inbody . "" . ":and the PHP added ME!" ;
$message = $client->account->sms_messages->create($from, $to, $body);
sleep($delayAmount);
$client = new Services_Twilio($AccountSid, $AuthToken);
$body = "2/3" . $inbody . "" . ":and the PHP added ME!" ;
$message = $client->account->sms_messages->create($from, $to, $body);
sleep($delayAmount);
$client = new Services_Twilio($AccountSid, $AuthToken);
$body = "3/3" . $inbody . "" . ":and the PHP added ME!" ;
$message = $client->account->sms_messages->create($from, $to, $body);
<?>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment