Facebook Tab with Fat Free Framework - php

Is there some limitation with Fat Free Framework when we try to output the page into facebook tab?
I already try it with this below code, and unfortunately in the facebook tab iframe always empty blank page.
<?php
$f3=require('app/lib/base.php');
require_once 'app/lib/fb/facebook.php';
$f3->route('GET /',
function() {
echo 'Hello';
}
);
$f3->route('GET /landing',
function() {
$app_id = 'xxxx';
$secret_key = 'yyyy';
$page_id = 'zzzz';
$config = array(
'appId' => $app_id,
'secret' => $secret_key
);
$fb = new Facebook($config);
$fbdata = $fb->getSignedRequest();
$fbInPage = false;
if(!empty($fbdata) && is_array($fbdata)
&& !empty($fbdata['page']) && is_array($fbdata['page'])
&& !empty($fbdata['page']['id'])
) {
$fbInPage = $fbdata['page']['id'];
}
// Check if user not in fb tab
if(!$fbInPage) {
// Redirect to facebook tab
echo '<script>window.location.href="https://www.facebook.com/'.
$page_id.
'?sk=app_'.
$app_id.
'"</script>';
exit;
}
// Get User ID
$user = $fb->getUser();
// Check if user not connected to facebook
if ($user) {
try {
$user_data = $fb->api("/me");
} catch (FacebookApiException $e) {
$user_data = null;
}
} else {
// Asking permission for email and user_likes
$fb_login_url = $fb->getLoginUrl(array(
'scope' => 'email, user_likes'
));
echo '<script>top.location.href = "'.$fb_login_url.'"</script>';
exit;
}
}
);
$f3->run();
First when user try to access GET /landing it will redirect to facebook tab and show the page GET /. But somehow it always return empty page, already inspect it with firebug on firefox and there is no error, on the response tab always shows Reload the page to get source for: https://localhost/f3-fb/. Already try it with my office framework and works perfectly.
If anybody ever get this problem please advise.

The problem is in XFRAME, on default F3 set the XFRAME value as SAME-ORIGIN, based on this doc, developer must overload the value with ALLOW-FROM uri.
Here is the full code for index.php:
<?php
$f3=require('app/lib/base.php');
require_once 'app/lib/fb/facebook.php';
$f3->set('XFRAME', 'ALLOW-FROM https://localhost/f3-fb/');
$f3->route('POST /',
function() {
echo 'Hello';
}
);
$f3->route('GET /landing',
function() {
$app_id = 'xxxx';
$secret_key = 'yyyy';
$page_id = 'zzzz';
$config = array(
'appId' => $app_id,
'secret' => $secret_key
);
$fb = new Facebook($config);
$fbdata = $fb->getSignedRequest();
$fbInPage = false;
if(!empty($fbdata) && is_array($fbdata)
&& !empty($fbdata['page']) && is_array($fbdata['page'])
&& !empty($fbdata['page']['id'])
) {
$fbInPage = $fbdata['page']['id'];
}
// Check if user not in fb tab
if(!$fbInPage) {
// Redirect to facebook tab
echo '<script>window.location.href="https://www.facebook.com/'.
$page_id.
'?sk=app_'.
$app_id.
'"</script>';
exit;
}
// Get User ID
$user = $fb->getUser();
// Check if user not connected to facebook
if ($user) {
try {
$user_data = $fb->api("/me");
} catch (FacebookApiException $e) {
$user_data = null;
}
} else {
// Asking permission for email and user_likes
$fb_login_url = $fb->getLoginUrl(array(
'scope' => 'email, user_likes'
));
echo '<script>top.location.href = "'.$fb_login_url.'"</script>';
exit;
}
}
);
$f3->run();
Add the config $f3->set('XFRAME', 'ALLOW-FROM https://localhost/f3-fb/'); to allow this url inside on facebook iframe.
And always use POST for the route inside the facebook iframe (still don't know why must using that with F3, but it will occur the error and asking for POST route)

Related

php: post on Facebook page using FB apis are visible only to admin

I am trying to post to FB page using FB apis. Post goes through fine but it's visible only to admin user & not everyone who has liked the page. I tried changing privacy settings with EVERYONE & ALL_FRIENDS but it didnt help much. Below is snippet of my code.
Any help is truly appreciated.
//--------code snippet------------
require '../src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXX',
'secret' => 'YYYYYYYYYYYYYYYYYYY',
));
// Get User ID
$user = $facebook->getUser();
if ($user)
{
try {
$page_id = 'ZZZZZZZZZZZ';
$page_info = $facebook->api("/$page_id?fields=access_token");
if( !empty($page_info['access_token']) )
{
$args = array(
'access_token' => $page_info['access_token'],
'message' => "My Msgs!"
);
$post_id = $facebook->api("/$page_id/feed","post",$args);
}
else
{
$permissions = $facebook->api("/me/permissions");
if( !array_key_exists('publish_stream', $permissions['data'][0]) ||
!array_key_exists('manage_pages', $permissions['data'][0]))
{
$loginUrl = $facebook->getLoginUrl(array("scope" => "publish_stream, manage_pages"));
echo 'Login1 with FB';
}
}
} catch (FacebookApiException $e)
{
echo "error ", $e;
}
}
if ($user)
{
$logoutUrl = $facebook->getLogoutUrl();
} else
{
$loginUrl = $facebook->getLoginUrl(array('scope'=>'manage_pages,publish_stream'));
echo 'Login2 with FB';
}
It seems your app is set in development mode, you need to switch it to public under https://developers.facebook.com -> My Apps -> Select your app -> Status & Review -> Do you want to make this app and its all features available to general public? -> switch to YES

