getUser() returns 0 - php

I have this code in php that used to work, but now it doesn't, and even thoug it sounds like a lie, I did not touch anything!
$facebook = new Facebook(array(
'appId' => 'myApp',
'secret' => 'mySecret',
'cookie' => true ,
'scope' => 'read_stream','manage_pages'
));
$user = $facebook->getUser();
if ($user!=0) {
doStaff;
}else{
$login_url = $facebook->getLoginUrl($params = array('scope' => "read_stream"));
echo ("<script> top.location.href='".$login_url."'</script>");
}
The problem now is that gets stack in an infinite loop.
Any help would be really appreciate.

You are probably using the deprecated Facebook PHP SDK.
Try to get the latest Facebook PHP SDK

Related

facebook send notification to users

i want to send notification who access my app.i use following code.
but it not work.i was goggling about it.but every example looks like my one.but it not work for me.
here is my code
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'myappsecret',
));
$app_id='myappid';
$app_secret='myappsecret';
$app_access_token=$app_id.'|'.$app_secret;
$user_id='359211054272153';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$response = $facebook->api( '/359211054272153/notifications', 'POST/v2.2/', array(
'template' => 'You have received a new message.',
'href' => 'RELATIVE URL',
'access_token' => $app_access_token
) );
i am used old version of Facebook sdk it want to real user id.
but currently
Facebook gives app scoped userid. thats the error.
using new sdk v4 it not problem.

Facebook $facebook->getUser() is not working

We are trying to get user information from getUser() method of facebook using php sdk (3.1.1) but it returns 0. what would be the cause?
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
$facebook->getUser();
The latest SDK is v.3.2.2 https://github.com/facebook/facebook-php-sdk/ you should use that instead and also be using a login handling logic with getLoginUrl()

Facebook PHP SDK Problems

I have problems with facebook SDK, I use this code:
$facebook = new Facebook(array(
'appId' => 'MY_API_KEY',
'secret' => 'MY_API_SECRET',
'cookie' => true,
));
$fql = "My fql query";
$response = $facebook->api(array(
'method' => 'fql.query',
'query' =>$fql,
));
print_r($response);
Works fine, but after a while returns an error like: "Uncaught CurlException: 3: No URL set!"
That problem appear for few minute (5-10 minutes) and after that works again. The big problem is that error appear few times at hour, somebody know how can I fix that problem?
I think you have set the default responseURL .
May be this is cause
example :- php codes
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'https://www.myapp.com/post_login_page'
);
$loginUrl = $facebook->getLoginUrl($params);
hope this solves the problem .

Facebook not sending get parameter after login success

I am using php sdk for my facebook application.
I am using following code for login url
if (isset($username)) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => 'http://www.mysite.com/redirect.php?username=' . $username
)
My problem is that after login when facebook redirects page to redirect.php but it removes ?username=ursername.
I need the username as get parameter after login.
How can i do this. Please suggest me.
Thanks in advance.
You cannot pass username to the redirect_uri. It's not really meant for this, but there is a state parameter you can pass that gets returned to you.
See the documentation:
https://developers.facebook.com/docs/reference/dialogs/oauth/
define('REDIRECT_URI',"YOUR-REDIRECT-URL-HERE");
$user = null;
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => true
));
if (isset($username)) {
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => $scope,
'redirect_uri' => REDIRECT_URI
)

Schedule posts on Facebook page with PHP SDK

I am trying to schedule wall posts to be added to the Facebook business page in the future.
As far as I can see Facebook does not recommend to use "offline_access" anymore.
How would you do that?
This is my code so far. It works if I am already logged into Facebook.
EDIT: Naturally I will create some code that check the schedule that I pull from the database. And use a cron job to regulary check that schedule.
require_once('src/facebook.php');
$config = array(
'appId' => 'xxxxxxx',
'secret' => 'xxxxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$page_info = $facebook->api("/PAGE_ID?fields=access_token");
try {
$ret_obj = $facebook->api('/PAGE_ID/feed', 'POST',
array (
'link' => 'http://www.example.com/',
'message' => 'This is a test',
'access_token' => $page_info['access_token']
));
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'publish_stream'
));
print_r($e->getType());
print_r($e->getMessage());
}
You have to extend access_token periodically now. All other code should be working fine as previously

Categories