function processFbEvent($event) { foreach ($event->changes as $change) { switch ($change->field) { case 'feed': // process new comment logic if ($change->value->item == 'comment' && $change->value->verb == 'add' && $change->value->parent_id == $change->value->post_id && $change->value->sender_id != PAGE_ID && (USER_COMMENT_AUTO_LIKE_ON || USER_COMENT_AUTO_REPLY_ON)) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN, 'fields' => 'from{id}']; $postAuthorId = json_decode(fb_api_get("/" . $change->value->post_id, $params))->from->id; logToFile('curl.log', $postAuthorId); if ($postAuthorId != PAGE_ID) { continue; } if (USER_COMMENT_AUTO_LIKE_ON) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN]; fb_api_post("/{$change->value->comment_id}/likes", $params); } if (USER_COMMENT_AUTO_REPLY_ON) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN, 'message' => USER_COMMENT_AUTO_REPLY_MSG]; fb_api_post("/{$change->value->comment_id}/comments", $params); } } // process new post logic if ($change->value->item == 'post' && $change->value->verb == 'add' && $change->value->sender_id != PAGE_ID && (USER_POST_AUTO_LIKE_ON || USER_POST_REPLY_ON)) { if (USER_POST_AUTO_LIKE_ON) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN]; fb_api_post("/{$change->value->post_id}/likes", $params); } if (USER_POST_AUTO_REPLY_ON) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN, 'message' => USER_POST_AUTO_REPLY_MSG]; fb_api_post("/{$change->value->post_id}/comments", $params); } } break; case 'conversations': if (CONVERSATION_REPLY_ON) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN, 'fields' => 'from,created_time', 'since' => time() - CONVERSATION_AUTO_REPLY_COOLDOWN]; $lastMsgs = json_decode(fb_api_get("/{$change->value->thread_id}/messages", $params)); logToFile('curl.log', print_r($lastMsgs, 1)); $needReply = true; foreach ($lastMsgs as $msg) { if ($msg['from']['id'] == PAGE_ID) { $needReply = false; break; } } if ($needReply) { $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => PAGE_VERIFY_TOKEN, 'message' => CONVERSATION_AUTO_REPLY_MSG]; fb_api_post("/{$change->value->thread_id}/messages", $params); } } break; default: //do nothing } } }
<?php require_once 'config.php'; require_once 'fb_api_controller.php'; //request longLived access token, to get longLive pageAccessToken $params = ['grant_type' => 'fb_exchange_token', 'fb_exchange_token' => USER_ACCESS_TOKEN, 'client_id' => APP_ID, 'client_secret' => APP_SECRET]; var_dump(fb_api_get('/oauth/access_token', $params)); $longLivedAccessToken = explode('=', fb_api_get('/oauth/access_token', $params))[1]; //get page tokens and ids for each page $params = ['client_id' => APP_ID, 'client_secret' => APP_SECRET, 'access_token' => $longLivedAccessToken]; $accounts = json_decode(fb_api_get('/me/accounts', $params)); foreach ($accounts->data as $page) { echo $page->name . " : " . $page->access_token . "\n"; echo "Page ID: " . $page->id . "\n"; } //get app access token $params = ['grant_type' => 'client_credentials', 'client_id' => APP_ID, 'client_secret' => APP_SECRET]; $appAccessToken = explode('=', fb_api_get('/oauth/access_token', $params))[1]; echo "APP access token: " . $appAccessToken . "\n";