需要帮助在 CodeIgniter 中接收 WhatsApp API Webhook 响应数据

问题描述 投票:0回答:1

我想接收 WhatsApp API Webhook 返回的数据,但是,使用 CodeIntier 它不起作用。我尝试了各种方法。

我已经使用了所有方法来捕获后值,但它不起作用。

<?php
defined('BASEPATH') || exit('No direct script access allowed');
set_time_limit(0);
class Whatsapp_webhook extends ClientsController
    {
        public function __construct()
        {
            parent::__construct();
            $this->load->library('session');
        }

public function webhook()
        {
            header('Access-Control-Allow-Origin: *');
            header('Access-Control-Allow-Methods: POST, GET, OPTIONS');
            header('Access-Control-Allow-Headers: Content-Type');
            
            file_put_contents('test.json', file_get_contents('php://input'));
            
            $WebhookToken = 'SecretToken';   //the token given at the time of creating webhook at meta
            if (isset($_REQUEST['hub_challenge']) && $_GET['hub_verify_token'] === $WebhookToken) {
                echo $_GET['hub_challenge'];
                exit();
            } else {
                $this->session->set_userdata('wa_response', file_get_contents('php://input'));
                $data = json_decode($this->session->userdata('wa_response'));
            
                $dataInsert['recorded_at'] = date('Y-m-d H:i:s');
                $dataInsert['incoming_response'] = json_encode($data);
                $this->db->insert(db_prefix() . 'whatsapp_messages', $dataInsert);


                $file = '/home/******/watrigger.txt';
                $txtdata = json_encode($data)."\n";  
                file_put_contents($file, $txtdata, FILE_APPEND | LOCK_EX);

                $this->session->unset_userdata('wa_response');
            }
        }

}

下面的代码作为独立的 php 工作得非常好,但是,我正在尝试将其转换为 CodeIgniter,

<?php 
$WebhookToken = 'SecretToken';   //the token given at the time of creating webhook at meta

if($_SERVER['REQUEST_METHOD'] === 'GET' && isset($_GET['hub_challenge']) && isset($_GET['hub_verify_token']) && $_GET['hub_verify_token'] === $WebhookToken){
    echo $_GET['hub_challenge'];
    exit();
}


$data = file_get_contents("php://input");
$event = json_decode($data, true);
if(isset($event))
{
    //Here, you now have event and can process them how you like e.g Add to the database or generate a response
    $file = 'log.txt';  
    $data =json_encode($event)."\n";  
    file_put_contents($file, $data, FILE_APPEND | LOCK_EX);
    
}
?>

如果了解 WhatsAPP API Webhook 知识的人可以帮助我,那就太好了。我正在使用开源 CRM 并尝试集成 Whatsapp API。

api codeigniter whatsapp endpoint
1个回答
0
投票

不知怎的,问题就解决了。我开始了全新的、最低限度的工作,并且成功了。谢谢

© www.soinside.com 2019 - 2024. All rights reserved.