Username not displayed in Facebook Hello world application - facebook

I am new to creating a facebook application and hence started out with a simple Hello World Application. But When I try to display my Name, it is not displayed.
This is my code:
<?php
require_once 'facebook.php';
$api_key = 'mykey';
$secret = 'mysecretkey';
$facebook = new Facebook($api_key, $secret);
$user = $facebook->require_login();
$fbml = "<h2>Hello world, my name is <fb:name uid='<?php echo $fb_user;?>' useyou='false' />,"
. "welcome to my first facebook application!</h2>";
echo $fbml;
?>
The output in my facebook app is,
Hello world,my name is,welcome to my first facebook application!
My name is missing here.
And If I add the code given below,to display the friend's list,
$friends = $facebook->api_client->friends_get();
echo "<pre>Friends:" . print_r($friends, true). "</pre>";
I get the error message,
Warning: fopen() [function.fopen]: php_network_getaddresses: getaddrinfo failed:
No such host is known.
in C:\Documents and Settings\256148\My Documents\ide\xampplite\htdocs\facebookApp\facebookapi_php5_restlib.php on line 1755
Warning: fopen(http://api.facebook.com/restserver.php) [function.fopen]:
failed to open stream: php_network_getaddresses: getaddrinfo failed:
No such host is known. in C:\Documents and Settings\256148\My Documents\ide\xampplite\htdocs\facebookApp\facebookapi_php5_restlib.php on line 1755
Please guide me.

I have found a solution for my problem. I had to set the connect url in the application setting to my canvas callback url and created a cross domain communication channel file as given in this tutorial. This tutorial is really very helpful and explains in detail about creating a simple facebook app.
But still I get the Network error when I use $facebook->api_client->friends_get();

FBML Facebook Application
First, try using the following code for authentication process (applogin.php):
<?php
require_once "facebook.php";
require_once "config.php";
$facebook = new Facebook(array(
'appId' => FB_APP_ID,
'secret' => FB_API_SECRET,
'cookie' => true
));
$session = $facebook->getSession();
$params = array(
'canvas' => 1,
'fbconnect' => 0,
'next' => URL_CANVAS,
'cancel_url' => 'http://www.facebook.com/',
'req_perms' => 'publish_stream, status_update, offline_acces'
);
$login_url = $facebook->getLoginUrl($params);
// if not logged in, redirec to login page
if (!$session) {
echo '<fb:redirect url="' . $login_url . '" />';
exit;
} else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$access_token = $session['access_token'];
} catch (FacebookApiException $e) {
echo 'Error';
exit;
}
}
?>
Here's the config.php:
<?php
// Facebook
define('FB_APP_ID', '');
define('FB_API_SECRET', '');
define('URL_CANVAS', 'http://apps.facebook.com/YOURAPP/');
define('URL_CALLBACK', 'http://FB.YOURCALLBACKURL.COM/YOURAPP/');
?>
And finally your index.php
<?php
require_once dirname(__FILE__) . "/applogin.php";
// using Graph API
echo $me['name'];
?>
<!--// some FBML //-->
<fb:bookmark />
<h2>Hello world, my name is fb:name uid='<?php echo $uid;?>' useyou='false' />
welcome to my first facebook application!</h2>
Hope this will help you write your own Facebook application.

Related

Access token gets lost after visiting another page

When I visit index.php, everything works well and I see the acces_token appear in the session variable.
However, when I click on a link to go to a new page, and I click (on the new page) on a link that brings me back to index.php, the access_token is changed to a much shorter token, starting with my appId.
(edit: I just discovered that this shorter token is this: appid|appsecret)
I've tried linking to an exact copy of index.php, and the same thing happens. It's like there isn't a new facebook instance created, or something... Why isn't a new access_token created?
Below is my code. You'll notice that I don't log the user in on my app. That's because this part is done on another page, via the Javascript SDK.
I've been looking for hours... thanks in advance for your advice.
index.php
<?php
include "config.php";
require "ok.php";
?>
<br/><br/>
Link.
<br/><br/>
<?php
$user = $facebook->getUser();
$userInfo = $facebook->api("/$user");
?>
<?php d($user); ?>
<strong>PHP Session</strong>
<?php d($_SESSION); ?>
?>
ok.php
<?php
$fbconfig['appid' ] = "###";
$fbconfig['secret'] = "###";
$user = null;
try{
include_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
}
?>
config.php
<?php
session_start();
//We log to the DataBase
mysql_connect('...', '...', '...');
mysql_select_db('...');
?>

