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

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.

Related

Photo_upload permission depracated in facebook, how to fix this PHP script to working?

I have a php script below, to upload photo to facebook cover
<?php
if(is_single()){
if($_REQUEST['state']){
$imgsource = show_src('851','315');
}else{
$imgsource = $_POST['o'];
}
if($imgsource){
require_once "script/facebook.php";
$config = array(
'appId' => get_theme_option('app_id'),
'secret' => get_theme_option('secret'),
'fileUpload' => true,
);
$facebook = new Facebook($config);
$user_id = $facebook->getUser();
$photo = str_replace(home_url( '/' ),'',$imgsource);
$message = get_theme_option('descover');
if($user_id) {
try {
$ret_obj = $facebook->api('/me/photos', 'POST', array(
'source' => '#' . $photo,
'message' => $message,
)
);
$hasil3 = '<div id="modal-from-dom" class="modal hide fade">
<div class="modal-header">
×
<h2>You\'re almost done!</h2>
</div>
<div class="modal-body">
<img src="'.get_bloginfo("template_directory").'/images/eee.jpg" />
</div>
<div class="modal-footer">
Go to Facebook Profile
</div>
</div>
<button data-controls-modal="modal-from-dom" data-backdrop="true" data-keyboard="true" class="btn large success btnsingle">Preview Timeline</button>';
} catch(FacebookApiException $e) {
$login_url = $facebook->getLoginUrl( array(
'scope' => 'photo_upload'
));
error_log($e->getType());
error_log($e->getMessage());
}
} else {
$login_url = $facebook->getLoginUrl( array( 'scope' => 'photo_upload') );
if($imgsource){header('Location: '.$login_url);}
}
}
}
?>
its not working now, because photo_upload permission in API depracated, how to fix this PHP script to working?
i already tried change photo_upload with these, but looks like still not right:
publish_stream
publish_actions
user_photos

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 Graph API - post to users wall

I have a form which is redirecting to a php script to add fields to a mysql database. Once it has been added I want the app to ask permission to publish to the users stream and once authorised I want a message to be published to the users wall.
here is what ive got so far. the mysql bit is working but the post to wall is not.
<?php
require_once 'config.php';
mysql_connect("$host","$username","$password") or die(mysql_error());
mysql_select_db("$database") or die(mysql_error());
$mysql="INSERT INTO `dhxmascomp`.`users` (`user_id` , `name` , `user_email`) VALUES (NULL, '{$_GET[name]}','{$_GET[name2]}')";
if(!mysql_query($mysql))
die(mysql_error());
mysql_close();
require_once 'facebook-php-sdk/src/facebook.php';
$facebook = new Facebook(array(
'appId' => $FB_AppID,
'secret' => $FB_AppSecret,
'cookie' => true,));
$user = $facebook->getUser($_GET['access_token']);
try {
$publishStream = $facebook->api("/$user/feed", 'post', array(
'message' => 'test',
'link' => $FB_canvas_page,
'picture' => $submitPrefix. 'ibizasocial.com/plastik/images/logo_fb.png',
'name' => 'Plastik',
'description'=> 'test'));
} catch (FacebookApiException $e) {
//echo($e);
}
?>
<html>
<body style="810px; overflow:hidden">
<div style="overflow:hidden; width:810px;">
<img src="http://ibizasocial.com/dhapp/images/DHAPP3.jpg" border="0" />
</div>
</body>
</html>

After Facebook installa facebook app redirects me to the url but outside of Facebook

I have the following problem:
I'm trying to do my first application for Facebook and after Facebook connection and install the application not automatically redirects me to the url but outside of Facebook. This only happens the first time, once it has been installed if the url is recharged and stays within Facebook.
Any ideas to fix it?
I do not know if the problem is the code or application configuration:(
// PHP SDK v.3.0.0 de Facebook
require 'includes/facebook.php';
$facebook = new Facebook(array(
'appId' => '*******',
'secret' => '*************',
));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_stream,user_about_me,user_likes,email,user_status'
));
}
if (!$user) {
echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
exit;
}
?>
<!doctype html>
<html xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
<title>php-sdk 3.0.0</title>
<style typ="text/css">
html, body { width: 520px;}
</style>
</head>
<body>
<h1>php-sdk</h1>
<h3>PHP Session</h3>
<?php foreach($_SESSION as $key=>$value){
echo '<strong>' . $key . '</strong> => ' . $value . '<br />';
}
?>
<h3>Tu</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Tus datos (/me)</h3>
<?php foreach($user_profile as $key=>$value){
echo '<strong>' . $key . '</strong> => ' . $value . '<br />';
}
?>
</body>
</html>
You didn't specify a redirect_uri in your getLoginUrl method and hence the SDK constructed one for you that is based on your current URL.
So all you need to do is just set the canvas URL as your redirect_uri:
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'read_stream,publish_stream,user_about_me,user_likes,email,user_status',
'redirect_uri' => 'CANVAS_URL'
));
P.S: $user_profile and $me holds the same value, just use one of them instead of making two Graph API calls.

