PHP ends or breaks randomly using Facebook API - php

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

Related

PHP detect if user is fan of page doesn't always work

I'm using the following code as part of my facebook application that get user's permissions (mainly : likes ). This code will detect if the user is a fan of a page or not and based on that it will show some content or redirect him to another page, if he is not a fan. The main problem is that it works only one time or two times and then it shows nothing.
http://pastebin.com/RxefpmzD
<?php
require 'lib/facebook.php';
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXX',
));
$user = $facebook->getUser();
if ($user) {
try {
$likes = $facebook->api("/me/likes/333605930105938");
if( !empty($likes['data']) )
echo "<iframe width=\"745\" height=\"535\" src=\"http://www.youtube.com/embed/".$donnees['link']."?fs=1&autoplay=1&loop=1\" frameborder=\"0\" allowFullScreen=\"\"></iframe>";
else
echo "<iframe width=\"745\" height=\"535\" src=\"scren.php?id=".$donnees['id']."\" frameborder=\"0\" allowFullScreen=\"\"></iframe>";
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
if ($user) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl(array(
'scope' => 'user_likes'
));
}
?>

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'

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..

Facebook API OAuth Excpetion #200 with '&scope=publish_steam'

Trying to post a status update using facebook php sdk. Code posted below. As I understand it, I need ask for extended persmission, which I do in the login URL with '&scope=publish_stream', but this still isn't working.
The resultant login URL looks like:
https://www.facebook.com/login.php?api_key=174954539217971&cancel_url=http%3A%2F%2Fexample.com%2facebook.post.php&display=page&fbconnect=1&next=http%3A%2F%2Fexample.com%2Ffacebook.post.php&return_session=1&session_version=3&v=1.0&scope=publish_stream
So you can see it is being set. While this is essentially the same question as: Facebook OAuthException: "user hasn't authorized the application to perform this action"
The suggested solution in that thread is not working here.
<?php
echo "Posting to facebook..<br/>";
require './fb_src/facebook.php';
$facebook = new Facebook(array(
'appId' => 'appId',
'secret' => 'secret',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
error_log($e);
}
}
else {
header('Location: ' . $facebook->getLoginUrl(array('scope' => 'publish_stream')));
}
print_r($me);
try {
$feed = $facebook->api('/me/feed', 'post', array('message' => 'Hello world!', 'cb' => ''));
} catch (FacebookApiException $e) {
print($e);
}
print_r($feed);
?>
parameter is 'req_perms' not 'scope'. So hard to find good documentation...
Works fine now.

Facebook Iframe application authentication?

I have developed a Facebook application that runs inside an iframe in the Facebook canvas. For it to work properly I request extended permissions from the user. If the user hasn't authorized the application I send him/her to a login page with the getLoginUrl() method in the PHP SDK.
It works, but it's not pretty. The method sends the user to a landing page before the authentication page. It looks like this:
When I click "Go to Facebook.com" I see the actual page for permission requests (I also get right to the permissions page if I print the url, copy it and enter it into a new browser window). How do I make Facebook skip this step when I do the redirect from an Iframe?
My code looks like this (using CodeIgniter and Facebook PHP SDK):
$this->facebook = new Facebook(array(
'appId' => '{MY_APP_ID}',
'secret' => '{MY_SECRET}',
'cookie' => TRUE,
'domain' => $_SERVER['SERVER_NAME']
));
$this->facebook->getSession();
try {
$this->me = $this->facebook->api('/me');
}
catch (FacebookApiException $e) {
$this->me = NULL;
}
if ( is_null($this->me) ) {
redirect($this->facebook->getLoginUrl(array(
'req_perms' => 'offline_access,read_stream,publish_stream,user_photos,user_videos,read_friendlists',
'next' => $this->config->item('base_url').'fblogin.php?redirect_uri='.$this->uri->uri_string()
)));
}
I think you need to redirect the parent frame (i.e. _top) rather than the iFrame itself?
The way I do it is set up an INDEX.PHP file with the following
//if user is logged in and session is valid.
if ($fbme){
//fql query example using legacy method call and passing
parameter
try{
$fql = "select name, hometown_location, sex,
pic_square from user where uid=" .
$uid;
$param = array(
'method' => 'fql.query',
'query' => $fql,
'callback' => 'http://apps.facebook.com/yoursite/'
);
$fqlResult = $facebook->api($param);
}
catch(Exception $o){
d($o);
}
}
Then point your canvas url to http://yoursite.com/INDEX.php
The callback url in the above code which will be in INDEX.PHP sets where to look after permissions are granted.
FBMain.php looks like this
//set application urls here
$fbconfig['http://www.yoursite.com/iframeapp/YOURMAINPAGE.php/']
= "http://www.tyoursite.com/YOURMAINPAGE.php/";
$fbconfig['http://apps.facebook.com/CANVASBASEURL']
= "http://apps.facebook.com/CANVASBASEURL";
$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'
)
);
$fbme = null;
if (!$session) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
else {
try {
$uid = $facebook->getUser();
$fbme = $facebook->api('/me');
} catch (FacebookApiException $e) {
echo "<script type='text/javascript'>top.location.href
= '$loginUrl';";
exit;
}
}
function d($d){
echo '<pre>';
print_r($d);
echo '</pre>';
} ?>
Hope its a little clearer. It took me a while to figure it out, but I got there, thought I would help.

Categories