-
Notifications
You must be signed in to change notification settings - Fork 24
/
webhook-forms.php
56 lines (51 loc) · 1.92 KB
/
webhook-forms.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
<?php
// facebook variables
$challenge = isset($_REQUEST['hub_challenge']) ? $_REQUEST['hub_challenge'] : '';
$verify_token = isset($_REQUEST['hub_verify_token']) ? $_REQUEST['hub_verify_token'] : '';
$fb_access_token = "";
// this is used to subscribe to facebook webhook
if ($verify_token === "mautic01") { //
echo $challenge;
}
// Process retrieved data from facebook webhook
$data = json_decode(file_get_contents("php://input"),true);
$leadgen_id = $data['entry'][0]['changes'][0]['value']['leadgen_id']; // extract leadgen ID
if($leadgen_id){
$ch = curl_init();
$url = "https://graph.facebook.com/v2.8/".$leadgen_id;
$url_query = "access_token=".$fb_access_token;
$url_final = $url.'?'.$url_query;
curl_setopt($ch, CURLOPT_URL, $url_final);
curl_setopt($ch, CURLOPT_HTTPGET, 1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$response = curl_exec($ch);
curl_close ($ch);
$data = json_decode($response, true);
$lead_first = $data['field_data'][2][values][0];
$lead_last = $data['field_data'][1][values][0];
$lead_email = $data['field_data'][0][values][0];
}
// check if we have a valid lead before calling Mautic Api
if($lead_email){
//Create a new Contact
$data =array();
$data['first_name'] = $lead_first;
$data['last_name'] = $lead_last;
$data['email'] = $lead_email;
$data['submit'] = "";
$data['formId'] = 3; // change this to the form ID that you created in your Mautic
$formId = $data['formId'];
$data = array('mauticform' => $data);
// Change [path-to-mautic] to URL where your Mautic is
$formUrl = '[path-to-mautic]/form/submit?formId=' . $formId;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $formUrl);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
return $response;
echo "Contact Added!";
} // end valid lead check
?>