This Website is in Experimental format. - By use of this site you understand the Terms of Service

Post to Facebook Via PHP

Posted by engineerisaac


Posted on 5 months ago

Script:




error_reporting(E_ALL);

session_start();



// Source Code Engineerisaac.com



// Download The facebook - API https://github.com/facebook/php-graph-sdk

define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__.'/Facebook/');



require_once(__DIR__.'/Facebook/autoload.php');



// Add ID and Secret You can create an app here - https://developers.facebook.com/apps/

$define_appid = '(Place your App ID here);

$define_appsecret = '(Place your App Secret here)';



// The page name is found - https://www.facebook.com/pg/(YourFB_Page@TagGoesHERE)/about/?ref=page_internal

$define_pagename = '(Enter your page name here)';



// What do you want to say?

$define_message = "Testing Message";

$define_source = "http://www.example.com";



// Access Token can be generated here - https://developers.facebook.com/tools/explorer/

$accessToken = '(Put your Access token here)';



// Advanced Modification below.



// Renew Access Token

$curl = curl_init();

curl_setopt_array($curl, array(

CURLOPT_RETURNTRANSFER => 1,

CURLOPT_URL => 'https://graph.facebook.com/v2.11/me?fields=id%2Cname%2Cdevices&access_token='.$accessToken.'"',

// Pretend to be Chrome.

CURLOPT_USERAGENT => 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/62.0.3202.94 Safari/537.36'

));



$resp = curl_exec($curl);

print_r($resp);

curl_close($curl);







$fb = new Facebook\Facebook([

'app_id' => $define_appid,

'app_secret' => $define_appsecret,

'default_graph_version' => 'v2.2',

]);





$helper = $fb->getRedirectLoginHelper();

$permissions = ['manage_pages', 'publish_pages']; // optional

try {

if (isset($_SESSION['facebook_access_token'])) {

$accessToken = $_SESSION['facebook_access_token'];

} else {

$accessToken = $helper->getAccessToken();

}

} catch(Facebook\Exceptions\FacebookResponseException $e) {

echo 'Graph returned an error: ' . $e->getMessage();

exit;

} catch(Facebook\Exceptions\FacebookSDKException $e) {

echo 'Facebook SDK returned an error: ' . $e->getMessage();

exit;

}

if (isset($accessToken)) {

if (isset($_SESSION['facebook_access_token'])) {

$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);

} else {

// getting short-lived access token

$_SESSION['facebook_access_token'] = (string) $accessToken;

// OAuth 2.0 client handler

$oAuth2Client = $fb->getOAuth2Client();

// Exchanges a short-lived access token for a long-lived one

$longLivedAccessToken = $oAuth2Client->getLongLivedAccessToken($_SESSION['facebook_access_token']);

$_SESSION['facebook_access_token'] = (string) $longLivedAccessToken;

// setting default access token to be used in script

$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);

}





try {

$profile_request = $fb->get('/me');

$profile = $profile_request->getGraphNode()->asArray();

} catch(Facebook\Exceptions\FacebookResponseException $e) {

// When Graph returns an error

echo 'Graph returned an error: ' . $e->getMessage();

session_destroy();

// redirecting user back to app login page

header("Location: ./");

exit;

} catch(Facebook\Exceptions\FacebookSDKException $e) {

// When validation fails or other local issues

echo 'Facebook SDK returned an error: ' . $e->getMessage();

exit;

}

// post on behalf of page

$pages = $fb->get('/me/accounts');

$pages = $pages->getGraphEdge()->asArray();

foreach ($pages as $key) {

if ($key['name'] == $define_pagename ) {

$post = $fb->post('/' . $key['id'] . '/feed', array(

'message' => $define_message,

'source' => $define_source

), $key['access_token']);

$post = $post->getGraphNode()->asArray();

print_r($post);

}

}

}




Comments are For the Community.

If you would like to Comment, Consider joining the Community!

We believe in accountability. Your comments are connected to your profile.

Register Login


Other Engineerisaac Articles