未调用 Twilio 回调 URL - PHP &WordPress [英] Twilio Callback URL not Called - PHP & Wordpress

查看:31
本文介绍了未调用 Twilio 回调 URL - PHP &WordPress的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将 Twilio 集成到我的 Wordpress 网站上.

我们的想法是允许用户输入他们的电话号码以获取我们应用的下载链接.我们在这里提供了一个简单的表格 - http://evntr.co/download - 并在提交表格时,运行 EvntrPHP.php 代码.

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.

使用模板代码,我们可以轻松地获取表单,使用免费的 Twilio 号码向经过验证的号码(当前在收件人"字段中的号码)发送消息.但是,当我们添加 StatusCallback 参数时,它永远不会调用我们的 callback.php 代码.EvntrPHP.phpcallback.php 都在根目录 - evntr.co/中.

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", 
 ));
?>

我的理解是流程应该是这样的:

  1. 用户导航到 evntr.co/download
  2. 用户提交带有他们的号码的表单
  3. form 调用 EvntrPHP.php 并向他们的号码发送一条短信
  4. 每当消息的状态发生变化(已发送、已交付、已取消等)时,Twilio 都会 POST 到 callback.php.

但是,每次我提交表单时,都会发送消息并且页面只停留在 evntr.co/EvntrPHP.php 并且从不加载 callback.php.也许这是我对回调 URL 如何工作的误解?或者 StatusCallback 参数可能不适用于免费的 Twilio 号码?

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?

推荐答案

Twilio 开发人员布道者在这里.

Twilio developer evangelist here.

Twilio 回调没有按预期工作是正确的.正如 McCann 指出的那样,请求是从 Twilio 异步发送到您提供的 URL 的.您可以使用回调来跟踪消息的进度,但不影响用户发出的请求.

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>

当然比普通的

更有风格!或者,您可以重定向到显示成功消息的页面.(PHP 不是我最擅长的主题,但 关于重定向的讨论在这个 StackOverflow 问题上很普遍)

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)

祝您使用该应用程序好运,如果您有更多 Twilio 问题,请告诉我!

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

如评论中所述,如果您想查看 API 请求是否成功,您需要执行以下操作:

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!
  }

?>

因此,将 API 调用包装在 try/catch 块中并做出适当的响应,应该可以从错误的电话号码或其他 API 错误中捕获大部分错误.它不能保证 SMS 已发送(您将从回调 webhook 中获取),但它会保证您已尽一切努力使 SMS 发送.

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.

这篇关于未调用 Twilio 回调 URL - PHP &amp;WordPress的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