facebook connect api - facebook

Is it possible to use the facebook connect api to just grab some information and use it with a customized form?
Because facebook register api shows a pre-filled form, how to get these data and use them with another form
Thanks

You should use the Facebook PHP SDK (see on github). Here is how you would do that.
require "facebook.php";
$facebook = new Facebook(array(
'appId' => '...',
'secret' => '...',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
Here, if $user is not null, the user is logged in and you have his personal data in the array $user_profile.
Here is an example of the form you can generate pre-filled with some Facebook data :
<?php if ($user): ?>
<form action="#" method="get">
<input type="text" name="name" value="<?php echo $user_profile['name'] ?>">
<input type="submit" value="Continue →">
</form>
Logout of Facebook
<?php else: ?>
Login with Facebook
<?php endif ?>
See the example of the Facebook PHP SDK for more detail on the flow.

Related

Facebook login - php sdk - prob

I am in a total mess with facebook-login. The code is given below. You can see the output at http://beta.jokesnfunnypics.com/login. In the scope, I have given as email but when I click the link and is redirected to facebook, it says that the app asks for only the basic permissions. Moreover, I have mentioned the redirect_uri as http://beta.jokesnfunnypics.com/add but I am instead redirected to the same page only. More over again, after getting the permission, no info is being displayed at the page also. Please help me. I am ready to give a few dollars to anyone who gives me the correct code.
<?php
//Application Configurations
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXX";
$site_url = "http://www.beta.jokesnfunnypics.com/add";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get User ID
$user = $facebook->getUser();
if($user){
try{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} 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();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
$_SESSION['user']="abc;
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
From the code, $loginUrl variable is defined 2 times, scope and redirect_uri is missing in the second time . try to remove those lines from your code
<?php
$app_id = "XXXXXXXXXX";
$app_secret = "XXXXXXXXXXXXX";
$site_url = "http://www.beta.jokesnfunnypics.com/add";
try{
include_once "src/facebook.php";
}catch(Exception $e){
error_log($e);
}
// Create our application instance
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $app_secret,
));
// Get User ID
$user = $facebook->getUser();
if($user){
try{
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
if($user){
// Get logout URL
$logoutUrl = $facebook->getLogoutUrl();
}else{
// Get login URL
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email',
'redirect_uri' => $site_url,
));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
$_SESSION['user']="abc";
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Refer these tutorials .
Login with Facebook using PHP SDK
Login with facebook using PHP ( Demo and Download )
Well usually I don't use the php sdk to generate my login url, instead I do it manually, try changing this line:
Login with Facebook
To this:
Login with Facebook
The correct code in PHP - 3 steps in 1 form - is explained by iZend - The web engine, including the configuration on Facebook: http://www.izend.org/en/identification-by-facebook.
Opening a connection with Facebook using the SDK.
Obtaining a URL for a Connect with Facebook button.
Retrieving a connected user's email address (and other information like firstname and lastname for a register form).

Facebook php sdk - info not being shown

I downloaded the php sdk from github (link provided from developers.facebook.com). I uploaded the src folder at the root. Now, I pasted the code and replaced the appId and secret with mine. But the page is showing a server error. The following is my code :-
<?php
require 'src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
));
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} 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();
}
$naitik = $facebook->api('/naitik');
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<title>php-sdk</title>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
Please help me. I badly need some help. I cannot find any mistake nor have I modified any part of the code except the app id and the secret. The facebook.php file is in the src folder only. You can find my output at http://beta.jokesnfunnypics.com/login.
the issue is possibly that you are missing php Curl extension
Facebook PHP SDK Use's Curl To Send Requests.
so First you need to check if the CURL extension is installed at your server
like this
function _isCurl(){
return function_exists('curl_version');
}
if not install it and then it will all work

Facebook application instance creation not working for every users

I have the following issue that drives me crazy. I use the sample code published with the Facebook PHP SDK (https://github.com/facebook/php-sdk/) for identifying a user. It works fine for most of my test users but for some users, the instance creation does not work. I’ve tried with the same machine and different machines, it does not change anything. Each user is correctly set as test user in my dev account.
What could be the cause?
Many thanks in advance.
require '../lib/facebook.php';
// Create our Application instance (replace this with your appId and secret).
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXX',
'secret' => 'XXXXXXXXXXX',
));
// Get User ID
$user = $facebook->getUser();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
} 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(array(
'scope' => 'publish_stream, publish_actions',
'redirect_uri' => 'http://apps.facebook.com/xxxxx/'
));
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
I finally found the answer. The problem was not related to the code. It was due to the fact that the test users bypassed the notification requesting to accept the invitation and instead entered directly the app url.
You have to delete the test user and recreate it. Then the test user will recieve a new notification and will then have to accept it.

Facebook - How to make the authorization dialog open in a popup?

I have the following code:
$facebook = new Facebook(array(
'appId' => 'id',
'secret' => 'secret',
));
$user = $facebook->getUser();
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'req_perms' => 'email,user_birthday,user_about_me'
));
}
and HTML:
<a class="button-fb" href="<?php echo $loginUrl; ?>" title="Connect with Facebook">Connect with Facebook</a>
The problem is that the <a> will open in the current page, I want to open in a popup (like most site do). How to do? =)
You can use this snippet to create a popup.. It uses Javascript.
<a class="button-fb" href="javascript: void(0)"
onclick="window.open('<?php echo $loginUrl; ?>',
'windowname1',
'width=200, height=77');
return false;" title="Connect with Facebook">Connect with Facebook</a>
I hope this helps.

PHP SDK v.3.1.1 - 'An active access token must be used' on simple example

I've had some big bugs implementing the latest PHP SDK and OAuth2, so I've broken it down into using the supplied example to demonstrate the problem I am experiencing.
I am using this code. I get the OAuthException 'An active access token must be used to query information about the current user.' When I click the Log In With Facebook link, I continue to get this error and I am not connected. Note that this does usually work when all cookies and sessions have been cleared, but after a couple of hours if I return it no longer works.
I'd be grateful for any help with this.
Also note this is not on a canvas page.
<?php
ob_start();
/**
* Copyright 2011 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License"); you may
* not use this file except in compliance with the License. You may obtain
* a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
* License for the specific language governing permissions and limitations
* under the License.
*/
require_once('../facebook-php-sdk/facebook.php');
include('app3.conf.inc.php');
$facebook = new Facebook(array (
'appId' => $appConf['appID'],
'secret' => $appConf['appsecret'],
));
// Get User ID
$user = $facebook->getUser();
// We may or may not have this data based on whether the user is logged in.
//
// If we have a $user id here, it means we know the user is logged into
// Facebook, but we don't know if the access token is valid. An access
// token is invalid if the user logged out of Facebook.
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me?fields=permissions');
} 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();
}
// This call will always work since we are fetching public data.
try
{
$naitik = $facebook->api('/naitik');
}
catch (Exception $ex)
{
echo("Exception with api/naitik:\n<pre>");
var_dump($ex);
echo("</pre>");
die();
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk</title>
<style>
body {
font-family: 'Lucida Grande', Verdana, Arial, sans-serif;
}
h1 a {
text-decoration: none;
color: #3b5998;
}
h1 a:hover {
text-decoration: underline;
}
</style>
</head>
<body>
<h1>php-sdk</h1>
<?php if ($user): ?>
Logout
<?php else: ?>
<div>
Login using OAuth 2.0 handled by the PHP SDK:
Login with Facebook
</div>
<?php endif ?>
<h3>PHP Session</h3>
<pre><?php print_r($_SESSION); ?></pre>
<h3>Cookies</h3>
<pre><?php print_r($_COOKIE); ?></pre>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
<h3>Public profile of Naitik</h3>
<img src="https://graph.facebook.com/naitik/picture">
<?php echo $naitik['name']; ?>
</body>
</html>
A bug has been raised and accepted by Facebook for the latest SDK. I suggest downgrading to one from earlier in 2011 until then. It solved my issue.
Seems like you were close. Below the php config for facebook, I provide a if GET query to detect if user logged out. When they log out, the code inside the condition fires
unset($_COOKIE['fbsr_' . $config['appId']]);
instead of
unset($_COOKIE['fbsr_' . $this->getAppId()]);
This seemed to workout like a charm.