facebook PHP SDK facebookrequest class using facebook\facebookapp - facebook-graph-api

I'm using facebook PHP SDK and GRAPH API to get all the posts of a page (which is I am an admin and It is a public page). I'm stuck by this instruction
$fbApp = new Facebook\FacebookApp('{app-id}', '{app-secret}');
$request = new Facebook\FacebookRequest($fbApp, '{access-token}', 'GET', '/me');
$fb = new Facebook\Facebook(/* . . . */);
$request = $fb->request('GET', '/me');
try {
$response = $fbApp->getClient()->sendRequest($request);
} 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 'User name: ' . $graphNode['name'];
instructions from here https://developers.facebook.com/docs/php/FacebookRequest/5.0.0#overview
it returns an error Call to undefined method Facebook\FacebookApp::getClient()
notice I'm usicng FacebookApp for getclient function so my question is how do I use facebookapp instead of facebook? to send a request to GRAPH and get a response.
also,
$request = new FacebookRequest(
$session,
'GET',
'/myfbpage/posts'
);
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
was recommended to me to be the code for PHP SDK got this from: https://developers.facebook.com/tools/explorer/
I also don't see a method execute(); in Facebookrequest is facebook documentation wrong or am I using the wrong PHP SDK?

Related

Error when extracting comments in my facebook page through laravel app using Facebook graph API

I'm trying to extract comments in my facebook page. For that I made a Facebook graph app. I can extract the comments in Graph API explorer using {page-id}?fields=posts.limit(1000){comments} query.
But when I tried to get comments through a laravel app, it gives me an error. The code in laravel app is
$fb = new Facebook([
'app_id' => getenv('FACEBOOK_APP_ID'),
'app_secret' => getenv('FACEBOOK_APP_SECRET'),
'access_token' => getenv('FACEBOOK_ACCESS_TOKEN'),
'default_graph_version' => 'v3.3',
]);
$accessToken=getenv('FACEBOOK_ACCESS_TOKEN');
try {
$response = $fb->get('/{page-id}?fields=posts.limit(1000){comments}',$accessToken);
} catch(Facebook\Exceptions\FacebookResponseExcepti9on $e) {
$e->getMessage();
exit;
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// echo 'Facebook SDK returned an error: ' . $e->getMessage();
$e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
Error is
(#10) To use 'Page Public Content Access', your use of this endpoint must be reviewed and approved by Facebook. To submit this 'Page Public Content Access' feature for review please read our documentation on reviewable features: https://developers.facebook.com/docs/apps/review.
This is how my graph permission and features page looks. What am I doing wrong?

Facebook Graph API SDK v5 - Publishing a picture to a page's Album

I'm trying to publish a picture to an album belonging to a Facebook page, using the Facebook PHP SDK v5.
I've already checked the (many) similar questions, however they are all related to prior versions of the SDK, when publish_actions was still allowed.
My Setup:
My app is properly configured and I was able to retrieve my access token
I was able to run a few test API queries with no issues
I'm the admin of this page and my access token has the following permissions ['manage_pages','publish_pages','user_photos']
The SDK was properly initialized with 'fileUpload' => true as a parameter
Code publishing the picture to the album
It is based on the example provided in the SDK documentation.
<?php
require_once __DIR__.'/vendor/autoload.php';
$fb = new Facebook\Facebook(['app_id' => '123456', 'app_secret' => '123456', 'default_graph_version' => 'v3.3', 'fileUpload' => true]);
$picture = 'https://www.example.com/pic.jpg';
$album_id = '123456';
$access_token = 'mytoken';
try {
$response = $fb->post('/'.$album_id.'/photos', array ('url' => $picture), $access_token);
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
die('Graph returned an error: ' . $e->getMessage());
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
die('Facebook SDK returned an error: ' . $e->getMessage());
}
$graphNode = $response->getGraphNode();
Here's the error I'm getting
Graph returned an error: (#200) This endpoint is deprecated since the
required permission publish_actions is deprecated
Indeed, changes introduced in April 2018 removed the ability to use publish_actions however it seems the documentation has not been updated accordingly.
What is the new way to publish a picture to a page's album?
Your help is much appreciated!
After several hours of researching alternate solutions, I was able to find a workaround.
Apparently I was using an User Access Token instead of the Page Access Token.
Solution
1 - First, verify if your token is a User/Page Access token
2 - If it is an User Token, you need to request the Page Access Token instead:
$user_access_token = 123456;
$page_id = 123456; // Can be retrieved via right click on your page logo + Copy link address
$response = $fb->get('/'.$page_id.'?fields=access_token', $user_access_token);
$page_access_token = json_decode($response->getBody())->access_token;
echo $page_access_token;
3 - Finally, post the picture on the Page using the Page Access Token
Full code
<?php
require_once __DIR__.'/vendor/autoload.php';
$fb = new Facebook\Facebook(['app_id' => '123456', 'app_secret' => '123456', 'default_graph_version' => 'v3.3', 'fileUpload' => true]);
$picture = 'https://www.example.com/pic.jpg';
$album_id = '123456';
$user_access_token = 'mytoken';
$page_id = 123456;
try {
$response = $fb->get('/'.$page_id.'?fields=access_token', $user_access_token);
} catch(Facebook\Exceptions\FacebookResponseException $e) {
die('Graph returned an error: ' . $e->getMessage());
} catch(Facebook\Exceptions\FacebookSDKException $e) {
die('Facebook SDK returned an error: ' . $e->getMessage());
}
$page_access_token = json_decode($response->getBody())->access_token;
try {
$response = $fb->post('/'.$album_id.'/photos', array ('url' => $picture), $page_access_token);
}
catch(Facebook\Exceptions\FacebookResponseException $e) {
die('Graph returned an error: ' . $e->getMessage());
}
catch(Facebook\Exceptions\FacebookSDKException $e) {
die('Facebook SDK returned an error: ' . $e->getMessage());
}
$graphNode = $response->getGraphNode();
I hope this helps!

"The domain of this URL isn't included in the app's domains" when it is

I'm trying to post to a Facebook wall from a PHP script. I've created a FB app, installed the Graph PHP API, and implemented some test scripts as follows:
fbinit.php:
<?php
session_start();
require_once('src/Facebook/autoload.php');
$fb = new Facebook\Facebook([
'app_id' => 'REDACTED',
'app_secret' => 'REDACTED',
'default_graph_version' => 'v2.9',
]);
?>
fbpost.php:
<?php
include('fbinit.php');
$helper = $fb->getRedirectLoginHelper();
$permissions = ['manage_pages','publish_pages']; //'publish_actions'
$loginUrl = $helper->getLoginUrl('https://www.REDACTED.net/fb-callback.php', $permissions);
echo 'Log in with Facebook!';
?>
fb-callback.php:
<?php
include('fbinit.php');
$helper = $fb->getRedirectLoginHelper();
$_SESSION['FBRLH_state']=$_GET['state'];
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)) {
echo 'No OAuth data could be obtained from the signed request. User has not authorized your app yet.';
exit;
}
try {
$response = $fb->get('me/accounts', $accessToken->getValue());
$response = $response->getDecodedBody();
} 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;
}
echo "<pre>";
print_r($response);
echo "</pre>";
?>
The first time I opened fbpost.php, I was asked to log in to Facebook as expected, and it asked for permission to post on my behalf on the page, which is fine. But then I am redirected to the call back page and presented with the following error:
Graph returned an error: Can't load URL: The domain of this URL isn't included in the app's domains. To be able to load this URL, add all domains and sub-domains of your app to the App Domains field in your app settings.
I have added every combination of the app URL's and callback URL's I can think of, but nothing works. See below for screenshots of the app settings. The App ID and secret are definitely correct.
The value of the redirect_uri parameter needs to be the exact same in your login dialog call, and the subsequent API call that tries to exchange the code for a token.
When you have generation of the login URL and handling of the response spread over different scripts (i.e., called via different URLs), that can easily lead to problems like this. The SDK tries to figure out the value based on the current script URL, if left to its own devices.
In such a case, explicitly specify the callback URL in your getAccessToken method call as well.

