Very new to PHP and I trying to write a program using Facebook PHP SDK and right now I am trying to get the following tutorial to work, https://developers.facebook.com/docs/php/howto/example_retrieve_user_profile, but I keep getting an error. I think it might have to do with the access token. I do not know if I am suppose to put something else there or not. And if I am how do I get my access token? Right now I just left it the way it was in the tutorial. But the error is saying something is wrong with line 2, so maybe the problem is there. Not quite sure. So any help would be greatly appreciated!
Error Message
[north#oreo ~/Facebook]$ php Practice_2.php
Fatal error: Uncaught Error: Class 'Facebook\Facebook' not found in /usr/home/north/Facebook/Practice_2.php:2
Stack trace:
#0 {main}
thrown in /usr/home/north/Facebook/Practice_2.php on line 2
Practice_2.php
<?php
$fb = new Facebook\Facebook([
'app_id' => '{}',
'app_secret' => '{}',
'default_graph_version' => 'v3.3',
]);
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['name'];
// OR
// echo 'Name: ' . $user->getName();
From the docs
When someone connects with an app using Facebook Login and approves the request for permissions, the app obtains an access token that provides temporary, secure access to Facebook APIs.
Related
Facebook SDK return my Unknown path components but i use this sabe path for cURL and works.
<?php
require_once __DIR__ . '/vendor/autoload.php'; // change path as needed
$fb = new \Facebook\Facebook([
'app_id' => '{APP_ID}',
'app_secret' => '{APP_SECRE}',
'default_graph_version' => 'v6.00',
]);
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get(
'/{GROUP_ID}/members',
'{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;
}
$graphNode = $response->getGraphNode();
?>
I tried several ways but same erros, search but same problem with other people, on cURL query this path works, but are 5K limitation.
Error:
Graph returned an error: Unknown path components: /{MY-GROUP-ID}/members
Thanks for help.
https://developers.facebook.com/docs/graph-api/reference/v6.0/group/members:
This edge was deprecated on April 4th, 2018, and can no longer be used.
You can not request a list of members any more.
on cURL query this path works
You mean something like https://www.facebook.com/groups/{group-id}/members/?
Then you would not be using the API in the first place.
Scraping data this way is not allowed.
I'm trying to get a list of the users friends with php but it keeps throwing an exception the code is the same from Facebook for developers docs
https://developers.facebook.com/docs/graph-api/reference/v3.0/user/friends
here is my code
$fb = new \Facebook\Facebook([
'app_id' => APP_ID,
'app_secret' => APP_SECRET,
'default_graph_version' => DEFAULT_GRAPH_VERSION,
//'access_token' => '{'.$fbToken.'}', // optional
]);
try {
$response = $fb->get('/friends', $fbToken);
} catch(FacebookExceptionsFacebookResponseException $e) {
echo 'Graph returned an error: ' . $e->getMessage();
exit;
} catch(FacebookExceptionsFacebookSDKException $e) {
echo 'Facebook SDK returned an error: ' . $e->getMessage();
exit;
}
$graphNode = $response->getGraphNode();
And here is the stacktrace of the error
[03-Jul-2018 07:29:33 UTC] PHP Fatal error: Uncaught exception 'Facebook\Exceptions\FacebookSDKException' with message 'Unable to convert response from Graph to a GraphNode because the response looks like a GraphEdge. Try using GraphNodeFactory::makeGraphEdge() instead.' in /facebook/Facebook/GraphNodes/GraphNodeFactory.php:224
Stack trace:
#0 /facebook/Facebook/GraphNodes/GraphNodeFactory.php(93): Facebook\GraphNodes\GraphNodeFactory->validateResponseCastableAsGraphNode()
#1 /facebook/Facebook/FacebookResponse.php(289): Facebook\GraphNodes\GraphNodeFactory->makeGraphNode(NULL)
#2 {main}
thrown in /facebook/Facebook/GraphNodes/GraphNodeFactory.php on line 224
Try this instead:
$graphNode = $response->getGraphEdge();
Quite old so i did not mark it as duplicate: FB Graph API query doesn't work in PHP SDK
I am struggling going round in circles getting an Facebook App to post to a Page's Wall, as that Page.
** Note: I put out a 300 point bounty to update an outdated answer of a related, well-viewed, existing question. Happy to award anyone who also answers there with the updated, working, solution
In brief:
I have the App Secret and ID
I have Page ID
I am relying on an in-code approach to generating the token I need.
It is based on a solution I saw posted on the SDK GitHub page but it does not work
I am using API v 2.9
I am using the latest PHP SDK
It's just been hard.
The solution I used is purely based on the one below:
<?php
define('FACEBOOK_SDK_V4_SRC_DIR', __DIR__ . '/Facebook');
require_once __DIR__ . '/Facebook/autoload.php';
use Facebook\FacebookRequest;
$app_id = {{your app-id}};
$app_secret = {{your app-secret}};
$fb = new Facebook\Facebook([
'app_id' => $app_id,
'app_secret' => $app_secret,
'default_graph_version' => '{{your app-version}}',
]);
$token = $fb->get(
'/oauth/access_token?client_id=' . $app_id . '&'.
'client_secret=' . $app_secret . '&'.
'grant_type=client_credentials');
$get_token = $token -> getdecodedBody();
$access_token = $get_token['access_token'];
try {
// Returns a `Facebook\FacebookResponse` object
$response = $fb->get('/{{your facebook 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;
}
$body = $response->getBody();
print_r($body);
?>
I am certainly not lazy. I have been stuck on this issue for weeks.
I have tried the following different solutions and have gotten stuck in either a permissions related issue that was also related to depracated functionality and API calls or getting the right token to do it.
Posting to a facebook page from a website using curl (using a pure CURL approach - "The user hasn't authorized the application to perform this action")
Facebook SDK returned an error: You must provide an access token. Php Facebook Api - (suggests that I support login and redirection callback - yet I do not want any user intervention everytime when posting onto the page)
Graph returned an error: Invalid appsecret_proof provided in the API argument - Previous attempt was based on this example but the appsecret-proof issue
Simple example to post to a Facebook fan page via PHP? - Another CURL-based approach that failed me
https://stackoverflow.com/a/14212125/919426 - Best explained step by
step process for permissions and tokens, but still failed
Update:
Tried the following and got the error "Invalid appsecret_proof provided in the API argument"
$fb = new Facebook([
'app_id' => 'APP_ID',
'app_secret' => 'APP_SECRET',
'default_graph_version' => 'v2.9',
]);
$postData = [
'message' => "Test Message",
'picture' => 'https://www.website.com/image.jpg'
];
try
{
// 'LONG_LIVED_PAGE_ACCESS_TOKEN' obtained by going to https://developers.facebook.com/tools/debug/accesstoken and clicking
// "Extend Access Token"
$response = $fb->post('/' . 'PAGE_ID' . '/feed', $postData,"LONG_LIVED_PAGE_ACCESS_TOKEN");
}
catch (FacebookResponseException $e)
{
var_dump("Facebook Response Exception occured: \n" . $e);
exit;
}
catch(FacebookSDKException $e)
{
var_dump("Generic Facebook SDK Exception occured: \n" . $e);
exit;
}
catch(Exception $e)
{
var_dump("Uknown Exception occured while attempting to call the Facebook SDK API: \n" . $e);
exit;
}
There are countless more that I have tried and failed.
I want that my Php script (myscript.php ) that i have deployed on a public host gets the Facebook user access token (with my facebook account) from Facebook. I don't want user interaction with login procedure where to inser user and pass. Is it possibile and how can be made? I understand the SDK is not useful for this scenario.
For now my script is:
require_once ('../php-graph-sdk-5.x/src/Facebook/autoload.php');
$fb = new \Facebook\Facebook([
'app_id' => 'myappid',
'app_secret' => 'mysecret',
'default_graph_version' => 'v2.10',]);
$helper = $fb->getRedirectLoginHelper();
$accessTokenEntity=$helper->getAccessToken();
try {
$accessTokenEntity=$helper->getAccessToken();
$response = $fb->get('/me', $accessTokenEntity->getValue());
}
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;
}
$me = $response->getGraphUser();
echo 'Logged in as ' . $me->getName();
Copied from gitub example but i think it s not good for my scenario.
And the output is
"Fatal error: Call to a member function getValue() on null in ..."
I'm integrating FB login on my website and for that I need the email of the user. Here's part of the code
$fb = new Facebook\Facebook([
'app_id' => 'app-id',
'app_secret' => 'secret',
'default_graph_version' => 'v2.8',
]);
$helper = $fb->getJavaScriptHelper();
try {
$accessToken = $helper->getAccessToken();
} 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();
}
if (isset($accessToken)) {
$fb->setDefaultAccessToken($accessToken);
try {
$requestProfile = $fb->get("/me?fields=name,email");
$profile = $requestProfile->getGraphNode()->asArray();
} 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();
}
print_r($profile);
}
What I'm getting back is name and id. Even the popup that requests users to grant permissions to the app doesnot ask for the email. And understandably it doesn't return it as well.
I've seen this and many other questions but none of the solutions have worked for me. What am I doing wrong?
You need to request permission to obtain the user email.
Please review https://developers.facebook.com/docs/php/howto/example_facebook_login to see how to request it (e.g.: permissions = ['email', 'user_likes', 'user_location', 'user_friends', 'user_birthday', 'user_hometown'];)
Refer to https://developers.facebook.com/docs/facebook-login/permissions for the permission reference and https://github.com/thephpleague/oauth2-facebook for more example.
Thanks to CBroe who pointed this out in the comments above, the permissions were supposed to be set in the javascript, not the php. It's because the php code is run when the authorization is complete and you have received all information you were supposed to.
To fix it, I changed
FB.login(function(response){
// take action based on response
});
To
FB.login(function(response){
// take action based on response
}, {scope: 'public_profile, email'});
And it works now. Thanks