andrebruton
andrebruton

Reputation: 2386

How to add the callback URL to Twilio SMS sending code?

I'm using the official PHP Twilio library code for sending and receiving call. I can send an SMS using the example code below:

<?php
require('twilio/Twilio.php');

$sid = "AC************";
$token = "2************";

$client = new Services_Twilio($sid, $token);
$message = $client->account->sms_messages->create(
  '+1905xxxxxxx', // From a valid Twilio number
  '+278xxxxxxxx', // Text this number
  "Hello, test SMS message number 001!"
);
?>

Now I wan to add the callback URL to the above code. It can do it, but I don't know how to add the array for the $params.

function create($from, $to, $body, array $params = array()) {
    return parent::_create(array(
        'From' => $from,
        'To' => $to,
        'Body' => $body
    ) + $params);
}

Thanks for the help

Upvotes: 3

Views: 5629

Answers (1)

GBD
GBD

Reputation: 15981

Try as below

function create($from, $to, $body, array $params = array()) {
    return parent::_create(array(
        'From' => $from,
        'To' => $to,
        'Body' => $body
    ) + array('StatusCallback'=>'http://yourdomain.com/yoururl.php'));
}

OR

$message = $client->account->sms_messages->create(
  '+1905xxxxxxx', // From a valid Twilio number
  '+278xxxxxxxx', // Text this number
  "Hello, test SMS message number 001!",
   array('StatusCallback'=>'http://yourdomain.com/yoururl.php')
);

You can learn more about how to use StatusCallback when sending sms here.

Upvotes: 6

Related Questions