Facebook redirect url to index.php

index.php
<?php
//facebook application
$fbconfig['appid'] = "32##########";
$fbconfig['secret'] = "ca2dc#############";
$fbconfig['baseurl'] = "http://localhost/sbs/fblogin/index.php";
//
if (isset($_GET['request_ids'])) {
//user comes from invitation
//track them if you need
}
//facebook user uid
try {
include_once "src/facebook.php";
}
catch (Exception $o) {
error_log($o);
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'baseurl' => $fbconfig['baseurl'],
'cookie' => true
));
//Facebook Authentication part
$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.
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'
));
$logoutUrl = $facebook->getLogoutUrl();
if ($user) {
try {
// Proceed knowing you have a logged in user who's authenticated.
$user_profile = $facebook->api('/me');
}
catch (FacebookApiException $e) {
//you should use error_log($e); instead of printing the info on browser
// d is a debug function defined at the end of this file
$user = null;
}
}
//if user is logged in and session is valid.
if ($user) {
//get user basic description
$userInfo = $facebook->api("/$user?fields=picture,name,email,gender,birthday");
//$pic = $facebook->api("/$user/pictures");
$profile = json_encode($userInfo);
$res = json_decode($profile, true);
$_SESSION['name'] = $res['name'];
$_SESSION['email'] = $res['email'];
$_SESSION['id'] = $res['id'];
$_SESSION['gender'] = $res['gender'];
$_SESSION['birthday'] = $res['birthday'];
$_SESSION['img'] = $res['picture']['data']['url'];
$_SESSION['auth_type'] = "facebook";
if (isset($_COOKIE['registration']) && $_COOKIE['registration'] == true) {
header("location:../sbs/registration.php");
} else {
header("location:../sbs/sbs_login.php");
}
}
?>
For the 1st time I am working on the Facebook app. I have made app on the Facebook developer. It's working properly but it is not redirecting me in the index.php. I want to redirect it in this page only so all the values are stored in the session and I am checking it if the cookies is made then this value is going to registration.php and if not then its going to sbs_login.php. Please can anybody tell where to give the redirect url?
$loginUrl = $facebook->getLoginUrl(array(
'baseurl' => $fbconfig['baseurl'],
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'
));
I find the solution of my problem . so this is the solution
$loginUrl = $facebook->getLoginUrl(array(
'baseurl' => $fbconfig['baseurl'],
'scope' => 'email,offline_access,publish_stream,user_birthday,user_location,user_work_history,user_about_me,user_hometown,user_photos ,user_work_history'

PHP ends or breaks randomly using Facebook API

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;
}
}

