I try to get my user_likes. I got an access token in the Graph API Explorer for user_likes. When I put hardly my PAGE_ID_WANT_TO_CHECK and my ACCESS_TOKEN_USER_LIKES in the url bar, my datas are good shown in my browser. But, in my code when I make the test if my $likes are empty or not, it still say it's empty.
var_dump($likes) is just empty.
Would like to know why and how to have a full array ?
Here my code.
Thank you.
require_once('fb-sdk/facebook.php');
require_once('config.php');
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
if ($user_id) {
try {
$likes = $facebook->api('/me/likes/PAGE_ID_WANT_TO_CHECK?access_token=ACCESS_TOKEN_USER_LIKES');
if( !empty($likes['data']) ){
echo "I LIKE!!!!!!";
}else{
echo "not a fan!";
}
} catch (FacebookApiException $e) {
error_log($e);
}
}
var_dump($likes['data']);
//During the login process
$login_url = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
Related
My PHP Facebook SDK is not working and always returns $user=0 Why?
It is my first time to deal with facebook sdk
my code:
require './facebook-php/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxx'
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if($user) {
$logoutUrl = $facebook->getLogoutUrl(); echo 'there is a user';
} else {
$loginUrl = $facebook->getLoginUrl();
echo 'there is noo user <a href='.$loginUrl.'>login</a>';
}
below is the url return code :
http://localhost/e-commerce/testit.php?code=AQA8DckDMqNarptbCUIfAmn2tIyn1mjVBmHTjOMaQ4GqsFW_6vqemGW35TC07Kxq7kuB4QkKibHJ2-m9GntIx0biWEiR_FUuozsONvgc_tNmtGURpcm78DnIE26vT6hbjz10eZECuh-GLec9tGplhLrDNbLO-aX2Hv2Mu-8CQhAdiFQ6AYW3Nt09a8Rzahbn35Jz0cNyB_G7Ksjorq3iSs6Bpa6Tk0d7Vx0A0f1Mg77PgD6IuN_grjkNHDWmb_ywoqW48IqoxJUhnRmL3qi1G-CT7ft_f77rRr-VP-yQ-LTkdNja0rBzoFnFGccMuQ86u8tk7nfnx5qrSLBpHLEIBqQT&state=84ff9f36e8de53d13c96bcf7cb2da3b3#=
I have uploaded Facebook setting image
Facebook settings images
I'm using the Facebook API connected script written in PHP (provided by Facebook). Everything works, it generates the login URL and redirects me back to the website when I'm logged in.
However, the $user variable seems to be undefined.
Have anyone experienced a similar problem? In the Facebook Apps statistics page I can see that the app has been used.
Update - The code:
<?php
$app_id = "xxxxxxxxxxxxxx";
$app_secret = "xxxxxxxxxxxxx";
$site_url = "http://xxx";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret
));
if($user = $facebook->getUser())
{
echo 'ok';
}
else
{
}
if($user){
//==================== Single query method ======================================
try{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
//==================== Single query method ends =================================
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,user_birthday,user_about_me',
'redirect_uri' => $site_url . '/auth_complete.php',
));
}
if($user){
// Proceed knowing you have a logged in user who has a valid session.
//========= Batch requests over the Facebook Graph API using the PHP-SDK ========
// Save your method calls into an array
$queries = array(
array('method' => 'GET', 'relative_url' => '/'.$user),
array('method' => 'GET', 'relative_url' => '/'.$user.'/home?limit=50'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/friends'),
array('method' => 'GET', 'relative_url' => '/'.$user.'/photos?limit=6'),
);
// POST your queries to the batch endpoint on the graph.
try{
$batchResponse = $facebook->api('?batch='.json_encode($queries), 'POST');
}catch(Exception $o){
error_log($o);
}
//Return values are indexed in order of the original array, content is in ['body'] as a JSON
//string. Decode for use as a PHP array.
$user_info = json_decode($batchResponse[0]['body'], TRUE);
$feed = json_decode($batchResponse[1]['body'], TRUE);
$friends_list = json_decode($batchResponse[2]['body'], TRUE);
$photos = json_decode($batchResponse[3]['body'], TRUE);
//========= Batch requests over the Facebook Graph API using the PHP-SDK ends =====
}
?>
I'm not entirely sure why it worked, but I redirected the user to a page where the Javascript SDK is included (http://developers.facebook.com/docs/reference/javascript/), and now it's working!
The user have not authorized the app yet. Try something like this-
$user_id = $facebook->getUser();
echo $uid;
if($user_id){
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user_id = null;
}
}else{
$login_url = $facebook->getLoginUrl();
echo("<br>login url=".$login_url);
};
An FB app of ours that previously worked for months is now experiencing the issue you speak of. $fb_user is now being returned undefined. =(
Update your SDK, sounds like you are hitting a certificate issue from a few weeks back.
You can find the latest version at https://github.com/facebook/facebook-php-sdk/
require_once($_SERVER['DOCUMENT_ROOT'] . '/facebook-php-sdk/facebook.php');
$facebook = new Facebook(array(
'appId' => FB_MAIN_APP_API_ID,
'secret' => FB_MAIN_APP_SECRET
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
Above is the code I'm using to get the Facebook details for my application. In my localhost this is working fine and it gives my Facebook email, user id. After I upload this in to live server this function is not working and it's not giving my Facebook details. I checked on the Facebook app id and it is the correct one.
I cannot figure out why it's not working in live site since it is working in my localhost. Please help me with this. Thank you.
I think problem is in your app which your are using on server. So try the same app which you are using for your localhost and in login and logout url instead of localhost use $_SERVER['HTTP_HOST'].
Example :
$session->fb_logout = $facebook->getLogoutUrl(array('next' => "http://" . $_SERVER['HTTP_HOST'] . "/path/to/logout"));
First check that what $user_profile = $facebook->api('/me'); showing. Just print_r the variable $user_profile.
Ex :
$user_profile = $facebook->api('/me');
print_r($user_profile);
exit;
if it showing blank then surely it will go in catch block. Then write following in catch block.
print_r($e);
exit;
For geting the user id, user need to grant permission
$user_id = $facebook->getUser();
echo $uid;
if($user_id){
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user_id = null;
}
}else{
$login_url = $facebook->getLoginUrl();
echo("<br>login url=".$login_url);
};
After the user grant permission check the login_url, you may access user id.
Try this
Have you checked you have the correct URL in the configuration of your app in Facebook?
I have this piece of code to my FB apps and work properly, could you try it?
$session = $this->facebook->getUser();
if ($session) {
$logoutUrl = $this->facebook->getLogoutUrl();
try {
$data['uid'] = $this->facebook->getUser();
$me = $this->facebook->api($session);
$data['me'] = $me;
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$loginUrl = $this->facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_about_me, email',
'redirect_uri' => 'YOUR URL',
'canvas' => 1,
'fbconnect' => 0,
'display' => 'page',
));
}
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$this->load->view('MY VIEW', $data);
i am using basic facebook php sdk to login users to my page via facebook.
facebook.php, base_facebook.php and:
$facebook = new Facebook(array(
'appId' => '',
'secret' => '',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if (!$user) {
header("Location: /");
} else {
$loginUrl = $facebook->getLogoutUrl();
and i want to allow my users to post (or skip) something to their wall's when logging in. like that:
http://developers.facebook.com/attachment/web_dialog.png
any ideas?
to post to a user's wall you'd do something like this:
if (isset($_POST['status'])){
try {
$post = stripslashes($_POST['status']);
$statusUpdate = $facebook->api('/<fb_user_id>/feed?access_token='.$session['access_token'], 'post', array('message'=> strip_tags($post), 'cb' => ''));
} catch (FacebookApiException $e) {
echo "<pre>";
echo $e;
echo "</p>";
}
}
You can style the form that posts to that script to match that in the png file.
hope that is what you are looking for and helps you.
I am using Codeignitor .
I want to post to facebook through my application.
the problem is it always returns you are not logged in.
here what i tried ..
function postTofb()
{
require_once("pocket/fb/facebook.php");
$fbconfig = array();
$fbconfig['appId'] = 'app_id';
$fbconfig['secret'] = 'app_secret';
$fbconfig['baseurl'] = "site_name";
$facebook = new Facebook($fbconfig);
$user = $facebook->getUser();
if($user)
{
try {
$user_profile = $facebook->api('/me');
$ID = $user_profile['id'];
echo $ID;
}
catch (FacebookApiException $e)
{
echo $e;
}
}
else
{
//else starts
$loginUrl = $facebook->getLoginUrl(
array(
'scope'=> 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri'=>$fbconfig['baseurl']
)
);
$data["status"] = FALSE;
$data["url"] = $loginUrl;
echo json_encode($data);
//else ends
}
}
now i am calling the function through ajax...but it always response with FALSE...i mean the else part.
here what i am getting
{"status":false,"url":"https:\/\/www.facebook.com\/dialog\/oauth?client_id=281255791959236&redirect_uri=http%3A%2F%2Fwww.MY_SITE_NAME.com%2F&state=3d19a96f48022344222f518609f30537&scope=email%2Coffline_access%2Cpublish_stream%2Cuser_birthday%2Cuser_location%2Cuser_work_history%2Cuser_about_me%2Cuser_hometown"}
looks like i missed some points...please help me..
Thank you.
Offline access is no longer supported by facebook, only thing you can do is generate a token with max 60 days.
The problem was associated with LoginUrl
I removed some of the permission parameters and it worked.
loginUrl = $facebook->getLoginUrl(array("scope")=>"publish_stream");
Thank you.