[英]Issues creating a PHP webhook for Twilio
So I have the following CURL command in PHP, if I send it https://webhook.site I can extract all the required data in JSON format.
我不想使用 webhook.site,而是想创建自己的 PHP webhook 客户端。
下面的代码是 CURL 命令,在使用 webhook.site 时可以 100% 工作:
<?php
$curl = curl_init();
curl_setopt_array($curl, array(
CURLOPT_URL => 'https://webhook.site/832090f1-f54f-4847-8c0d-5ec9208541a1',
CURLOPT_RETURNTRANSFER => true,
CURLOPT_ENCODING => '',
CURLOPT_MAXREDIRS => 10,
CURLOPT_TIMEOUT => 0,
CURLOPT_FOLLOWLOCATION => true,
CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1,
CURLOPT_CUSTOMREQUEST => 'POST',
CURLOPT_POSTFIELDS => array('SmsSid' => 'SMe8723661742d423fbf3fa9f7bbede050','SmsStatus' => 'sent','MessageStatus' => 'sent','ChannelToAddress' => '+1788123XXXX','To' => 'whatsapp:+15196978899','ChannelPrefix' => 'whatsapp','MessageSid' => 'SMe8723661742d423fbf3fa9f7bbede050','AccountSid' => 'AC306a09582e77715b0eb72df90de4c590','StructuredMessage' => 'false','From' => 'whatsapp:+154xxxxxx','MediaUrl0' => 'https://api.twilio.com/2010-04-01/Accounts/werwersdsdg72df90de4c590/Messages/wweugryuwyr7762b11ea/Media/wjeruwiy6243742
'),
CURLOPT_HTTPHEADER => array(
'user-agent: TwilioProxy/1.1',
'host: Postman'
),
));
$response = curl_exec($curl);
curl_close($curl);
echo $response;
然后我尝试使用 PHP 创建一个基本的 webhook 来提取数据:
<?php
if($json = json_decode(file_get_contents("php://input"), true)){
$data = $json;
$fp = file_put_contents( 'request.log', $data );
}
print_r($data);
?>
但是我一直带着一个空白的 request.log 文件——我做错了什么??? 提前致谢
Twilio 开发人员布道师在这里。
默认情况下,curl 将使用Content-Type
header application/x-www-form-urlencoded
发出 POST 请求。 这实际上与 Twilio 在发送 webhook 请求时使用的内容类型相同。
您接收请求的 PHP 正在尝试对数据进行json_decode
,这不适用于表单编码数据。 相反,您可以访问$_POST
以获取发送到脚本的参数的关联数组。 然后,您可以随意将它们写入日志文件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.