Get Facebook Post/Photo Url Using Facebook API

I created an Facebook app that post my webpage photo on my facebook page by clicking on a button.
Now I also want to show my facebook post url on my webpage.
Means when I'll click on the button on my website, it will publish a photo on facebook and then I want to show that published photo url on my page from facebook.
I'm using this code to publish on facebook:-
<?php
require_once("/path-to/facebook_php_sdk/facebook.php"); // set the right path
$config = array();
$config['appId'] = 'your-app-id';
$config['secret'] = 'your-secret-key;
$config['fileUpload'] = true; // optional
$fb = new Facebook($config);
$params = array(
"access_token" => "your_access_token",
"url" => "http://photo-url",
);
try {
$ret = $fb->api('/fb-page-id/photos', 'POST', $params);
echo 'Successfully posted photo to Facebook Fan Page';
} catch(Exception $e) {
echo $e->getMessage();
}
?>
I have found the answer myself.
You can use this code to get Facebook post/photo url using fb api
try
{
$ret = $fb->api('/fb-page-id/photos', 'POST', $params);
echo 'Successfully posted photo to Facebook Fan Page';
$fbdurl = $ret[post_id];
$url= "https://facebook.com/".$fbdurl;
}
catch(Exception $e) {
echo $e->getMessage();
}

Is it possible Facebook PHP SDK 4.0 and Graph API v2.8?

I am using Facebook PHP SDK 4.0 along with Graph API v2.2. I wondered if it is possible to use Graph API v2.8 with this PHP SDK 4.0 (I've got PHP 5.2 installed on my server and I can not use PHP SDK 5.0).
Currently, I use the following code to obtain the user ID
$facebook = new Facebook(array(
'appId' => $facebookAppId,
'secret' => $facebookSecret
));
$user = $facebook->getUser();
if ($user) {
$user_profile = $facebook->api('/me');
$user_id = $user_profile['id'];
}
I was warned that the API v2.2 will be deprecated late this month, and I am trying to migrate my code to API v2.8.
Thank you very much.
// Initialize the Facebook PHPSDK5 and v2.8.
$fb = new Facebook\Facebook([
'app_id' => 'xxxxxxxxxxxx',
'app_secret' => 'xxxxxxxxxxxxxxxxxxxxxxxx',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getRedirectLoginHelper();
try {
$accessToken = $helper->getAccessToken();
} catch(Facebook\Exceptions\FacebookSDKException $e) {
// There was an error communicating with Graph
echo $e->getMessage();
exit;
}
if (isset($accessToken)) {
// Logged in.
$_SESSION['facebook_access_token'] = (string) $accessTokenLong;
}
$fb->setDefaultAccessToken($_SESSION['facebook_access_token']);
$me = $fb->get('/me');
$me_array = $me->getGraphEdge()->asArray();