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 ;)
Related
Im a totally facebook newbie developer.This is the sdk version that i use: click . This is the login part:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXx',
'cookie' => true
));
?>
<?php
$loginUrl = $facebook->getLoginUrl(array (
'scope' => 'email',
'redirect_uri' => 'http://facebook.ebis-servicii.ro/mytest/test.php'
));
$user = $facebook->getUser();
echo 'Login Here ';
?>
And this is the part where I want to get some informations about the user:
<?php
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXX',
'cookie' => true
));
$user = $facebook->getUser();
if ($user) {
try{
$user_profile = $facebook->api('/me');
var_dump($user_profile);
$fbid = $user_profile['id']; // To Get Facebook ID
$fbuname = $user_profile['username']; // To Get Facebook Username
$fbfullname = $user_profile['name']; // To Get Facebook full name
$femail = $user_profile['email']; // To Get Facebook email ID
}catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
echo "FBID ".$fbid."<br>";
echo "Email ".$femail."<br>";
echo "Name ".$fbfullname."<br>";
echo "Username ".$fbuname."<br>";
echo "Facebook Picture:<br>";
//echo '<img src="https://graph.facebook.com/'.$_SESSION["USERNAME"].'>;
echo "<img src ='https://graph.facebook.com/".$fbuname."'>";
}
?>
This is the url link where you see the results. So my question is how do I get the username? In the result of the var dump there is no username .
Since v2.0, the username is not included in the result anymore, as you can see in the Facebook docs: https://developers.facebook.com/docs/graph-api/reference/v2.1/user
Else, the App Scoped IDs would be pointless, and they came with v2.0 too. Apps should not be able to get to the "real" profile of the users.
You should be able to use the App Scoped ID to show the user picture:
echo '<img src="https://graph.facebook.com/'.$fbid.'/picture?width=121&height=100" />';
Btw, i would suggest using the new PHP SDK (4.x) for new Apps.
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.
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.
I want to extract user profile details using php. I used the following code but I m getting the error: "Fatal error: Class 'Facebook' not found in C:\wamp\www\test\cc.php on line 12".
How do I include Facebook class into my file cc.php?
<?php
require_once('aa.php');
$config = array(
'appId' => 'YOUR_APP_ID',
'secret' => 'YOUR_APP_SECRET',
'allowSignedRequest' => false // optional but should be set to false for non-canvas
apps
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
?>
<html>
<head></head>
<body>
<?php
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>
I'm using the Facebook openGraph API for PHP. Here is my code:
<?php
session_start();
if (!isset($_SESSION['username'])) {
header("Location: index.php");
break;
}
$fbconfig['appid' ] = "APPID";
$fbconfig['secret'] = "SECRET";
$fbconfig['baseurl'] = "http://blah.com/";
$user = null;
try{
include_once "facebook.php";
}
catch(Exception $o){
error_log($o);
}
// Create our Application instance.
try {
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
} catch (Exception $e) { echo "ERROR: ".$e; }
$user = $facebook->getUser();
$loginUrl = $facebook->getLoginUrl(
array(
'scope' => 'email,publish_stream,read_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown',
'redirect_uri' => $fbconfig['baseurl'].'blah.php'
)
);
$logoutUrl = $facebook->getLogoutUrl(array( 'next' => ($fbconfig['baseurl'].'blah.php') ));
//if user is logged in and session is valid.
if ($user ){
header("Location;$loginUrl");
break;
}
?>
Now, this works on other pages (the Facebook part) but on this page only (same dir as other pages) The browser thinks that the PHP ends at "'appId' =>" and from then on interprets it as html. So I get this in my browser:
http://cl.ly/image/1y2a2F0e3I35
Sorry for the weird cropped picture, but I can't divulge the name or intent of the business.
There is HTML after the PHP on the real site and I can give this + js if needed.
Thank you!
your header is wrong.......try following......
header("Location:$loginUrl");
and also if you don't get facebook user id then you have to redirect to login page but you did reverse.may do following
//if user is not loggin
if (empty($user)){
header("Location:$loginUrl");
}else{
try {
$userInfo = $facebook->api("/$user");
} catch (FacebookApiException $e) {
$userInfo=null;
}
}