Post daily on Facebook with the Facebook SDK - php

first i want to ask if its possible to post on a facebook page all day a post with the PHP SDK.
Here is my version (it works but the token expires after some hours)
<?php
define('FACEBOOK_SDK_V4_SRC_DIR','../sdks/facebook/');
require_once("../sdks/facebook/autoload.php");
$access_token ="EAACEdEose0cBAGVFnr3vE8U6FYXxsJbRadxZB4ynIWaG5aFzDo0bx6vdIWysh4sPvnOPu3CHZCfLSM9MYLRRlvdeyJazIXgP0h3fqbbNc3oxg24b4I7rZChUS1FNHqm1HsWbym4IjsaoVw4fAqRlYfPsw2iX4nFZB7KDeAu1BglrRg2qGug8";
$app_secret ="c19ca5fc56bc0ddce32c75a3cb53059f";
$fb = new Facebook\Facebook([
'app_id' => '17852532523721',
'app_secret' => $app_secret,
'default_graph_version' => 'v2.4',
]);
$linkData = [
'link' => 'http://www.url.com/',
'message' => "Test"
];
try {
$response = $fb->post("/42935243503912968/feed",$linkData, $access_token);
} 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;
}
?>
It's possibole to get the actual acces_token of my users. Maybe someone now a solution.
Big thanks

Related

Facebook graph return only the id

I used Facebook SDK for get share count for posts in web site, but when I send the request the responses is only the id
$link = 'url-i-want-get-share-count';
$access_token="My-Access-token";
$app_id="My-app-id";
$app_secret="My-app-secret";
$fb = new \Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => 'v2.5',
'default_access_token' => $access_token, // optional
]);
try {
$request = $fb->get('/' . $link . '/posts?fields=og_object{engagement}');
} catch(\Facebook\Exceptions\FacebookResponseException $e) {
// When Graph returns an error
echo 'Graph returned an error: ' . $e->getMessage();
} catch(\Facebook\Exceptions\FacebookSDKException $e) {
// When validation fails or other local issues
echo 'Facebook SDK returned an error: ' . $e->getMessage();
}
var_dump($request);
the response is

how to post a link on facebook use sdk PHP

i'm creating function to share a post to facebook and i'm following docs : https://developers.facebook.com/docs/php/howto/postwithgraphapi. but it's not work.
This is my function
public function shareFB(Request $request, $slugify)
{
$petition = Petitions::where('slug', $slugify)->first();
$access_token = auth()->user()->social_account->access_token;
$fb = new Facebook\Facebook([
'app_id' => config("services.facebook.client_id"),
'app_secret' => config("services.facebook.client_secret"),
'default_graph_version' => 'v2.2',
]);
$linkData = [
'link' => route('petitions.index',$petition->slug),
'message' => $request->get('shareFB'),
];
try {
$response = $fb->post('/me/feed','post', $linkData, $access_token);
} 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;
}
$share = $response->getGraphNode();
}
and here is my route in laravel
Route::post('share/p/{slugify}','Frontend\SocialController#shareFB')->name('social.shareFB');
When i clicked a button have href above, it's show MethodNotAllowedHttpException . somebody help me solve that!!!

Post Image on Facebook Page Feed as Page

Using this example from fb-sdk:
require_once 'fb-sdk/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => '123456',
'app_secret' => 'abc1233',
'default_graph_version' => 'v2.6',
]);
$data = [
'message' => 'Testupload',
'source' => $fb->fileToUpload('image.jpg'),
];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->post('/PageID/photos', $data, 'XXXXXXYYYYYY');
} 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;
}
$graphNode = $response->getGraphNode();
echo 'Photo ID: ' . $graphNode['id'];
i am able to post as myself to the wall of my facebook-page.
'app_id' and 'app_secret' come from the app i created for this.
If i set $response = $fb->post('/me/photos', $data, 'XXXXXXYYYYYY'); the post shows up on my timeline.
What do i need to change, so get a photo, posted by "Page" on the feed of "Page"?
You need to use a Page Token with the publish_pages permission to post "as Page". You can get a Page Token with the following API call, after authorizing with manage_pages and publish_pages:
/page-id?fields=access_token
More information:
https://developers.facebook.com/docs/facebook-login/access-tokens
http://www.devils-heaven.com/facebook-access-tokens/

