facebook php sdk not working - php

i used two codes and its not working the first one :
<?php
require_once('fb/facebook.php');
$config = array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
function render_login($facebook) {
$canvas_page = 'http://fbbost.eb2a.com/';
// HERE YOU ASK THE USER TO ACCEPT YOUR APP AND SPECIFY THE PERMISSIONS NEEDED BY
$login_url = $facebook->getLoginUrl(array('scope'=>'email,user_photos,friends_photos', 'redirect_uri'=>$canvas_page));
echo 'Please login.';
}
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "Name: " . $user_profile['name'];
} catch(FacebookApiException $e) {
render_login($facebook);
echo "1";
error_log($e->getType());
error_log($e->getMessage());
}
} else {
render_login($facebook);
echo "2";
}
?>
</body>
</html>
and the second one :
<html>
<head>
<title>NNN</title>
</head>
<body>
<?php
include "fb/facebook.php";
$facebook=new Facebook(array(
'appId' => 'xxxxx',
'secret' => 'xxxxx',
'cookie' => true
));
$session=$facebook->getUser();
$me=null;
if($session){
try{
$me=$facbook->api('/me');
print_r($me);
}
catch (FacebookApiException $e){
echo $e->getMessage();
}
}
if($me){
$logoutUrl=$facbook->getLogoutUrl();
echo "<a href='$logoutUrl'>Logout</a>";
}
else{
$loginUrl=$facebook->getLoginUrl(array(
'scope' => 'publish_stream,read_friendlists'
));
echo "<a href='$loginUrl'>Login</a>";
}
?>
</body>
</html>
both codes return the same error when i login :
App Not Setup: The developers of this app have not set up this app properly for Facebook Login.

I think your app is not public. You need to set it to public to use it with any Facebook account other than the developer's own account. Go to "Status and Review" and make the app public.
Besides, check that you have properly setup the app domain.

Related

Extracting friend's list using Facebook PHP SDK

I am stuck with this issue since awhile now. I am trying to retrieve my friends list (who are using this app, ofcourse). The SDK version is 3.2.3. The code does pull out information from my profile, but does not pull anything from my friends profiles. Though i am using the scope permission parameter with my login. Following is my index.php file :
require_once('facebook.php');
$config = array(
'appId' => 'xxxx',
'secret' => 'xxxx'
//'allowSignedRequest' => false
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
<html>
<head></head>
<body>
if($user_id) {
try {
$user_profile = $facebook->api('/me','GET');
echo "<pre>";
print_r($user_profile); //prints my profile
echo "</pre>";
foreach ($user_profile["data"] as $friend) {
echo $friend['id'];
echo $friend['name'];
}
echo '<br><a href=' . $facebook->destroySession() . '>Logout</a>';
} catch(FacebookApiException $e) {
}
} else {
$params = array(
'scope' => 'public_profile, user_friends, email',
);
$login_url = $facebook->getLoginUrl($params);
echo 'Please login.';
}
</body>
</html>
How can i just print my friend's list? (those friends whose permission i have!)
Change the
$user_profile = $facebook->api('/me','GET');
to
$user_profile = $facebook->api('/me?fields=id,first_name,last_name,friends','GET');
for example.

Facebook URL by using php sdk is not loading

I use the following code to connect to the facebook via my site, but the facebook page is not loading.
The cook is 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>
and when I click on the login
If you click on that login here it does not load the facebook
what should I do in this case
You're outputting logoutUrl rather than loginUrl
Change:
echo 'Login';
to use
echo 'Login';

Facebook Php SDK instantiate

my problem is:
I downloaded Facebook Php SDK from: https://github.com/facebook/facebook-php-sdk
I put all in a folder an created my index in this way:
myAppFolder:
src
index.php
In my index.php I tried this code:
require_once("src/facebook.php");
$facebook = new Facebook(array('appId' => 666, 'secret' => 616));
die("why not zoidberg?");
But my app doesn't die and doesn't return anything, some ideas?
Next I tried this code, but only the first echo is displayed:
<?php
echo "This is visible";
try{
require_once "src/facebook.php";
$facebook = new Facebook(array(
'appId' => '666',
'secret' => '616',
));
}catch(ErrorException $e){
echo error_reporting(E_ALL);
die(var_dump($e));
}
die("This is not visible");
My output is:
This is visible
This code works perfectly for me:
index.php
<?
require_once('src/facebook.php');
$config = array(
'appId' => 'myappId',
'secret' => 'mysepratecode',
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?
if($user_id) {
// We have a user ID, so probably a logged in user.
// If not, we'll get an exception, which we handle below.
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.';
error_log($e->getType());
error_log($e->getMessage());
}
} else {
// No user, print a link for the user to login
$login_url = $facebook->getLoginUrl();
echo 'Please login.';
}
?>
</body>
</html>
Resolved, I didn't have Json and Curl installed ;)

facebook user authentication and post on user wall

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>

Facebook Authentication window does not display?

I have a file named fbmain.php which do Authentication part of my facebook app. But it doesn't display the Authentication window?
//fbmain.php
<?php
//facebook application
//set facebook application id, secret key and api key here
$fbconfig['appid' ] = "MY_APP_ID";
$fbconfig['secret'] = "MY_APP_SECRET";
$uid = null; //facebook user id
try{
include_once "facebook.php";
}catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
//Facebook Authentication part
$session = $facebook->getSession();
$loginUrl = $facebook->getLoginUrl(
array(
'canvas' => 1,
'fbconnect' => 0,
'req_perms' => 'email,publish_stream,status_update,user_birthday,user_location,user_work_history,user_likes,user_photos'
)
);
if (!$session) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
if ($session) {
echo "<br/>session ok"; //It create a session and disply 'session ok' string
try {
$uid = $facebook->getUser();
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
}
?>
Can anyone please help me with this?
Which version of PHP-SDK you are using??
I think you can get rid of getSession() and use just getUser().
change your code as below and try again..
$uid = $facebook->getUser();
if ($uid) {
// do some api calls or something else
}
else
{
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
}
else in index.php just include your fbmain.php and use a image button where you can link the $loginurl..

Categories