My facebook app was working perfectly fine till this morning. The only change I made was that I changed the url to a local server. Later when I changed the url back to my deployment server, it gets stuck in an infinite loop with state and code variables constantly getting refreshed every second.
I then created another app on my deployment server and it is doing the same. Interestingly, the same code runs fine on my staging machine. My deployment server is from awardspace. Any idea what to do.
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'app id',
'secret' => 'secret',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
if($user==725363641){
try {
echo "something" , "</br>";
}
catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}// second if
else{
echo "You are not authorized to use this application", "<br/>";
}
}//outer if
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'user_status,user_likes,manage_pages,publish_stream'));
}
?>
<?php if ($user): ?>
Logout or Return to Main Page
***<?php else: echo "<script type='text/javascript'>top.location.href = '$loginUrl'; </script>";
exit;***
?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
Here is the very basic code which isnt running. Actually I tested it again and the part in ** is giving the problem. IF i take it out I see my page and html being displayed but when i put it in this infinite loop problem occurs. However, putting it back or taking it out, nothing is making the app run properly
thanks.
Try updating the facebook source file and use the latest php-sdk provided by facebook.
Related
I have just started mingling with facebook's php sdk, but to no avail. Although php works fine for all the other stuff on my ubuntu instance, facebook's php sdk doesn't (i.e. php code is not executed). All dependencies (only curl and json) are enabled and working.
I tried to look into /var/log/apache2/error.log but it seems that the file is empty - but maybe I'm not looking in the correct place.
So my question is: what would be the steps for me to debug this problem! Thanks for helping a noob!
Here is the boilerplate index.php code for from facebook:
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
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;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
When I run this code, and my cookies have been cleared and I am not logged into facebook. It directs me to facebook and I log in but when it brings me back to my page it is the same, where as it should be show me profile pic and so on...
I seem to have narrowed down the problem of the return statement of the $user because it returns a 0. I have stared at this code for a long time and I havent found what I am doing wrong.
What do i need to change to get it so that When i return from logging in from facebook it will show the profile pic and so on...
<?php
require_once 'libs/facebook.php';
require 'connections/connection.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxx',
));
// Get User ID
$user = $facebook->getUser();
echo $user;
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
echo $user;
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
//$logoutUrl = $facebook->getLogoutUrl();
$logoutUrl = $facebook->getLogoutUrl(array('next' => ($fbconfig['baseurl'] . 'logout.php')));
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_about_me',
'scope' => 'read_friendlists'
));
}
?>
Not to worry my friend, you are not doing anything wrong. I'm having same issue with my DEMO application which was running alright till yesterday.
I'm sure you are having issue with getting access token as well. I think issue is with PHP SDK only.So probably will be fixed in some time.
SOLUTION : This worked for me after trying for many solutions for this issue.
In base_facebook.php file, find the makeRequest() method and check for following Line.
$opts = self::$CURL_OPTS;
Immediately following it, add this line
$opts[CURLOPT_SSL_VERIFYPEER] = false;
More details can be found here - http://net.tutsplus.com/tutorials/php/how-to-authenticate-your-users-with-facebook-connect/
I am trying to add Facebook login to my web site.
What I have done?
Created APP on Facebook as Website with Facebook Login
Sandbox mode is off
APP is set to work on localhost.
What is the problem?
I keep getting $user variable's value as 0
example.php in Facebook PHP SDK works just fine!
I can see my APP when I visit Facebook -> Privacy Settings -> Apps. No matter if I login with my login.php or example.php of Facebook PHP SDK, the app seems to be added just the same. However while example.php can retrieve data from facebook, login.php can't.
I copy and paste the codes from example.php to my login.php page (please check the code below).
Here is my code (login.php);
require_once('system/api/facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => '123456',
'secret' => 'abcde12345',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
if (!$user) echo 'Login';
echo '<pre>';
var_dump($user);
echo '</pre>';
Can someone please tell me what is the difference between my login.php and example.php? I also Googled and searched here without finding any info which was helpful.
I am trying to create a competition entry which checks to see if the user likes a page before entering. If they do, it will fetch their name, email and birthday for the competition entry details.
For some reason however, I cannot get my PHP code to work.
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXX',
'secret' => 'XXXX',
'cooke' => true
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$me = $facebook->api('/me?fields=likes.target_id(215359398497970),birthday,first_name,last_name,email');
if($me){
$first_name = $me['first_name'];
$last_name = $me['last_name'];
$email = $me['email'];
$dob = $me['birthday'];
die($me);
} else {
$likes = false;
}
} catch (FacebookApiException $e) {
die($e);
error_log($e);
$user = null;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
//echo $loginUrl;
}
?>
For some reason, it gets stuck in a redirect loop because the loginURL doesn't seem to log my user in.
For anyone who's still looking. This is how I fixed my problem. I had to ensure that theis code was executed BEFORE any other code so I plonked it to the very top of my php file and it worked. I think the problem was that the headers had already been sent but I was unaware because my PHP warnings / errors are not enabled to show up.
I've encountered the same problem. I used an example project, and couldn't figure out why it keeps returning 0. tried nearly all related links here but none solved it.
The solution was to update Facebook's SDK files in the example project.
Simple and easily missed,
I started playing around with the fb api a couple of days ago. I am using the fb php-sdk to authenticate users with facebook on my site. I use the following piece of code which works, but intermittently and I cant seem to figure out why.
<?php
session_start();
require './library/facebook.php';
include './library/fb_keys.php';
// Create app instance
$facebook = new Facebook(array(
'appId' => YOUR_APP_ID,
'secret' => YOUR_APP_SECRET,
));
// Get User ID
$user = $facebook->getUser();
//Check Access token
if ($user) {
try {
// Proceed with logged in user.
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
//error_log($e);
$user = null;
}
}
// Login or logout based on user state
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array('scope' => 'status_update, publish_stream', 'redirect_uri' => 'http://webaddress.com/web/'));
}
?>
This is really straightforward piece of code that I got from the https://github.com/facebook/php-sdk/.
I usually can't log in when I clear all my cookies and/or session is cleared. but once I log on to the facebook.com site all seems to work again. So it indicates that I am missing something with regard to setting the session or cookies.
Any help will be appreciated.
I guess by not working you mean that user isn't redirected to the login page?
You do get the loginUrl but you're not redirecting user there.
This can be easly done with adding the following code after $loginUrl line:
print '<script language="javascript" type="text/javascript"> top.location.href="'. $loginUrl .'"; </script>';
If I'm wrong please tell me exactly what you mean under "can't log in", what stops you etc.