Facebook sends empty POST to a webhook
Asked Answered
W

4

6

I am trying to install a webhook for leadgen event for my page via a facebook app. So I have:

  1. Facebook Page where leads come from
  2. Facebook App
  3. Webserver where I want to save leads

App and webserver are connected well I believe. Webhook is shown at app page etc. But when I am trying to create a test lead with this tool https://developers.facebook.com/tools/lead-ads-testing I am getting a POST request with no data in it.

I was suspecting permissions problems, but I am able to check a lead from page (via leadgen_id) directly with PHP SDK and the POST request is sent from Facebook just by URL, so they don't know about tokens yet.

UPD Plain POST request to the same url (curl -d "param=value" https://..url..) works as expected.

Whisenant answered 10/3, 2016 at 12:55 Comment(0)
H
2

Facebook sends the leads data in the request body. If you are using a framework, please check if you have access to the request body. Try using a third party intermediate service like Runscope to see the full request, it is very usef

Hotien answered 10/3, 2016 at 13:33 Comment(1)
Okay, I see what are you talking about. It's being sent as a serialized string, not as a set of parameters. Thank you!Whisenant
H
8

Facebook sends webhook data as Content-Type: application/json, not as …: application/x-www-form-urlencoded (as a normal form with method=post would.)

Therefor, PHP does not populate $_POST – you need to read the raw input stream instead. That can be done using file_get_contents('php://input') – and then just apply json_decode on that data, and you’ll have a proper data structure to work with.

Helfrich answered 10/3, 2016 at 15:22 Comment(0)
H
2

Facebook sends the leads data in the request body. If you are using a framework, please check if you have access to the request body. Try using a third party intermediate service like Runscope to see the full request, it is very usef

Hotien answered 10/3, 2016 at 13:33 Comment(1)
Okay, I see what are you talking about. It's being sent as a serialized string, not as a set of parameters. Thank you!Whisenant
B
2

This code works for me...

if (!empty($_REQUEST['hub_mode']) && $_REQUEST['hub_mode'] == 'subscribe' && $_REQUEST['hub_verify_token'] == "<mytoken>") {
    echo $_REQUEST['hub_challenge'];
} else {
    $data = json_decode(file_get_contents("php://input"), true);
    file_put_contents('logFB.txt', print_r($data, true));
}  

First part is for verifying webhook, second for getting data from facebook webhook.

Hope this will help...

Bewray answered 28/2, 2017 at 8:47 Comment(0)
B
1

In case you are using the django framework, you should request.body, as post data will remain empty.

def webhook_response(request):
   # it will print the contents from facebook webhook response
   print(request.body)

   # something like the following object will print up if you are using leadgen
   {"object": "page", "entry": [{"id": "0", "time": 111111111, "changes": [{"field": "leadgen", "value": {"ad_id": "444444444", "form_id": "444444444444", "leadgen_id": "444444444444", "created_time": 11111111, "page_id": "444444444444", "adgroup_id": "44444444444"}}]}]}
Booby answered 6/8, 2020 at 7:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.