duannuo4620 2015-07-21 21:34
浏览 98
已采纳

不调用Twilio回调URL - 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.

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?

  • 写回答

2条回答 默认 最新

  • dongshendie8849 2015-07-22 15:52
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?