简体   繁体   English

为 Twilio 创建 PHP webhook 时出现问题

[英]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. 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.

Instead of using webhook.site, I would like to create my own PHP webhook client.我不想使用 webhook.site,而是想创建自己的 PHP webhook 客户端。

The code below is the CURL command that works 100% when using webhook.site:下面的代码是 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;

I then tried to use PHP to create a basic webhook just to pull the data:然后我尝试使用 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);
?>

But I keep coming with a blank request.log file - what am I doing wrong???但是我一直带着一个空白的 request.log 文件——我做错了什么??? Thanks in advance提前致谢

Twilio developer evangelist here. Twilio 开发人员布道师在这里。

By default, curl will make a POST request with the Content-Type header application/x-www-form-urlencoded .默认情况下,curl 将使用Content-Type header application/x-www-form-urlencoded发出 POST 请求。 This is actually the same content type that Twilio uses when it sends a webhook request.这实际上与 Twilio 在发送 webhook 请求时使用的内容类型相同。

Your PHP to receive the request is trying to json_decode the data, which won't work with form encoded data.您接收请求的 PHP 正在尝试对数据进行json_decode ,这不适用于表单编码数据。 Instead, you can access$_POST to get an associative array of the parameters sent to your script.相反,您可以访问$_POST以获取发送到脚本的参数的关联数组。 You can then write them to a log file however you like.然后,您可以随意将它们写入日志文件。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM