I'm trying to do login via FB. If I'm not logged in FB, everything works fine but if I'm logged in FB and I click on "login via facebook!" hyperlink the URL has changed on comething like this fb2.php?code=AQAlKrGXWS2bhiZ-ZK7UPiDRLdVMjR84jAbxS41kW3I5h5P... but I'm not logged and if I'm not login till I click second time. It's strange, isn't it? Could you someone help me? The source code is here.
<?php session_start();
define('APP_ID', 'my_api_id');
define('APP_SECRET', 'my_app_secret');
define('CANVAS_PAGE', 'http://apps.facebook.com/xxx/');
define('CANVAS_URL', 'http://localhost/xxx/');
require_once 'facebook.php';
$facebook = new Facebook( array('appId' => APP_ID, 'secret' => APP_SECRET, ));
$user = $facebook->getUser();
if(isset($user)) {
try {
xtended_permission)
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = NULL;
}
}
if ($user) { $logoutUrl = $facebook->getLogoutUrl(array('next' =>
"http://metalradio.cz/metalRadioTesting/facebook-sdk/logout.php")); }
else { $loginUrl = $facebook->getLoginUrl( array(
'scope' => 'email' ) ); }
?>
<!doctype html>
<html>
<head>
<title>Facebook login</title>
<meta content="text/html; charset=utf-8" http-equiv="Content-Type">
</head>
<body>
<?php
if(!is_null($user))
echo "Name: " . $user_profile['email'];
?>
<h2>Users authentification via FB</h2>
<?php if ($user):
?>
logout!
<?php else:?>
<div>
login via facebook!
</div>
<?php endif?>
</body>
</html>
Related
yesterday i got answer get friends list from facebook using App_id and App_secret i got friends list.today i trying to run the code i getting error.but same code working yesterday ?
today I am getting Error Like this
Fatal error: Call to undefined method Facebook::getLoginStatusUrl() in D:\xampp\htdocs\kenshinkan-new\kenshinkan\facebook.php on line 34
<?php
require '/src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxx',
));
$app_id = 'xxxxxxxxxxxxxxxxxxxx';
$app_secret = 'xxxxxxxxxxxxxxxxxxx';
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
$result = $facebook->api('/me/friends');
print "<pre>";
// print_r($result);
$json_output=($result['summary']['total_count']);
echo "$json_output";
print "</pre>";
} else {
$statusUrl = $facebook->getLoginStatusUrl();
$loginUrl = $facebook->getLoginUrl(array('scope' => 'user_friends,read_stream, export_stream'));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>demo</title>
</head>
<body>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
Logout
<?php else: ?>
<div>
Login:Login with Facebook
</div>
<?php endif ?>
</body>
</html>
but this code working yesterday? Today i am getting Error? how to solve this error? i want get friends list from facebook? please any idea about it?
I got Answer. i have just change Line
$statusUrl = $facebook->getLoginStatusUrl();
change To
$statusUrl = $facebook->getLoginUrl();
its Working Good Now...
I’m just using facebook php sdk v3.2.3
error type : curl exception…
I just use host with curl but it doesnt work…
Here is my code:
<?php
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => '',
'secret' => ''
));
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Facebook PHP</title>
</hea <body>
<h1>Hello World</h1>
<?php
//get user from facebook object
$user = $facebook->getUser();
if ($user): //check for existing user id
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
echo '<p>getType: ',$e->getType(), '</p>';;
echo '<p>getMessage: ',$e->getMessage(), '</p>';;
}
echo '<p>User ID: ', $user, '</p>';
//print logout link
echo '<p>logout</p>';
else: //user doesn't exist
$loginUrl = $facebook->getLoginUrl(array(
'diplay'=>'popup',
'scope'=>'email',
'redirect_uri' => 'http://apps.facebook.com/blabla'
));
echo '<p>login</p>';
endif; //check for user id
?>
</body>
</html>
Just a few modifications will solve it, I GUESS (Guess, as I don't still see the facebook.php)
<?php
require 'facebook.php';
$facebook = new Facebook(array('appId' => '', 'secret' => ''));
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN""http://www.w3.org/TR/html4/loose.dtd">
<html lang="en">
<head>
<meta charset="utf-8" />
<title>Facebook PHP</title>
</head>
<body>
<h1>Hello World</h1>
<?php
//get user from facebook object
$user = $facebook->getUser();
if ($user): //check for existing user id
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
// If the user is logged out, you can have a
// user ID even though the access token is invalid.
// In this case, we'll get an exception, so we'll
// just ask the user to login again here.
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
echo '<p>getType: ',$e->getType(), '</p>';;
echo '<p>getMessage: ',$e->getMessage(), '</p>';;
}
echo '<p>User ID: ', $user, '</p>';
//print logout link
echo '<p>logout</p>';
else: //user doesn't exist
$loginUrl = $facebook->getLoginUrl(array(
'diplay'=>'popup',
'scope'=>'email',
'redirect_uri' => 'http://apps.facebook.com/blabla'
));
echo '<p>login</p>';
endif; //check for user id
?>
</body>
</html>
Let's see what will happen, we are tuned here, keep in touch.
im trying to get some data with the facebook api. Here is my code
<?php
include 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxx',
'secret' => 'xxx',
'cookie' => true
));
?>
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title></title>
</head>
<body>
<?php
$user = $facebook->getUser();
if($user)
{
$user_profile = $facebook->api('/me/statuses');
echo "<pre>", print_r($user_profile), "</pre>";
echo "<a href=logout.php>Logout</a>";
}
else
{
$login_url_params = array(
'scope' => 'publish_stream,read_stream,offline_access,manage_pages'
);
$login_url = $facebook->getLoginUrl($login_url_params);
//redirect to the login URL on facebook
header("Location: {$login_url}");
}
?>
</body>
</html>
The problem is that when i print_r($user_profile) it shows all the data that is supposed to come with $user_profile = $facebook->api('/me/statuses'); but it doesn't show the comments associated with the status. I tried this in the Graph API Explorer and it works fine (shows all of the data), but in my code when i run it; it doesn't get the comments.
Any idea on why this is happening?
$user_profile = $facebook->api('/me/feed');
So I've started learning facebook application and got my first obstacle. Whenever i log into my simple hello user application, i'm redirected into canvas URL (App content opens on server directly, instead inside facebook iFrame).
Here's the code
<?php
require_once("php-sdk/facebook.php");
$config = array(
'appId' => '',
'secret' => ''
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'https://apps.facebook.com/401822713222945/'
);
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
</head>
<body>
<?php
if ($user_id){
try {
$user_profile = $facebook->api('/me','GET');
echo 'Welcome ' . $user_profile['name'] . '!';
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please log in.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please log in.';
}
?>
</body>
</html>
Try the following right after the <body> tag.
<script type="text/javascript">
if(top === self){
document.location = "<?php echo $params['redirect_uri'];?>";
}
</script>
It will check if the current window is the most top window, if you're accessing the page directly it will then redirect you to the iframed page.
I followed this video tutorial but i am stuck.When i click on the login link the browser keeps loading and then nothing happens and still the login page is in front of me.It always return 0 in $user_id.It showed me the facebook authentication page 1 to 2 times previously and i clicked 'allow' but now nothing is happening.One more thing , when i click the login link the browser url changes from
"http://localhost/php/fbdata.php"
to
"http://localhost/php/fbdata.php?state=e1d5ccf919c6a9d3325200596c12b447&code=AQDg8hE1GuKQ6eq9nGeLV8BIK8EjeIuY8Drf6g0FwAWAjGkvFi70EoNquPmoYsk2PxKpcfxVWYqNgVxU7rRQ-xBxcZXziH5n9IXNNl1KKzLtUYgVRKqWRczh2wINcvDY8WuWoMpIETxWpYhIbrZ-w46xB1v2YMADbOfrFNxLhiyIC239GIQRC__Tw_KiYoZiK1A#="
i dont have any idea if it's returning me something or anything else.
What i want is that i am able to post "hello world" at my facebook page.Here's my php code.
require_once('facebook.php');
$config = array(
'appId' => 'xxxxx',
'secret' => 'xxxxxxxxxx',
'cookie' => 'true'
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$user_profile=null;
if($user_id)
{
try
{
$user_profile = $facebook->api('/me');
$facebook->api('/me/feed','post',array(
'message'=>'hello world'
));
}
catch(FacebookApiException $e)
{
echo $e->getMessage();
}
}
if($user_id)
{
$logout_url=$facebook->getLogoutUrl();
echo "<a href='$logout_url'>Logout</a>";
}
else
{
$login_url=$facebook->getLoginUrl(array(
'scope'=>'read_friendlists,publish_stream,email,user_work_history,user_website,user_religion_politics,user_relationship_details,user_relationships,user_interests,user_hometown,user_education_history,user_birthday,user_about_me'
));
echo "<a href='$login_url'>Login</a>";
}
Mauzzam,
This code works for me - please take a look at the examples folder in the PHP SDK
<?php
require '../src/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '350881691595504',
'secret' => 'a50ae255f0ba3c24000e38ede7f666b9',
));
$user = $facebook->getUser();
if ($user) {
try {
$response = $facebook->api('/me/feed','post',array(
'message'=>'hello world'
));
} 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();
}
?>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>PHP SDK TEST</title>
</head>
<body>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<?php if ($user): ?>
<h3>Hello World Result</h3>
<pre><?php print_r($response); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>