Facebook php example: Fatal error - php

I am trying to make a php facebook app. Downloaded the Facebook php sdk and used the example.php (with my own appId and secret) to try to get a example app running. I can use the example app, but when I click login with facebook and and authenticate the app it throws this exception:
"Fatal error: Uncaught GraphMethodException: Unsupported get request. thrown in /var/www/facebook_test/base_facebook.php on line 1264"
Below is the code I am using.
Any guidance would be greatly appreciated.
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'myappid',
'secret' => 'mysecretid',
'cookie' => true,
));
// 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;
}
}
// 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');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Check the login status using OAuth 2.0 handled by the PHP SDK:
Check the login status
</div>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Edit:
This is the stack trace:
#0 /var/www/facebook_test/base_facebook.php(873):
BaseFacebook->throwAPIException(Array) #1 [internal function]:
BaseFacebook->_graph('/naitik') #2 /var/www/facebook_test/base_facebook.php(647):
call_user_func_array(Array, Array) #3 /var/www/facebook_test/index.php(66):
BaseFacebook->api('/naitik') #4 {main}

// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
Apparently it doesn't!

Related

Facebook app not retrieving user info

I just built an app with the Facebook PHP SDK and set it up. I am testing it and though I get the image to show up from my profile, the user info requested is not showing up. Here is the screenshot of the results
Here is the php code that I uploaded to the server. Note that the App ID and Secret have been removed from here.
<?php
require ('src/facebook.php');
$app_id = " "; //has been removed
$secret = " "; //has been removed
$app_url = "http://apps.facebook.com/instahomework/";
// 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) {
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 {
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk example 1 - user login and authorisation</title>
<style>
body { font-family: Helvetica, Verdana, Arial, sans-serif; }
a { text-decoration:none; }
a:hover { text-decoration:underline; }
</style>
</head>
<body>
<pre>
<?php print_r($_REQUEST); ?>
</pre>
<?php if ($user) { ?>
<h2>Welcome</h2>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><br/>
<?php echo $name; ?><br/>
<br/>
Gender : <?php echo $gender; ?><br/>
Locale : <?php echo $locale; ?><br/>
Username : <?php echo $username; ?><br/>
<?php } else { ?>
<strong><em>You have not granted this App permission to access your data. Redirecting to permissions request...</em></strong>
<?php } ?>
</body>
</html>
you should replace your HTML code with:
<h2>Welcome</h2>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture"><br/>
<?php echo $user_profile['name']; ?><br/>
<br/>
Gender : <?php echo $user_profile['gender']; ?><br/>
Locale : <?php echo $user_profile['locale']; ?><br/>
Username : <?php echo $user_profile['username']; ?><br/>
as the "user info" you want is requested by this line of code:
$user_profile = $facebook->api('/me');
and stored into the $user_profile array.
Another workaround (but not recommended) is using extract() to extract the data from facebook into your global variable scope, by adding this line of code:
extract($user_profile);

Get facebook user birthday with API

I'm trying to get user birthday with facebook API. I do know we need now to ask permission for this. I don't really know how it works
Just found this to add:
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array('req_perms' => 'email,read_stream')
);
}
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
and this is my whole code ( quite basic ):
<?php
require 'php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => '4539768XXXXX',
'secret' => 'fe86f3b0b34b3eXXXXXXXXXX',
'cookie' => true,
));
$signedRequest = $facebook->getSignedRequest();
$liked = $signedRequest["page"]["liked"];
// 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;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(
array('req_perms' => 'email,read_stream')
);
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<?php
if ($liked) {
header("Location:youlike.php");
} else {
echo "you don't like yet";
}
?>
<fb:login-button autologoutlink="true" perms="email,user_birthday,status_update,publish_stream"></fb:login-button>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login:
Login with Facebook
</div>
<?php endif ?>
<?php if ($user): ?>
<h3>Vous</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Personne connectée</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>Vous n'etes pas loggé!</em></strong>
<?php endif ?>
</body>
<script>
</script>
</html>
i would appreciate your help
thanks in advance!
You have a couple of problems in your code.
First, you aren't asking for the user_birthday permission when you create your $loginUrl. You should be requesting the same permissions for req_perms on line 33 as you do in the <fb:login-button> on line 68 of your code: email,user_birthday,status_update,publish_stream.
Once you copy over the permissions, you can remove line 68 of your code. The <fb:login-button> tag won't work without the JavaScript SDK.
Calling header() on line 61 after you've output text to the browser on lines 37-58 won't work. If you are going to redirect the browser, you can only do this before you've output text. Move this block of code before you output anything and it will work.
The white space between your ?> on line 36 and <!doctype> on line 39 will be output at the top of your HTML file. It cause the Facebook parser to choke. Some browsers don't like white space at the beginning of an HTML document either and will render your page incorrectly. Your code should really be ?><!doctype