How do you pass multiple scope parameters through a Facebook PHP SDK to execute queries that require permissions?

I have a current 3-page web site/app to pull/display data from Facebook using my PHP SDK code, I'm getting the error:
The redirect_uri URL must be absolute
This is because of this code below:
$params = array(
'scope' => 'ads_management',
'redirect_uri' => 'http://example.com/login-callback.php',
);
$loginUrl = $helper->getLoginUrl($params);
Before changing it to this, I didn't have this ^ at all, and just this line instead:
$loginUrl = $helper->getLoginUrl('http://example.com/login-callback.php');
If I were to query posts and stuff that didn't require permissions, things worked smoothly. But I need to do queries like the ones below, located in fb-stats.php. I guess the way I have the params is wrong, but I don't exactly know the right way to format it.
Here's fb.php:
<?php
session_start();
require_once '../vendor/autoload.php';
// Checks if token is set and redirects to another page.
if (isset($_SESSION['facebook_access_token'])) {
// Logged in!
header('Location: fb-stats.php'); exit();
}
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
// The site below is 'example' is actually my live site.
$params = array(
'scope' => 'ads_management',
'redirect_uri' => 'http://example.com/login-callback.php',
);
$loginUrl = $helper->getLoginUrl($params);
echo 'Log in with Facebook!';
?>
Here's login-callback.php:
<?php
session_start();
require_once '../vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$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 (! $accessToken->isLongLived()) {
try {
$accessToken = $oAuth2Client->getLongLivedAccessToken($accessToken);
}
catch (Facebook\Exceptions\FacebookSDKException $e) {
echo "<p>Error getting long-lived access token: " . $helper->getMessage() . "</p>\n\n";
exit;
}
}
header('Location: fb-stats.php'); exit();
}
Here's fb-stats.php:
<?php
session_start();
require_once '../vendor/autoload.php';
$fb = new Facebook\Facebook([
'app_id' => 'MYAPPID',
'app_secret' => 'MYAPPSECRET',
'default_graph_version' => 'v2.5',
]);
$fb->setDefaultAccessToken('access-token');
try {
$response = $fb->get('/PAGEID/insights/page_impressions?period=day', '{access token}');
$insightsNode = $response->getGraphEdge();
}
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;
}
var_dump($insightsNode);
The query I'm trying to execute needs permissions of ads_management and read_insights but I'm confused as to how to get mutltiple scopes going aside from the error I'm currently getting for the redirect_uri not being absolute. How would I go about fixing these things?
$params = array(
'scope' => 'ads_management',
'redirect_uri' => 'http://example.com/login-callback.php',
);
$loginUrl = $helper->getLoginUrl($params);
The getLoginUrl method expects two parameters - but you are passing only one array to it here.
The correct way to use this method is
$helper->getLoginUrl('http://example.com/login-callback.php', ['ads_management']);
The redirect URI is passed as the first parameter (string value), and then the second parameter is the scope (array of permissions you want to ask for.)
(Whether you want to put those values into a different array for configuration purposes beforehand, is up to you.)

Graph API method not working

Just one day before the following codes working for me but now it's not working for me.
require 'facebook.php';
$facebook = new Facebook(array( 'appId' =>'APPID','secret' => 'some_secret'));
$user = $facebook->getUser();
if ($user){$user_profile = $facebook->api('/me');}
it's not showing the current loggged in user name or id. Is n't it working anymore ???
Looks like you are using some deprecated api/code examples, current facebook docs have this snippet:
$fb = new Facebook\Facebook([
'app_id' => '{app-id}',
'app_secret' => '{app-secret}',
'default_graph_version' => 'v2.2',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/me?fields=id,name', '{access-token}');
} 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;
}
$user = $response->getGraphUser();
echo 'Name: ' . $user->getName();

Categories