Skip to content

Instantly share code, notes, and snippets.

@thanhhh
Created November 17, 2015 08:27
Show Gist options
  • Save thanhhh/189c206951868bc22e07 to your computer and use it in GitHub Desktop.
Save thanhhh/189c206951868bc22e07 to your computer and use it in GitHub Desktop.
Auto post to Facebook page
<?php
require_once __DIR__ . '/vendor/autoload.php';
use Facebook\Authentication\AccessToken;
use Facebook\FacebookApp;
use Facebook\FacebookRequest;
session_start();
$app_id = '{APP ID}';
$app_secret = '{APP SECRET}';
$app = new FacebookApp($app_id, $app_secret);
$fb = new Facebook\Facebook(array(
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
));
//Page access token has been got from get_page_access_token.php
$access_token = '{PAGE ACCESS TOKEN}';
$page_id = '{PAGE ID}';
$post_data = array(
'message' => '{Your message data here}'
);
$request = new FacebookRequest($app, $access_token, 'POST', '/' . $page_id . '/feed', $post_data);
// Send the request to Graph
try {
$response = $fb->getClient()->sendRequest($request);
$graphNode = $response->getGraphNode();
echo 'Post ID: ' . $graphNode['id'];
} catch(Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
@Aluui
Copy link

Aluui commented Apr 4, 2019

Question: Is it possible to use this to post to the Facebook Marketplace?

@ProjectSeve
Copy link

is this working until now?
any updates?

@thanhhh
Copy link
Author

thanhhh commented Sep 8, 2021

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