Questions about webhooks: how to manipulate data that comes instantly?

Asked

Viewed 12 times

-1

I can get the data I want through a webhook, but I want to know how to manipulate this data that comes to me with file_put_contents or even save bd. The way I’m doing I can’t save to txt file

     function receiveMessage(){
    $update_response = file_get_contents("php://input");
    $update = json_decode($update_response, true);
     
    $dataMesseger = $update['entry'][0]['messaging'];//array que contem os dados principais das mensagens 

    $sender=  $dataMesseger[0]['sender']['id'];// id de quem envio mensagem para
    $recipient = $dataMesseger[0]['recipient']['id'];// id quem esta recebendo a mensagem
    $msg = $dataMesseger[0]['message']['text'];//texto recebido na mensagem
  
    // print_r("Sender: ".$sender);
    // print_r("Recipient: ".$recipient);
    // print_r("Messege: ".$msg);

    $dados = array(
        'sender' => $sender,
        'recipient' => $recipient,
        'messege' => $msg
    );

    var_dump($dados);
 
    file_put_contents('dados.txt',$dados);
    return json_encode($dados);
}

receiveMessage();

1 answer

0

Try this on before your Return json_encode

$myfile = fopen("dados_" . date('Y-m-d H:i:s') . ".txt", "w") or die("falha ao gerar arquivo!");
  $txt = "";
  fwrite($myfile, $txt);
  $txt = json_encode($dados);
  fwrite($myfile, $txt);
  fclose($myfile);

Browser other questions tagged

You are not signed in. Login or sign up in order to post.