簡體   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.

我不想使用 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM