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\2561开发者_如何学运维48\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.
精彩评论