Twilio Callback URL not Called – PHP & WordPress

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.

Read More

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?

Related posts

2 comments

  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.

  2. 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

Comments are closed.