Create Auto-POST by Facebook-SDK

I want to enable Facebook Auto Post in my website.
first I created an app, then follow the facebook docs I used the facebook-sdk for PHP, I inserted APP-ID and APP-SECRET, create LOGIN-URL and... to my script (everything like the facebook docs), but I still have problem!!
The problem is:
for first time when user visit my page, he see the login link. when he click on it he will redirect to facebook dialog page for allowing app activities. after this, when facebook redirect user to my canvas page, he see the login link again!! (It seems the getUser() function not worked correctly in my script!).
base on facebook guides the user must see the user profile details... but still the login link is visible.
how can I fix this problem...?
<?php
require_once("libs/facebook.php");
$config = array(
'appId' => 'XXXX',
'secret' => 'XXXX'
);
$fbConnect = new Facebook($config);
$user_id = $fbConnect->getUser();
if($user_id)
{
try {
$userProfile = $fbConnect->api('/me', 'GET');
echo "Name: " . $userProfile['name'];
} catch (FacebookApiException $e) {
$loginUrl = $fbConnect->getLoginUrl();
echo "<a href='" . $loginUrl . "'>LOGIN 2</a>";
}
}
else
{
$loginUrl = $fbConnect->getLoginUrl(array( 'scope' => 'publish_stream' ));
echo "<a href='" . $loginUrl . "'>LOGIN 1</a>";
}
?>
User always see "LOGIN 1"! it means the $user_id is always null (before and after app allowing activities)!! after app allowing (when user for first time click on loginUrl link) I have 'stat' and 'code' in my url query string! but still "LOGIN 1" is visible!
I guess you want to publish a message like "Hey guys, I am now using Aref's superawsome Facebook app now", right?
This can be accomplished with the following lines :
<?php
require_once("/FBAPI/src/facebook.php");
$config = array();
$config['appId'] = 'your_app_id';
$config['secret'] = 'your_app_secret';
$facebook = new Facebook($config);
$user = $facebook->getUser();
if(!$user){
$loginUrl = $facebook->getLoginUrl(array('scope'=>'publish_stream', 'redirect_uri'=>'http://www.example.com'));
}
if($user){
try{
$user_profile = $facebook->api('/me');
$access_token = $facebook->getAccessToken();
$vars = array(
'caption' => 'Aref\'s Facebook Application',
'message' => 'Hey guys, I am now using Aref\'s superawsome Facebook App :D',
'name' => 'Test',
'link' => 'http://www.example.com',
'description' => 'Aref\'s Facebook Canvas App',
'picture' => 'http://fbrell.com/f8.jpg'
);
$result = $facebook->api('/me/feed', 'post', $vars);
if($result){
echo "Post was set";
}
else{
echo "Error!";
}
}
catch(FacebookApiException $e){
error_log($e);
$user = NULL;
}
}
else{
echo '<img src="img/login.png"/>';
}
?>

Direct access after authentication insteed iframe access

When the user authorize my app, he is redirected to my site and not to facebook with my app in the iframe. Is there a way to stay in facebook ?
My code :
require './src/facebook.php';
$config = array();
$config['appId'] = 'XXXXX';
$config['secret'] = 'XXXXX';
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$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) {
echo "blah";
} else {
$params = array(
'scope' => 'read_stream, friends_likes',
'redirect_uri' => 'https://xxxxx'
);
$loginUrl = $facebook->getLoginUrl($params);
echo("<script> top.location.href='" . $loginUrl . "'</script>");
}
?>
Try putting your app's url (http://apps.facebook.com/your_app_namespace) in the redirect_uri param. This should fix the issue you are facing.

Categories