Cernokneznik
Cernokneznik

Reputation: 21

Facebook Messenger Bot blank response

I've just stumbled upon messenger bot and felt like doing myself one too. I've setuped webhooks correctly, verified my webhook script and made myself a temporary simple thing to see the request when I send message to my bot.

<?php
$file = "data.txt";
$current = file_get_contents($file);
$data = $current ."\n". json_encode($_REQUEST);
file_put_contents ( $file , $data  );

It works (catches all the requests), but whenewer I type in the chat, I get just a blank [] in my file. This means facebook contacts my siete when I try to communicate with the bot, but without any request (data)?

Could somebody tell me what am I doing wrong? Thanks!

Upvotes: 0

Views: 481

Answers (1)

Mukarram Khalid
Mukarram Khalid

Reputation: 2143

Facebook hits your webhook with Content-type application/json and JSON string in the request body. $_REQUEST cannot handle it because $_REQUEST contains the data with HTTP Content-type application/x-www-form-urlencoded or multipart/form-data. Here, you need to read the input stream (raw data).

<?php
$file = "data.txt";
$current = file_get_contents($file);
$data = $current ."\n". file_get_contents('php://input');
file_put_contents ( $file , $data  );

Upvotes: 1

Related Questions