Skip to content

Instantly share code, notes, and snippets.

@jimitit
Created December 15, 2015 15:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jimitit/9563db83f6c978761ca8 to your computer and use it in GitHub Desktop.
Save jimitit/9563db83f6c978761ca8 to your computer and use it in GitHub Desktop.
Facebook webhook
<?php
require_once __DIR__ . '/config/facebook.php';
require_once __DIR__ . '/vendor/autoload.php';
require_once __DIR__ . '/lib/utility.class.php';
use Facebook\Facebook;
use Facebook\Exceptions\FacebookResponseException;
use Facebook\Exceptions\FacebookSDKException;
use FacebookAds\Object\AdUser;
use FacebookAds\Api;
use FacebookAds\Object\AdAccount;
use FacebookAds\Object\CustomAudience;
use FacebookAds\Object\Fields\CustomAudienceFields;
use FacebookAds\Object\Values\CustomAudienceSubtypes;
use FacebookAds\Object\Values\CustomAudienceTypes;
use Facebook\FacebookRequest;
use Facebook\FacebookSession;
$utility = new Utility();
$tokenInfo = $utility->readToken();
$fb = new Facebook([
'app_id' => $config['app_id'],
'app_secret' => $config['app_secret'],
]);
//App Access Token
//$fb->setDefaultAccessToken($config['app_access_token']);
$fb->setDefaultAccessToken($tokenInfo->_token);
/*$data = array(
'object' => 'page',
'callback_url' => 'http://www.marriagecircle.in/infufb/webhook.php',
'fields' => 'conversations',
'verify_token' => 'CAAK3ZCgnFDPABAMnJ',
'access_token' => $config['app_access_token']
);
try {
$response = $fb->post('/' . $config['app_id'] . '/subscriptions', $data);
} catch(FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}*/
// Verifying the verify_token ensures nobody else can
// subscribe random things to your application.
if (isset($_REQUEST['hub_verify_token']) && $_REQUEST['hub_verify_token'] === 'CAAK3ZCgnFDPABAMnJ') {
echo $_REQUEST['hub_challenge'];
exit;
}
Api::init(
$config['app_id'], // App ID
$config['app_secret'],
$tokenInfo->_token
);
$accounts = Api::instance()->call('/me/accounts');
$response = $fb->get('/me/accounts');
$accounts = $response->getDecodedBody();
$pageToken = $accounts['data'][0]['access_token'];
$pageId = $accounts['data'][0]['id'];
$pageName = $accounts['data'][0]['name'];
/*$fb->setDefaultAccessToken($pageToken);
$response = $fb->post('/' . $pageId . '/subscribed_apps', array(
'access_token' => $pageToken
));*/
//echo '<pre>';
//var_dump($response);
//exit;
try {
$fb->setDefaultAccessToken($tokenInfo->_token);
$response = $fb->post('/' . $config['app_id'] . '/subscriptions_sample', array(
'object_id' => $pageId,
'object' => 'page',
'field' => 'leadgen',
'access_token' => $tokenInfo->_token,
'custom_fields' => json_encode(array(
'page_id' => $pageId
))
));
} catch(FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
echo '<pre>';
var_dump($response);
$inputJSON = file_get_contents('php://input');
//$input = json_decode($inputJSON, true);
//var_dump($inputJSON);
$utility = new Utility();
$ret = $utility->writeToFile('webhook.txt', $inputJSON);
var_dump($ret);
/*$page_id = $input['entry'][0]['changes'][0]['value']['page_id'];

$conversation_id = $input['entry'][0]['changes'][0]['value']['conversation_id'];
$field = $input['entry'][0]['changes'][0]['value']['field'];

switch ($field) {
case 'conversations':
$request = new FacebookRequest(
$session,
'GET',
'/' . $page_id . '/conversations'
);
$response = $request->execute();
$conversations = $response->getGraphObjectList();
foreach ($conversations as $conversation) {
$messages = $conversation->getProperty('messages')->asArray();
foreach ($messages['data'] as $message) {
// Again just using error_log so I can tail my log file.
error_log(sprintf(
"%s sent by %s (%s)",
$message->message,
$message->from->name,
$message->from->id
));
}
}
break;
}
*/
@ugljanin
Copy link

Hi,

Could you please help me in defining webhooks for facebook page. I have created page hook and user hook and verify them, but changes on page feed are not triggering webhook, only on user feed. I am logging every request in txt files so I track what is happening.

@albertleao
Copy link

Do you not verify the incoming requests are valid?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment