I had completed setting for ngrok and webhook on facebook.
this is very simple webhook by PHP
$access_token = "my_access_token";
$verify_token = "my_verify_token";
$hub_verify_token = "";
if (isset($_REQUEST['hub_challenge'])) {
$challenge = $_REQUEST['hub_challenge'];
$hub_verify_token = $_REQUEST['hub_verify_token'];
}
if ($hub_verify_token === $verify_token) {
echo $challenge;
exit();
}
$input = json_decode(file_get_contents('php://input'), true);
$sender = $input['entry'][0]['messaging'][0]['sender']['id'];
$message = $input['entry'][0]['messaging'][0]['message']['text'];
$message_to_reply = 'I could see your message';
$myjsondata = '{
"recipient":{
"id":"' . $sender . '"
},
"message":{
"text":"' . $message_to_reply . '"
}
}';
send("me/messages", $mydata, $input, $access_token);
function send($type, $data, $input, $access_token) {
$url = 'https://graph.facebook.com/v2.11/' . $type . '?access_token=' . $access_token;
$ch = curl_init($url);
$jsonDataEncoded = $data;
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $jsonDataEncoded);
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/json'));
if (!empty($input['entry'][0]['messaging'][0]['message'])) {
$result = curl_exec($ch);
// **this $result is always NULL**
}
}
Result, i could receive json info from user typing in my localhost envirment. But when excuting curl, this $result is always NULL and having no error ,so weird. What's wrong with this ?
PS: If i change webhook to cloud host, Operation is normal but hard to debug :(