Facebook App Not Working in Safari

I have recently developed a facebook app using php. Though the app works in all browsers, it does not in Safari, it returns me to the first app step, after i try posting on a users wall. Following is my code.
My app displays a users friend list, and allows one to select friends, and post on their wall.
<?php
require ("facebook-api/src/facebook.php");
$facebook = new Facebook(array(
'appId' => '####',
'secret' => '####',
'cookie' => true,
));
// Login or logout url will be needed depending on current user state.
$params = array(
'scope' => 'email,user_birthday,friends_photos,publish_stream',
'redirect_uri' => 'https://apps.facebook.com/appname/'
);
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl($params);
}
$user = $facebook->getUser();
if ($user) {
$friends = $facebook->api('me/friends');
$uids = "";
foreach($friends as $f) {
$uids .= "uid=$f OR ";
}
$user_id_sess = $_SESSION['userid'];
$query_uids = substr($uids,0,strlen($query_uids)-4);
date_default_timezone_set('UTC');
$today = date('m\/d');
$data = $facebook->api(array('method'=>'fql.query','req_perms' => 'friends_birthday','query'=>"select uid,sex,name,birthday from user where uid in (select uid2 from friend where uid1=me()) ORDER by name ASC"));
echo "
<div class='span12 center-align' style='display:none;' id='block3'>
<form id='frmFb' name='frmFb' action='".$_SERVER['PHP_SELF']."' method='post' >
<p><img src='../static/images/logo.png' alt='logo' /></p>
<br />
<h1 style='margin-bottom:10px;'>Share with your friends</h1>
<p id='progress' style='display:none;'><img src='static/img/processing.gif' alt='Processing' /></p>
<div class='center-align well pull-center' style='max-height:300px; overflow:auto;'>
";
foreach($data as $today_bday) {
//$table='user_recipient';
$remfbid = $today_bday['uid'];
$fb_bday = $today_bday['birthday'];
echo "
<div style='width:50px; height:110px; margin:15px; float:left;' class='center-align'>
<a href='#' class='thumbnail'><img src='https://graph.facebook.com/".$today_bday['uid']."/picture' class='friend' alt='".$today_bday['name']."'></a>
<input type='checkbox' name='fbchoose[]' value='".$today_bday['uid']."'>
<p><small>".$today_bday['name']."</small></p>
<br/><br/>
</div>
";
}
echo "
</div>
<br />
<a href='#' class='btn btn-inverse btn-large'><i class='icon-ban-circle icon-white' style='margin-top:4px;'></i> CANCEL</a><input type='submit' value='SHARE NOW' name='btnAddReminder' class='btn btn-success btn-large' onclick='show()'>
</form>
</div>
";
} else {
echo "<p><script>top.location.href='$loginUrl';</script></p>
";
}
?>
<?
if(isset($_REQUEST['btnAddReminder'])){
if($_POST['fbchoose'] == "") {
$error_msg = "<div class='alert pull-center alert-error'><i class='icon-warning-sign' style='margin-top:2px;'></i> Please select at least one friend to share</div><br/>";
}
else {
foreach($_POST['fbchoose'] as $row) {
$vars = array(
'message' => 'Message...',
'name' => 'Name..',
'caption' => 'Caption',
'link' => 'Link',
'description' => 'Description',
'picture' => 'Picture...'
);
$facebook->api("/".$row ."/feed", 'post', $vars);
}
echo "<script>location.href='http://www.mywebsite.com/';</script>";
}
}
?>
in safari preferences, under "Privacy", set "Block cookies" to Never