I'm trying to use php to access and collect basic information on-
users name, gender, birthday, age_range etc.
but for some reason I can only get the name and gender to work and I get an Undefined index: with birthday and age_range.
Can someone help me figure this out? I am new to Facebook Graph API and trying to modify the given code they gave for logging. I think I don't need an access token yet because I'm trying to get the initial permissions when they log into the site.
<?php
include('src\facebook.php');
//$scope = 'username,age_range,birthday,gender';
$config = array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',
'fileUpload' => false,
'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
//$user_profile = $facebook->api('/me','GET');
$user_profile = $facebook->api('/me','GET', array('scope' => 'user_birthday,gender,age_range,name,likes'));
echo "Name: " . $user_profile['name'];
echo " Gender: " . $user_profile['gender'];
echo " DOB: " . $user_profile['birthday'];
echo " Age_Range: " . $user_profile['age_range'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
Also could someone explain to how to get user likes, I know I have to specify user permission using the Facebook->api(me/like) but then how do I access the array? Thanks for the help!
Please follow only the official docs to avoid such mistakes-
You are adding the permissions scope into the wrong place.
You are using the wrong permissions: gender,age_range,name,likes - these are not valid permissions!
Permissions that you require are: user_likes (for likes), user_birthday(for birthday); rest are the part of the basic(default) permissions only!
The age range field is also included in the public profile- for anyone who installs your app.
You have to add the scope parameter while using the login code: getLoginUrl().
$params = array(
'scope' => 'user_likes, user_birthday'
);
$loginUrl = $facebook->getLoginUrl($params);
After the user successfully grants the permissions, you'll get the required things!
Related
Hi i need help with a small error i received
Warning: Illegal string offset 'name' in C:\xampp\htdocs\facebooktest.php on line 41
You are logged into centralrp using your facebook account! [1] when im tring to show the name
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('php-sdk/facebook.php');
$config = array(
'appId' => '*',
'secret' => '*',
'allowSignedRequest' => false // optional but should be set to false for noncanvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
if (!isset($_SESSION['user']['facebookposted']))
{
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'app.com',
'message' => 'Just logged into App with Facebook.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
$_SESSION['user']['facebookposted'] = true;
//ADD THE LOGIN STUFF HERE LATER
}
else
{
$_SESSON['user']['loggedin'] = true;
echo 'You are logged into centralrp using your facebook account! ['.$user_id['name'].']';
}
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
There is no name key in $user_id because $user_id is not an array.
if you need the name, get it like this:
try {
$user_info = $facebook->api('/me/');
echo '<pre>Name: ' . $user_info['name'] . '</pre>';
} catch(FacebookApiException $e) {
echo 'There was an error';
}
I used the below code to get user's facebook details. Now when I run that, a page comes saying "Please login" where login is an anchor tag. When I click on login, another page comes saying "Invalid App ID: YOUR_APP_ID". When I do all this, i m already logged in to facebook. So why m I not getting details?
<?php
include 'facebook-php-sdk/src/facebook.php';
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'allowSignedRequest' => false // optional but should be set to false for non-canvas
apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
I think you need to replace YOUR_APP_ID and YOUR_APP_SECRET with the values you find on your Facebook developer page after you created an app.
Where can I find my Facebook application id and secret key?
Read the first question and click on the "here" link.
I have created a facebook app using PHP-SDK which posts a message on user's feed and it will print app access token on the canvas page. It does not opens any Dialog box for permissions access to app, also after opening the app it does not do anything. It just show "Please Login" where login isa button having some link which too isnot working. Help me!
<?php
// Remember to copy files from the SDK's src/ directory to a
// directory in your application on the server, such as php-sdk/
require_once('src/facebook.php');
$config = array(
'appId' => 'myappidhere',
'secret' => 'mysecrete,
'allowSignedRequest' => false // optional but should be set to false for non-canvas apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.facebook.com',
'message' => 'Posting with the PHP SDK!Learning PHP and Facebook API :D :D'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
$token = $facebook->getAccessToken();
echo 'test';
echo $token;
// Give the user a logout link
echo '<br />logout';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
Is there any special settings which i have to do in my APP dashboard on facebook?
You missing single quotes at:
'secret' => 'mysecrete',
Is this a typo only in this post, not in your code? And is any arrors displayed? Use var_dump for dumping url:
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
var_dump($login_url);
The FIRST time my index.php loads I get a value of 0 when calling:
$facebook->getUser();//returning 0 instead of id on first page load
The call works fine on any subsequent page load.
This is my code:
$facebook = new Facebook(array(
'appId' => $FB_APP_ID,
'secret' => $FB_APP_SECRET
));
$user_id = $facebook->getUser();//Returns 0 on first load.
Has anyone else experienced this? Maybe I should automatically reload the page if $facebook->getUser(); does not return a valid user?
May be it is not authenticating it for very first time, You can do some thing like this:
<?php
$facebook = new Facebook(array(
'appId' => xxx,
'secret' => xxx
));
$user_id = $facebook->getUser();
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$me = $facebook->api('/me','GET');
echo "Name: " . $me['name'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll redirect and
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
} else {
// No user, redirect for the user to login
$login_url = $facebook->getLoginUrl();
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
EDIT:
If error persists, try with this edit, just change your this line:
$me = $facebook->api('/me','GET');
to this:
$me= $facebook->api('/'.$user_id, 'GET');
Hope this will solve your problem.
I'm trying to post to all my app user wall
& and i use this code to post to only one user : ==>>
this code to post in only one wall
<?
$config = array(
'appId' => 'xxxxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
try {
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.example.com',
'message' => 'Posting with the PHP SDK!'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
// Give the user a logout link
echo '<br />logout';
} else {
// No user, so print a link for the user to login
// To post to a user's wall, we need publish_stream permission
// We'll use the current URL as the redirect_uri, so we don't
// need to specify it here.
$login_url = $facebook->getLoginUrl( array( 'scope' => 'publish_stream' ) );
echo 'Please login.';
}
?>
</body>
</html>
How to publish a post to all my app users ?!!
please advice !
Thanks
Get all the app users-
With FQL, just do :
$fql="SELECT uid FROM user
WHERE uid IN (SELECT uid2 FROM friend WHERE uid1 = ".$me['id'].")
AND is_app_user = 1";
Loop through each user and use feed