Twilio WhatsApp API回调失败(错误11200):代码排查求助
排查Twilio WhatsApp回调11200错误的方案
核心排查方向
回调URL必须公网可达
Twilio无法访问本地或内网地址,必须用内网穿透工具把你的PHP服务暴露到公网。直接在浏览器/Postman访问回调URL,确认能正常返回XML格式的Twiml内容,没有404、500等HTTP错误。确保请求参数正确接收
Twilio默认用POST方法发送回调,建议把$_REQUEST['Body']改成$_POST['Body'],避免服务器配置限制导致参数丢失。同时提前判断参数是否存在,避免未定义警告:$guess = isset($_POST['Body']) ? trim($_POST['Body']) : '';添加正确的响应头
Twiml是XML格式,必须指定响应头让Twilio识别,在输出响应前添加:header('Content-Type: application/xml');这是很多人忽略的点,没有正确的Content-Type会导致Twilio无法解析响应,触发11200错误。
验证SDK加载路径
确认手动下载的SDK路径正确,/twilio-php-main/src/Twilio/autoload.php确实存在于你的项目目录中,没有拼写错误或路径层级问题。检查服务器错误日志
查看PHP错误日志,排查是否有类找不到、变量未定义等隐性错误,这些错误会导致脚本执行失败,无法返回有效响应。
修改后的示例代码
<?php require __DIR__ . '/twilio-php-main/src/Twilio/autoload.php'; use Twilio\Twiml; // 安全获取消息内容 $guess = isset($_POST['Body']) ? trim($_POST['Body']) : ''; $pick = rand(1,5); $response = new Twiml; // 注意Twilio传的Body是字符串,所以判断时用字符串数组 if (!in_array($guess, ['1','2','3','4','5'])) { $response->message("Hiya! I'm thinking of a number between 1 and 5 - try to guess it!"); } elseif ($guess == (string)$pick) { $response->message("Yes! You guessed it!"); } else { $response->message("Nope, it was actually $pick - Pick a new number to play again!"); } // 设置XML响应头 header('Content-Type: application/xml'); print $response;
内容的提问来源于stack exchange,提问作者dhiego mota
相关产品推荐
相关产品推荐