Can't log in with facebooks's php example?

I have been trying to get this to work for a few days on and off and seem to get no where with it. what ever I try I get
You are not Connected. Public profile of Naitik
I am using a shared ssl does this have any to do with this?
I am guessing its an OAuth 2.0 that is giving me problems?
if I go to https://www.facebook.com/dialog/oauth?client_id=IDNUMBER&redirect_uri=https://apps.facebook.com/... I can see all my info
but if I just click the app from facebook's homepage it will say I am not logged in?
any ideas?
Thank you!
<?php
require 'src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '...',
'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;
}
}
// Login or logout url will be needed depending on current user state.
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Try adding a few more permissions like read_friendlist so that the API throws an exception on the statement $fb->api('/me') when the user is logged out as you want it to. For eg. set
$params = array('scope' => 'read_friendlists, friends_photos');
//$facebook->getLoginUrl($params);
//$facebook->getLogoutUrl($params);
// now use these login logout urls
You can also try adding $params = array('next'=> 'your-landing-page') to your facebook login request as $fb->loginUrl($params).

Facebook php connect

Im trying to connect to facebook using php-sdk. facebook login window appears when i click login, i fill my username and password and then it redirects to same page, saying not connected. I am new with facebook php. Help me
<?php
require_once("php-sdk/src/facebook.php");
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '---',
'secret' => '---',
));
// 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 {
$loginUrl = $facebook->getLoginUrl();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
Please place your appId and secret and follow the code below. You should also use the latest Facebook PHP SDK
$facebook = new Facebook(array(
'appId' => '111111111',// your appId here
'secret' => '1a1a1a1a1a1a1',// your app secret here
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
You need to provide certain information to the app you just created. The call back url is needed. Please check the information at the facebook end.

Can't include facebook inside my codeigniter project

I wanted to include the facebook login inside my application. This is the sample code which I was provided with:
<?php
require 'facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'my api goes here',
'secret' => 'my api secret goes here',
'cookie' => true,
));
// We may or may not have this data based on a $_GET or $_COOKIE based session.
//
// If we get a session here, it means we found a correctly signed session using
// the Application Secret only Facebook and the Application know. We dont know
// if it is still valid until we make an API call using the session. A session
// can become invalid if it has already expired (should not be getting the
// session back in this case) or if the user logged out of Facebook.
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<!--
We use the JS SDK to provide a richer user experience. For more info,
look here: http://github.com/facebook/connect-js
-->
<div id="fb-root"></div>
<script>
window.fbAsyncInit = function() {
FB.init({
appId : '<?php echo $facebook->getAppId(); ?>',
session : <?php echo json_encode($session); ?>, // don't refetch the session when PHP already has it
status : true, // check login status
cookie : true, // enable cookies to allow the server to access the session
xfbml : true // parse XFBML
});
// whenever the user logs in, we refresh the page
FB.Event.subscribe('auth.login', function() {
window.location.reload();
});
};
(function() {
var e = document.createElement('script');
e.src = document.location.protocol + '//connect.facebook.net/en_US/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
</script>
<h1>php-sdk</h1>
<?php if ($me): ?>
<a href="<?php echo $logoutUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/z2Y31/hash/cxrz4k7j.gif">
</a>
<?php else: ?>
<div>
Using JavaScript & XFBML: <fb:login-button></fb:login-button>
</div>
<div>
Without using JavaScript & XFBML:
<a href="<?php echo $loginUrl; ?>">
<img src="http://static.ak.fbcdn.net/rsrc.php/zB6N8/hash/4li2k73z.gif">
</a>
</div>
<?php endif ?>
<h3>Session</h3>
<?php if ($me): ?>
<pre><?php print_r($session); ?></pre>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $uid; ?>/picture">
<?php echo $me['name']; ?>
<?php echo $me['education'][0]['school']['name']; ?>
<h3>Your User Object</h3>
<pre><?php print_r($me); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Now - this works well but when I'm trying to include that in my code igniter view file it's not allowing me to login due to an error. In the code where it says 'my api goes here' and 'my api secret goes here' I'm obviously inserting my app data. Does any of you encountered similar issue? When I click login I'm getting this error: An error occurred with MY APP. Please try again later.
Can anyone help me with this one?
Have you tried any of these two CodeIgniter libraries for Facebook?
http://www.haughin.com/code/facebook/
http://brandonbeasley.com/blog/facebook-api-codeigniter/
UPDATE: Sadly these links are no longer valid.
My partner just made a library and indepth tut to help with this:
http://hitsend.ca/2010/10/facebook-connect-user-authentication-using-the-new-graph-api-in-codeigniter/
Let us know what you think. We needed it for our uses, and figured it was a common problem.
It uses the new graph api, not the old one. That solves A LOT of problems.
Let us know what you think.

Categories