1

I'm trying to integrate Twilio onto my Wordpress site.

The idea is to allow users to type in their phone number to get a download link to our app. We've put up a simple form here - http://evntr.co/download - and upon submitting the form, the EvntrPHP.php code is run.

Using template code, we've easily been able to get the form to send a message to a verified number (the one currently in the To field) using a free Twilio number. However, when we add the StatusCallback parameter, it never calls our callback.php code. Both EvntrPHP.php and callback.php are in the root directory - evntr.co/.

<?php
 require 'twiliophp/Services/Twilio.php';

 $AccountSid = "--------";
 $AuthToken = "---------";

 $client = new Services_Twilio($AccountSid, $AuthToken);

 $phonenum = $_POST["phonenum"];
 $callbackURL = "https://evntr.co/callback.php";

 $client->account->messages->create(array( 
    'To' => "XXXXXXXXXX", 
    'From' => "+XXXXXXXXXX", 
    'Body' => "Test Message",  
    'StatusCallback' => "https://evntr.co/callback.php", 
 ));
?>

My understanding is that the flow should be like this:

  1. user navigates to evntr.co/download
  2. user submits the form with their number
  3. form calls EvntrPHP.php and sends a text message to their number
  4. Twilio POSTs to callback.php whenever the status of the message changes (Sent, Delivered, Canceled, etc).

However, every time I submit the form, the message is sent and the page just stays at evntr.co/EvntrPHP.php and never loads callback.php. Maybe this is a misunderstanding on my part with how Callback URLs work? Or maybe the StatusCallback parameter doesn't work with a free Twilio number?

U2Pride15
  • 33
  • 4

2 Answers2

1

Twilio developer evangelist here.

You are correct that the Twilio callbacks are not working as you expect. As McCann points out, the request is made asynchronously from Twilio to the URL you supply. You can use the callback to keep track of the progress of the message, but not affect the request the user has made.

So, in your example you either want to render something after you have sent the message:

<?php
  require 'twiliophp/Services/Twilio.php';

  // other stuff

  $client->account->messages->create(array( 
    'To' => $phonenum, 
    'From' => "+14708655xxx", 
    'Body' => "Test Message",  
    'StatusCallback' => "https://evntr.co/callback.php", 
  ));
?>

<h1>You should receive an SMS, click the link in the SMS to download the app on the platform of choice.</h1>

With some more style than a plain <h1> of course! Or, you could redirect to a page with a success message on. (PHP is not my strongest subject, but discussions of redirects are rife on this StackOverflow question)

Good luck with the app, and let me know if you have any more Twilio questions!

[edit]

As discussed in the comments, if you want to see if the API request was successful you'll want to do something like:

<?php
  require 'twiliophp/Services/Twilio.php';

  // other stuff

  try {
    $client->account->messages->create(array( 
      'To' => $phonenum, 
      'From' => "+14708655xxx", 
      'Body' => "Test Message",  
      'StatusCallback' => "https://evntr.co/callback.php", 
    ));

    // Success! Redirect to success page!
    header("Location: http://evntr.co/success.php");
    die();

  } catch (Services_Twilio_RestException $e) {
    // Something went wrong!
    // Do something about it!
  }

?>

So, wrapping the API call in a try/catch block and responding appropriately should catch most errors from incorrect phone numbers or other API errors. It won't guarantee that the SMS has been delivered (you'll get that from the callback webhook) but it will guarantee that you've done all you can to get the SMS sent.

Community
  • 1
  • 1
philnash
  • 70,667
  • 10
  • 60
  • 88
  • Ah, so after sending the message, I redirect to my page - evntr.co/callback.php - and then react based on the status message. Do you have any sample code on how to catch the status messages sent to that callback url? I tried a simple echo of the variable - $_POST['SmsStatus'] - but that never showed anything. – U2Pride15 Jul 22 '15 at 20:14
  • Ah, no, your redirect is entirely separate to any of the status callbacks from Twilio. They happen asynchronously, pour of the band of your REST API call. This could be almost immediately or take a few minutes, so I would not base any user interaction on them. I'm not sure what user interaction you're trying to accomplish with the callbacks, so I'm not sure how to guide you from this point. – philnash Jul 22 '15 at 20:26
  • Gotcha. I'm trying to direct users to either a success page or a failure page (for example, twilio was unable to send the text because of an invalid number). I might be approaching this wrong. – U2Pride15 Jul 22 '15 at 21:06
  • I've added some more to my answer that might help. What do you think? – philnash Jul 23 '15 at 09:53
  • This works. What is a use case for the callback url then? I seem to have been approaching this wrong. – U2Pride15 Jul 23 '15 at 14:36
  • Callback URLs are so that you can track the SMS messages that you send and discover errors that occur after the original API request. Those errors that could occur are listed here: https://www.twilio.com/docs/api/rest/message#error-values. It's best to prepare for these callbacks so that you can look out for these errors and deal with them if/when they occur. – philnash Jul 23 '15 at 15:22
0

The callback doesn't work like you think it does.

Call End Callback (StatusCallback) Requests

After receiving a call, requesting TwiML from your app, processing it, and finally ending the call, Twilio will make an asynchronous HTTP request to the StatusCallback URL configured for the called Twilio number (if there is one). By providing a StatusCallback URL for your Twilio number and capturing this request you can determine when a call ends and receive information about the call.

-- https://www.twilio.com/docs/api/twiml/twilio_request

McCann
  • 316
  • 4
  • 8