Facebook Canvas app login doesnt't work

i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>

Facebook Canvas App Login for new Users

i have created a facebook app and it works well for new users if they start the app with the direct server link (https://www.mydomain.com/app/index.php) and use the login button.
But if they try to use the app within facebook (https://apps.facebook.com/myapp) as a canvas app(for the first time) then they see the "landing" page of my app and the log in button. But if they click on it, an errormessage appears, that the content of the iframe couldn't be displayed. If they start the app in a new tab, then the login process works. (see the error below, sorry it's german, maybe you are familiar with this type of error message?)
It would be desirable if the new user could use the login link in the canvas app.
How can this be achieved? My index.php looks like this: (i am using the facebook SDK)
<?php
require_once('php-sdk/facebook.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>

iFrame app no longer working

I have a simple Facebook App that used to work in the old API. It's an iFrame app and I can't get the user session with the new API no matter what I try. Here is the code I have now.
error_reporting(E_ALL);
require 'facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxx',
'secret' => 'xxxx',
'cookie' => true,
));
$session = $facebook->getSession();
if ( !$session )
{
//echo "Failed. No session.<br />";
$url = $facebook->getLoginUrl(array(
'canvas' => 1,
'fbconnect' => 0,
'next'=>'http://www.facebook.com',
'cancel_url' => 'http://www.facebook.com'
));
//echo "<script type='text/javascript'>top.location.href = '$url';</script>";
echo "<a href='".$url."' target='_blank'>First time using this App? Click here to connect it to your Facebook.</a>";
}
else {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$updated = date("l, F j, Y", strtotime($me['updated_time']));
echo "Hello " . $me['name'] . "<br />";
echo "You last updated your profile on " . $updated;
} catch (FacebookApiException $e) {
echo "Error:" . print_r($e, true);
}
}
I've tried various things but $session is never returned (after clicking the link). I am using the correct appID/secret/facebook.php but it always falls into the !$session if path. What am I doing wrong?
Use new version of php-sdk (3.1.1) :)
Your code uses old sdk (in new there is no getSession method) and you probably have migration setting "OAuth Migration" set to true.

Simple Iframe based facebook authentication

I wrote a facebook application but authentication is not working properly. Despite of googling several hours it did not solve.
please please write the script which authenticates the user properly and if the user clicks the allow button simply display the text "Welcome" assuming that i have written all basic suff aas..
require 'sdk/src/facebook.php';
$app_id = 'MY APP ID';
$canvas_page = "MY CANVAS URL";
// Create our Application instance (replace this with your appId and secret).
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page) ."&scope=email,read_stream&installed=1";
$facebook = new Facebook(array(
'appId' => $app_id ,
'secret' => MY APP SECRET,
'cookie' => true,
));
///... your code to solve !
Nobody provides the ultimate solution: Finally I solved it by myself.
what I do is. Facebook sends an "installed=1" parameter with the url when the user allows the application. SO I checked this parameter and redirected again to the application. I m extremely happy on finding extremely nice and efficient solution. Nobody answered me any votable answer.
if(isset($_REQUEST['installed']))
echo("<script>top.href.location='http://apps.facebook/myapp' </script>");
It seems that you are mixing the examples from the documentations and the PHP-SDK, dropping the example from the PHP-SDK should work.
Following the code in this answer:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXX',
'secret' => 'XXXXXXXX',
'cookie' => true,
));
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
echo "Welcome User: " . $me['name'] . "<br />";
} catch (FacebookApiException $e) {
error_log($e);
}
} else {
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>