facebook user authentication and post on user wall - php

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>

Related

FB: PHP simple connect script : how to get email (PHP SDK OAuth 2.0 )?

I am using a simple php connect script:
http://code.runnable.com/UfwdES1fQz9uAAAh/simple-facebook-connect-php-example
I do retrieve name, id
How can I retrieve the email ?
I tried with
$user_profile = $facebook->api('/me?fields=email');
$user_profile = $facebook->api('/me/email');
with no success !
<?php
require 'lib/facebook-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>
<body>
<?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>
Replace your $loginUrl var with
$facebook->getLoginUrl(array("scope" => "email"));
afterwards you can use
/me?fields=email

Get Friends List in facebook Graph Api?

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 am using php sdk to post on facebook but the page is not loading

I am using the following code to post on facebook from my page but the lonin page is not loading
you can see the page
When I click on the following link the facebook required page is not loading
The code is look like that
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => '198516340340394',
'secret' => 'f11117b96e0996ecbf7d7f4919c0cf70',
'cookie' => true
));
$user = $facebook->getUser();
$user_profile = null;
if ($user) {
try {
$user_profile = $facebook->api('/me');
$facebook->api('/me/feed/', 'post', array(
'message' => 'I want to display this message on my wall'
));
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user_profile) {
$logoutUrl = $facebook->getLogoutUrl();
echo 'Logout';
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'publish_stream, read_friendlists',
));
echo 'Login';
}
?>
<html>
<head>
<title>PHP SDK</title>
</head>
<body>
</body>
</html>
Please help me in this case and please give me the detail answer, and be with me
There are a couple problems with what you're doing here.
First it's always best to have valid markup ie doctype and html tags your page is missing. but more importantly... this
echo 'Login';
is wrong. the markup is just as it looks I think you're expecting a link. Change it to
echo 'Login;
and try again. the reason is php does not recognize variables in a single quoted string.

Authentification via Facebook need two clicks in some cases

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>

Facebook PHP API code not working with "Pages" to get Wall posts

<?php
require '../src/facebook.php';
try
{
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => '120875194666085',
'secret' => '0272027b5c5c1dabde81096497970c56',
'scope' => 'read_stream',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me/feed');
} 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('scope' => 'read_stream'));
}
}
catch(FacebookApiException $e){}
?>
<?php
if ($user): ?>
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<?php if ($user): ?>
<?php
for($i = 0; $i < 25; $i++)
{
echo "<br />";
echo $user_profile['data'][$i]['from']['name'];
echo " : ";
echo $user_profile['data'][$i]['message'];
}
?>
<?php endif ?>
This worked just fine for normal accounts, but when I tried to log in with a Facebook "Pages" account, it didn't work at all. Any help on this matter? Also, when I got to the Facebook Graphs API documentation, even those links don't give me a proper feed of the Pages wall. From what I had gathered, I had to
https://graph.facebook.com/[company page id]/feed??access_token=[access_token]
to get it to work (which I am not sure how to translate into the Facebook API.
You should be able to access Fan Page Feeds as long as you have any access_token.
If you have a valid authorised user that is logged in
$fan_page_feed = $facebook->api('/COMPANY_PAGE_ID/feed');
will return a JSON object containing feed data.
Alternatively you could use the applications access_token to get the information. This example gets the feed from the Facebook Platfrom page (For the sake of conciseness, the script also assumes everything will work perfectly - you should implement your own error handling as you see fit):
<html><head></head>
<body>
<pre>
<?php
require ('facebook.php');
$facebook = new Facebook(array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'cookie' => true
));
$result = $facebook->api('oauth/access_token', array('client_id'=>'YOUR_APP_ID', 'client_secret'=>'YOUR_APP_SECRET', 'grant_type'=>'client_credentials'));
// $result should contain 'access_token=VALID_ACCESS_TOKEN'
$access_token = explode('=',$result);
$fan_page_feed = $facebook->api('/19292868552/feed', array('access_token'=>$access_token[1]));
print_r($fan_page_feed);
?>
</pre>
</body>
</html>

Categories