tags:

views:

330

answers:

2

Hello,

Im new to developing facebook apps.I have the following issue and would be glad if someone could help.

I have registered my app on facebook and uploaded the code and php client library to the hosting server. If i use the code below then everything works fine.

<?php

require_once('./facebook/php/facebook.php');

/* initialize the facebook API with your application API Key
  and Secret */
$facebook = new Facebook("<my_api_key>","<my_secret_key>");

$user = $facebook->require_login();

echo "<p>Your User ID is: $user</p>";

echo "<p>Your name is: <fb:name uid=\"$user\" useyou=\"false\"/></p>";

echo "<p>You have several friends: </p>";
$friends = $facebook->api_client->friends_get();
echo "<ul>";

foreach ($friends as $friend)  {
echo "<li><fb:name uid=\"$friend\" useyou=\"false\" /></li>"; 
}
echo "</ul>";

/* Echo some information that will
  help us see what's going on with the Facebook API: */
echo "<pre>Debug:" . print_r($facebook,true) . "</pre>";


?>

But, if i divide the code into two files as follows then i just get a blank canvas when i navigate to http://apps.facebook.com/myapp

appinclude.php

<?php
require_once('./facebook/php/facebook.php');

/* initialize the facebook API with application API Key
  and Secret */
$facebook = new Facebook("<my_api_key>","<my_secret_key>");

$user = $facebook->require_login();

?>

index.php

<?php

require_once('./appinclude.php');

echo "<p>Your User ID is: $user</p>";

echo "<p>Your name is: <fb:name uid=\"$user\" useyou=\"false\"/></p>";

echo "<p>You have several friends: </p>";
$friends = $facebook->api_client->friends_get();
echo "<ul>";

foreach ($friends as $friend)  {
echo "<li><fb:name uid=\"$friend\" useyou=\"false\" /></li>"; 
}
echo "</ul>";

/* Echo some information that will 
  help us see what's going on with the Facebook API: */
echo "<pre>Debug:" . print_r($facebook,true) . "</pre>";

?>

Any way to fix this ?

Thank You.

A: 

I would check file paths.

It seems in your second (non-working version) you're trying to include a file from a directory called 'facebook/php/'.

If this is the directory that you've specified as your application's root then I would guess Facebook won't allow you to include files below that directory level.

Martin Bean
+1  A: 

The way I set up my app is to have all the files in the same directory so there is no confusion.

/Myapp/index.php

<?php
require_once('appinclude.php');
?>

/Myapp/appinclude.php

<?php
require_once('facebook.php');
?>

The Myapp directory looks like this:

/Myapp/jsonwrapper/JSON/JSON.php
/Myapp/jsonwrapper/JSON/LICENSE
/Myapp/jsonwrapper/jsonwrapper.php
/Myapp/jsonwrapper/jsonwrapper_inner.php
/Myapp/facebook.php
/Myapp/facebookapi_php5_restlib.php
/Myapp/appinclude.php
/Myapp/index.php

In your second example the layout would need to be like this to work:

/facebook/php/facebook.php
/Myapp/AnotherDirectory/index.php
/Myapp/appinclude.php

Notice the appinclude.php and index.php are not in the same folder. index.php looks for a file appinclude.php in the directory above itself, and appinclude.php looks for a file facebook/php/facebook.php in the directory above itself. If you were to remove the dots and slashes from the front of the require statements it would look for it all in the same directory. Basically what you need to realize is that the ./ in require_once('./appinclude.php'); says look in the directory above this one.

Samuel