Facebook API sometimes doesn't load - facebook

I'm having problem when using
$facebook->api('/me','GET');
When loading the page for the first time, nothing happen. I have to reload the page again to make the script work. Not sure where the problem is.
require_once('src/facebook.php');
$app_id = "APP_ID";
$app_secret = "APP_SECRET";
// Init facebook api.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true,
));
//Facebook Authentication part
$user_id = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'publish_stream, user_likes'
)
);
if (!$user_id) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
$user_profile = $facebook->api('/me','GET');
$user_gender = $user_profile['gender'];
if($user_gender == 'male'){
echo "you are male";
} else {
if ($user_gender == 'female'){
echo "you are female";
} else{
echo "gender not specified";
}
}
I tried take the $facebook->api('/me','GET'); out and echo $user_id, for example, it works.

Related

Strange behavior in Facebook login with PHP SDK

I'm learning about Facebook apps programming and I've wroted the next code that works OK, but if I comment the indicated line in the "do-login.php" file, then when I call getUser() on "index.php" will return 0.
I'm curious about that and I think maybe I'am doing something bad.
At this point, I think it wouldn't be necessary to create a Facebook object in the "do-login.php" file.
Finally, note that the link on "index.php" file points to the Heroku URL, and the link on "do-login.php" file points to the "apps.facebook.com" URL... this is the only way I found to the code works.
Any advice, please ? I'm confussed with this and I spent a day and half to get working this
Thanks!
index.php
<?php
require_once("facebook.php");
$facebook = new Facebook(array(
'appId' => THE_APP_ID,
'secret' => THE_APP_SECRET,
'fileUpload' => false,
'cookie' => true,
'trustForwarded' => true,
'allowSignedRequest' => false
));
$user = $facebook->getUser();
if( $user )
{
try
{
$user_info = $facebook->api(THE_USER_ID?fields=id,name,first_name,last_name,email&access_token='.$facebook->getAccessToken());
echo "getUser(): ".$user."<br/>";
echo "<b>Name</b>: ".$user_info['name'];
}
catch( FacebookApiException $e )
{
echo $e->getMessage();
}
}
else
{
$facebook_login_url = $facebook->getLoginUrl(array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'https://MY-APP.herokuapp.com/do-login.php'
));
echo "<p><a href='".$facebook_login_url."' target='_top'>Login</a></p>";
}
?>
do-login.php
<?php
require_once("facebook.php");
if( isset($_GET['code']) )
{
echo "<p><b>¡¡ Se autorizó la app !!</b></p>";
$facebook = new Facebook(array(
'appId' => THE_APP_ID,
'secret' => THE_APP_SECRET,
'fileUpload' => false,
'cookie' => true,
'trustForwarded' => true,
'allowSignedRequest' => false
));
// PROBLEM IS that if I comment this line, getUser() will return 0 when called in index.php
echo $facebook->getUser();
echo "<p><a href='https://apps.facebook.com/MY-APP/index.php' >Go back</a></p>";
}
else
{
echo "Error: ".$_GET['error']."<br />";
echo "Description: ".$_GET['error_description'];
}
?>

facebook app wont ask for extended permissions when validating

Have been trying for weeks, but my App won't ask for extended permissions, obviously that means that it wont POST to the timeline afterwards either.
require_once('/wp-content/php-sdk/src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxx',
'secret' => 'xxxxxxx',
));
$user = $facebook->getUser();
if ($userID) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$ret_obj = $facebook->api('/me/feed', 'POST',
array(
'link' => 'www.theaandrdepartment.com/raw',
'message' => 'I just helped an Aussie band make it to
radio play! By voting, you can too.'
));
echo '<pre>Post ID: ' . $ret_obj['id'] . '</pre>';
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else { $login_url = $facebook->getLoginUrl(array(
'scope' => 'publish_stream'
));
header("Location: ".$login_url);
}
If anyone could help that would amazing.
I copied most of this code straight from the Facebook example, but to no avail.

Why can't I redirect to Facebook's getLoginUrl()?

