I am trying to get an access token using the latest Facebook SDK for PHP. To my knowledge I am doing everything correct, but when it redirects to facebook.com/oauth/dialog, it just has a white screen. My code:
<div class="authorize_btn" style="float:right; margin-top:-35px; padding-right:10px">
<?php
include ('facebook/facebook.php');
$facebook = new Facebook(array( 'appId' => 'xxxxx',
'secret' => 'xxxx',
'cookie' => true ));
$session = $facebook->getUser();
$me = null;
if ($session) {
try {
$me = $facebook->api('/me');
}
catch (FacebookApiException $e) {
echo $e->getMessage();
}
}
if ($me) {
$logout = $facebook->getLogoutUrl();
echo "<button class='btn btn-primary'></button";
} else {
$login = $facebook->getLoginUrl();
echo "<a href='$login'>Authorize</a>";
}
?>
</div>
Get the latest Facebook SDK for PHP - the one you are using is about 2 years old and not supported .
https://developers.facebook.com/docs/php/gettingstarted/4.0.0
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 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 the following code and it's v strange - when i log out, if i try to actually go back into facebook i cannot but my code below does not register that i'm logged out and will show my facebook user id still
include '/home/php_/lib/fb_libs/facebook.php';
$facebook = new Facebook(array(
'appId' => '123456789',
'secret' => 'abcdefghilk',
'cookie' => true
));
$user = $facebook->getUser();
echo "user id is $user";
if ($user){
try { $user_profile = $facebook->api('/me'); }
catch (FacebookApiException $e) { echo $e->getMessage(); }
}
if ($user)
{
$logoutURL = $facebook->getLogoutURL();
echo "<a href='$logoutURL'>Log out </a>";
}
else
{
$loginURL = $facebook->getLoginURL(array(
'scope' => 'publish_stream,read_friendslists'
));
echo "<a href='$loginURL'>Log in </a>";
}
I understand there are other methods e.g. FB.getLoginStatus() so this is not a duplicate question but why, when this is on github facebook SDK, does it not work?
I think your cookie is causing the problem try not setting the cookie.
I am trying to get if user is logined in facebook, if yes then I need to store their profile pic url, name in my db, so that users can comment in my web application.
I cant do this, I created a facebook application for website and using the following code for auth purpose. But its showing "An error occurred. Please try again later."
Any one please help me ? or help me with working code ?
<?php
require_once('../src/facebook.php');
$fb_app_id = "key";
$fb_secret = "secret";
$fb_app_url = "url";
$facebook = new Facebook(array(
'appId' => $fb_app_id,
'secret' => $fb_secret,
'cookie' => true,
));
$user = $facebook->getUser();
if ($user) {
// The user is logged in
try {
$user_profile = $facebook->api('/me');
// Here : API call succeeded, you have a valid access token
} catch (FacebookApiException $e) {
// Here : API call failed, you don't have a valid access token
// you have to send him to $facebook->getLoginUrl()
$user = null;
}
} // else : the user is not logged in
?>
<?php if ($user): ?>
Logout of Facebook
<?php else: ?>
Login with Facebook
<?php endif ?>
You can try this
require_once('../src/facebook.php');
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxx',
'cookie' => true,
));
$fb_user_id = $facebook->getUser();
try
{
$fb_user_profile = $facebook->api('/me');
$me = $facebook->api('/me');
}
catch (FacebookApiException $e)
{
$fb_user_id = NULL;
}
$fbid = $me['id'];
$firstname = $me["first_name"];
$lastname = $me["last_name"];
$gender = $me["gender"];
$email = $me["email"];
if ($facebook->getSession()) {
echo '<div id="fbc-img"><img src="http://graph.facebook.com/'.$me['id'].'/picture?type=normal" width="56" height="56"></div>
<div id="fbc-info">Welkom, '.$firstname.' ! <br>Logout </div> ';
} else {
echo 'Login met facebook ';
}
Trying to post a status update using facebook php sdk. Code posted below. As I understand it, I need ask for extended persmission, which I do in the login URL with '&scope=publish_stream', but this still isn't working.
The resultant login URL looks like:
https://www.facebook.com/login.php?api_key=174954539217971&cancel_url=http%3A%2F%2Fexample.com%2facebook.post.php&display=page&fbconnect=1&next=http%3A%2F%2Fexample.com%2Ffacebook.post.php&return_session=1&session_version=3&v=1.0&scope=publish_stream
So you can see it is being set. While this is essentially the same question as: Facebook OAuthException: "user hasn't authorized the application to perform this action"
The suggested solution in that thread is not working here.
<?php
echo "Posting to facebook..<br/>";
require './fb_src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
error_log($e);
}
}
else {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
}
print_r($me);
try {
$feed = $facebook->api('/me/feed', 'post', array('message' => 'Hello world!', 'cb' => ''));
} catch (FacebookApiException $e) {
print($e);
}
print_r($feed);
?>
parameter is 'req_perms' not 'scope'. So hard to find good documentation...
Works fine now.