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条)

报告相同问题?

悬赏问题

  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?
  • ¥15 file converter 转换格式失败 报错 Error marking filters as finished,如何解决?
  • ¥15 ubuntu系统下挂载磁盘上执行./提示权限不够
  • ¥15 Arcgis相交分析无法绘制一个或多个图形
  • ¥15 关于#r语言#的问题:差异分析前数据准备,报错Error in data[, sampleName1] : subscript out of bounds请问怎么解决呀以下是全部代码:
  • ¥15 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)