This topic is locked

Sending record data via SMS (Twilio)

11/21/2014 4:21:53 PM
PHPRunner Tips and Tricks
admin

In this tutorial we will show how to and SMS sending capabilities to your project. We will add a button to one of List page and that button will send record info via SMS to the current logged user.
[size="4"]
Note that Twilio is not free. Their rates depend on number of messages you going to send and many other things.
[size="4"][font="Trebuchet MS"]2. Download Twilio PHP client code and unzip Services folder to your output directory.[/size]

This will create Services folder with one file (Twilio.php) and one folder (Twilio) in there.
[size="4"]
Assuming that cell_phone is a field in the login table we can use AfterSuccessfulLogin event. That's the phone number where we will be sending SMSs.
AfterSuccessfulLogin event:

$_SESSION["cell_phone"]=$userdata["cell_phone"];


[size="4"][font="Trebuchet MS"]4. Add button to the List page grid.[/size]


Proceed to Twilio control panel and note your phone number, Account SID and Auth Token. You are going to need them soon.


Button Server event code

require "Services/Twilio.php";
$AccountSid = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$AuthToken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";

$TwilioNumber="xxx-xxx-xxxx";
$http = new Services_Twilio_TinyHttp('https://api.twilio.com', array('curlopts' => array(

CURLOPT_SSL_VERIFYPEER => false

)));

$client = new Services_Twilio($AccountSid, $AuthToken, null, $http);
$record = $button->getCurrentRecord();
$sms = $client->account->messages->sendMessage(

$TwilioNumber,

$_SESSION["cell_phone"],

"Make: ".$record["Make"]."\n".

"Model: ".$record["Model"]."\n".

"YearOfMake: ".$record["YearOfMake"]."\n".

"Price: ".$record["Price"]

);
J
jwehner 9/6/2017

This code no longer works as far as I can tell.
I think Twilio may have changed something on the php sdk maybe? I can't find the twilio.php file. Or is this my own php code?
is the services folder in my web folder or php install folder?

J
jwehner 9/7/2017



This code no longer works as far as I can tell.
I think Twilio may have changed something on the php sdk maybe? I can't find the twilio.php file. Or is this my own php code?
is the services folder in my web folder or php install folder?


OK well after a good night's sleep I figured it out!
I'm sure there are more ways to solve this but this is what works for me.
I could get the new php software to work by uploading the SDK to my site and naming the folder 'vendor' then copying the 'Twilio' folder to the root also. (Not sure why but it works!)
I just use an include on my button to call my twilio code



$record = $button->getCurrentRecord();

$name = $record["Name"];

$phone = $record["Phone"];

include "twilio_sms.php";


and my twilio_sms.php code:

<?php
require_once "vendor/autoload.php";

use Twilio\Rest\Client;

$sid = "ACXXXXXXXXXXXXXXXXXXXXX"; // Your Account SID from www.twilio.com/console

$token = "XXXXXXXXXXXXXXXXXXXXXXXXX"; // Your Auth Token from www.twilio.com/console
$client = new Twilio\Rest\Client($sid, $token);

$message = $client->messages->create($phone, // Text this number

array(

'from' => '+15555555555', // From a valid Twilio number

'body' => 'Hello '.$name.' from Twilio!'

));
print $message->sid;