<?php
$fb = new Facebook(array(
'appId' => $fb_config['app_id'],
'secret' => $fb_config['secret'],
));
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect',
);
$fb_login_url = $fb->getLoginUrl($params);
$this->redirect($fb_login_url);
$user = $fb->getUser();
print_r($user); // returns 0
?>
In the example above, if I redirect to the getLoginUrl result, the getUser() method returns zero.
Is there a reason why this is happening?
Replace Facebook PHP SDK path, appId, secret and redirect_uri with your app settings and Try:
require_once dirname(__FILE__) . '/facebook/facebook.php'; // You need Facebook PHP SDK
$facebook = new Facebook(array(
'appId' => $fb_config['app_id'], // Your Facebook app id
'secret' => $fb_config['secret'], // Your Facebook app secret
'cookie' => true
));
$fb_user_id = $facebook->getUser();
if ($fb_user_id) {
try {
$fb_user_profile = $facebook->api($fb_user_id);
var_dump($fb_user_profile);
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
$params = array(
'scope' => 'email, read_stream, user_interests, user_likes, user_location, user_status',
'redirect_uri' => 'http://myurl/facebook_connect', // Replace with your app url
);
$facebook_login_url = $facebook->getLoginUrl($params);
echo '<script>top.location="' . $facebook_login_url . '";</script>';
exit();
}

Facebook. After approve permission wrong redirect link

so I have a little problem, when first time user use my app It is redirected to:
http://www.facebook.com/MY_APP.../?state=f5c913239670dc934fae1274a81fa538&code=AQAmY5k8gRRt7woNKmpc_6z2eHgT-2B3prjNEDYFma1KqpQDKob-CS_bWfqd0qPkQD-tGoVdKUK8twGZRnLhoiotpZ3SlBeo3uusfmMyWDYStv-D79RvQEP8m9M8S8GYlbN3x1eLEd4D3RajWokTNAhRP8a1VB9VSKAb35xXFzg3PkheRKHqjr-nex5jcjM7Nv2B_JEV1w8AzM2LJg2M9kAb#_=_
And I get error: page not found
Durring permission approvig my link is: https://www.facebook.com/dialog/oauth?client_id=456080124457246&redirect_uri=http%3A%2F%2Fpadekime.wu.lt%2Fplaukai%2Findex.php&state=2c46a86dae43af0467da756bc3e52779&scope=publish_stream%2Cphoto_upload
I added redirect link and now It go to:
http://www.padekime.wu.lt/plaukai/kontekstas/?state=5abfd521df3c68ae55f26ad51a704743&code=AQDMmpZC-_Yxatq4xuAE60Tl2qW4pXCvDeghrbMGGu3Lg1X8zqrk-lvxRc3VOoYvgoLvATJhcsyykZnPoM7XI2sXih-nJhKrjx-HS3GFapELmC4KvX4KyN-VU-znHopkA-q_zcuIffJ0tY79CXPn7mo05BpxfvPntXaUAe7ymLf1p8Kg29eERaP5nw1dChbwwZ13FNY7BXD2ymAsFHPNH5zw#_=_
Possible to make that after permission approved go normally to my app http://www.facebook.com/Padekime/app_456080124457246 without /?state=..... ?
My code now looks like:
<?php
require_once('images/Facebook.php');
$facebook = new Facebook(array(
'appId' => '456080124457246',
'secret' => 'e2956a6e1de8791363faedb1ef44c408',
));
# Get User ID
$user = $facebook->getUser();
if ($user) {
try {
$redirectUri = 'http://www.padekime.wu.lt/plaukai/kontekstas/';
# Photo Caption
$photoCaption = 'Patarimų plaukams sužinojau čia http://goo.gl/otwhf';
# Absolute Path to your image.
$imageUrl = 'http://padekime.wu.lt/plaukai/images/PlaukaiNeuzvedus.jpg'; // Example URL
# Post Data for Photos API
$post_data = array(
'message' => $photoCaption,
'url' => $imageUrl,
'redirect_uri' => $redirectUri
);
$apiResponse = $facebook->api('/me/photos', 'POST', $post_data);
} catch (FacebookApiException $e) {
$user = null;
error_log($e);
}
} else {
$redirectUri = 'http://www.padekime.wu.lt/plaukai/kontekstas/';
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload',
'redirect_uri' => $redirectUri
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
}
?>
As I understand problem is somewhere here, but I cant solve it.
$loginUrl = $facebook->getLoginUrl( array(
'scope' => 'publish_stream,photo_upload'
));
echo("<script>top.location.href = '" . $loginUrl . "';</script>");
So if you know something about this, please help me, thank you very much.
SOLUTION:
I added ?ref=ts to $redirectUri:
$redirectUri = 'http://www.facebook.com/Padekime/app_456080124457246?ref=ts';
SOLUTION:
I added ?ref=ts to $redirectUri:
$redirectUri = 'http://www.facebook.com/Padekime/app_456080124457246?ref=ts';

How do I get Facebook Application Login to work

I just started making an application for Facebook, however I ran into problem early on. The first step I want people to do is to give permission to access their profile. All over the web are examples of how to do this with:
$user_id = $facebook->require_login();
However, this is the way it works using the Old PHP API. I have downloaded and installed the new one in my application folder and it is not working anymore.
My question is (and i really have been searching for an answer for a long time) what is the code to do this with the new API?
(and related question: is it better to use the old API, or learn to work with the new one when I am just starting making apps right now)
I have this code now;
<?php
// Awesome Facebook Application
//
// Name: -
//
require_once 'facebook-php-sdk/src/facebook.php';
$app_id = "-";
$app_secret = "-";
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
'cookie' => true
));
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday, user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
echo "<p>hello, <fb:name uid=\"$user_id\" useyou=\"false\" />!</p>";
?>
Download and use new code from github.
How to get user permission?
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,publish_stream,sms,status_update,user_location'
));
Or
<fb:login-button perms="email,user_birthday,publish_stream,sms,status_update,user_location"></fb:login-button>
Example how to login
Update
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday, user_location,user_work_history'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}