Connect with facebook using PHP SDK in codeigniter - php

Im implementing facebook's PHP SDK for authentication here. I've searched over net and found lots of tutorial on that. like Using Facebook PHP SDK 3 with CodeIgniter
i've modified base_facebook.php to return www.skillpaper.com/auth/fb_register in
$this->facebook->getLoginUrl()
when i click "Connect with Facebook" in login modal and facebook auth popup appears its successfully redirect to my given url. i.e www.skillpaper.com/auth/fb_register
and my code is like in auth controller:
public function fb_register(){
$userId = $this->facebook->getUser();
if($userId != 0){
// Get user's data and print it
$user = $this->facebook->api('/me');
var_dump($user);
}else{ echo "No user";}
}
i'm getting "No user" every time!
What am i missing? I don't want to use any JS API for this. any suggestion?
Thanks in advance.

Please Download the Facebook PHP SDK From https://developers.facebook.com/docs/reference/php/
Now Put src folder into application/controller folder.
Call into controller any function like this :-
require 'src/facebook.php';
$appId = FBAPPID;
$secretkey = FBSECRETKEY;
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $secretkey,
));
$fb['facebook']=$facebook;
$facebook_user = $facebook->getUser();
$fb['facebook_user']=$facebook_user;
if ($facebook_user) {
try {
$user_profile = $facebook->api('/me');
//$fb['logoutUrl'] = $facebook->getLogoutUrl();
} catch (FacebookApiException $e) {
error_log($e);
$facebook_user = null;
$this->load->view('Your Page');
}
}else {
$data['loginUrl'] = $facebook->getLoginUrl(array(
'scope' => 'user_about_me,email,user_birthday,friends_birthday,publish_stream,manage_pages,offline_access'
));
$this->load->view('Your Page');
}

Related

Facebook login not showing userdata

I have tried to create a facebook login, but it seems after it redirects it to the my controller, I don't know how to fetch the data. I know I need to call the https://www.facebook.com/dialog/oauth? part, but I don't know how.
What I did in my view:
require_once(__DIR__."/facebook/facebook.php");
$facebook = new Facebook(array( 'appId' => 'MY_APP_ID', 'secret' => 'MY_APP_SECRET',));
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
}
$params = array( 'scope' => 'email', 'redirect_uri' => 'http://mywebsite.com/some/directory/loginusingFacebook', );
$loginUrl = $facebook->getLoginUrl($params);
Login with facebook
I don't know how to call https://www.facebook.com/dialog/oauth? so what I did is after I get in the loginusingFacebook function I try redirecting the url to this:
My Controller after I click the login button:
redirect(https://www.facebook.com/dialog/oauth?client_id="MY_APP_ID"&redirect_uri="http://mywebsite.com/some/directory/anotherfunction"&scope="user_birthday,email");
But I'm having errors with it. How do I pass the user data to loginusingFacebook the first time around with out having to redirect it to the oauth facebook page?
I was hoping that I could get it to go to loginusingFacebook or anotherfunction function with the authentication data and userdata so I could echo it out and save it in my database.
I just needed to remove the quotation mark on the redirect_uri.

Facebook Login Doesn't Work

I am trying to add Facebook login to my web site.
What I have done?
Created APP on Facebook as Website with Facebook Login
Sandbox mode is off
APP is set to work on localhost.
What is the problem?
I keep getting $user variable's value as 0
example.php in Facebook PHP SDK works just fine!
I can see my APP when I visit Facebook -> Privacy Settings -> Apps. No matter if I login with my login.php or example.php of Facebook PHP SDK, the app seems to be added just the same. However while example.php can retrieve data from facebook, login.php can't.
I copy and paste the codes from example.php to my login.php page (please check the code below).
Here is my code (login.php);
require_once('system/api/facebook/facebook.php');
$facebook = new Facebook(array(
'appId' => '123456',
'secret' => 'abcde12345',
));
$user = $facebook->getUser();
if ($user) {
try {
$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();
}
if (!$user) echo 'Login';
echo '<pre>';
var_dump($user);
echo '</pre>';
Can someone please tell me what is the difference between my login.php and example.php? I also Googled and searched here without finding any info which was helpful.

Unable to post message to my facebook using php sdk

Fatal error: Uncaught OAuthException: An active access token must be used to query information about the current user. thrown in C:\xampp\htdocs\fyp\plugin\facebook-sdk\src\base_facebook.php on line 1106
The problem is like mentioned above. I have spend sometime on searching the solution but that still not work after i modified the code. I notice this there is website to get one
https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=116122545078207&
redirect_uri=http%3A%2F%2Fxyz.com&
scope=user_photos,email,user_birthday,user_online_presence
Can i include it into my script? so that i don't have to going this url everytime
.And how to include it into my phpscript so that the problem can be fixed?
Thank you for any kind of help.
<?require 'plugin/facebook-sdk/src/facebook.php';
$session='123456';
$facebook = new Facebook(array(
'appId' => '1234567',
'secret' => '123456789',
'cookie' => true,
));
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
// login or logout url will be needed depending on current user state.
if ($me) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
if ($session) {
// We have a valid FB session, so we can use 'me'
$result = $facebook->api('/me/feed','post',array('message' => 'testmessage'));
} elseif( isset($_SESSION['user_id']) ) {
$result = $facebook->api("/{$_SESSION['user_id']}/feed",'post',array('message' => 'testmessage'));
}
?>
Edit Scope ,
scope=user_photos,email,user_birthday,user_online_presence,publish_stream
& visit this link again
https://graph.facebook.com/oauth/authorize?
type=user_agent&
client_id=116122545078207&
redirect_uri=http%3A%2F%2Fxyz.com&
scope=user_photos,email,user_birthday,user_online_presence,publish_stream

Getting facebook access token

I'm trying to get the access token of my app using graph api, by requesting the following string:
https://graph.facebook.com/oauth/authorize?type=user_agent&client_id=app_id&redirect_uri=canvas_page&scope=read_friendlists
This redirects to: canvas_page/#access_token=xxx&expires_in=4124&code=A...
But i am not sure how i can parse this with PHP, so that i eventually can make a json request to get the friendlist.
I hope you may be able to guide me :)
Thank you
Are you using the Facebook PHP sdk?
https://github.com/facebook/php-sdk
You can use it like this:
$facebook = new Facebook(array(
'appId' => 'your_app_id',
'secret' => 'your_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) {
$logoutUrl = $facebook->getLogoutUrl();
} else {
$loginUrl = $facebook->getLoginUrl();
}
You can use the $loginUrl to make a link where the user needs to click that will authorize your application if they haven't done so already
Then you can make calls to the graph api like this:
$friends = $facebook->api('/user_id/friends');
You can use the following code below. It worked for me.
First, you need to download Facebook SDK from here https://github.com/facebook/php-sdk
function facebook_oauth_init($facebook_app_id, $facebook_app_secret, $cookie = false)
{
$facebook = new Facebook(array("appId" => $facebook_app_id, "secret" => $facebook_app_secret,
"cookie" => $cookie));
return $facebook;
}
$fb_app_id = "YOUR_APP_ID";
$fb_app_secret = "YOUR_APP_SECRET";
$fb_user_id = "YOUR_USER_ID";
$fb_access_token = "YOUR_GIVEN_ACCESS_TOKEN";
$facebook = facebook_oauth_init($fb_app_id, $fb_app_secret);
$accounts = $facebook->api('/' . $fb_user_id . '/accounts', 'get', array('access_token'=>$fb_access_token));
$accounts = $accounts['data'];
$access_token_2
foreach($accounts as $acc)
{
if($fb_page_id == $acc['id'])
{
$access_token_2 = $acc['access_token'];
break;
}
}
"$access_token_2" should be what you are looking for.
Hope this helps.
Muhammad.
Using this "api" method (on php sdk), you don't need to especify the access token, it will be generated and setted into Facebook object.
Anyway, you can to use $facebook->getAccessToken()
http://developers.facebook.com/docs/reference/php/facebook-getAccessToken/

How to use facebook connect on mobile web application using the OAuth Dialog

Now I'm developing a mobile web application using facebook connect, but I am having problems with the OAuth Dialog. I use the following facebook documents for my reference:
http://developers.facebook.com/docs/guides/mobile/#web
I choose to use the OAuth Dialog instead of the Login Button because mobile browsers do not recognize the FBML (I use the Blackberry browser on testing). The OAuth Dialog also lets me add a list of permissions within the parameters of scope. But the problem is when I've logged in using the OAuth Dialog, the parameter $me wasn't recognized so that the login button still appears and doesn't not change to the logout button.
Here is an example of my code:
$facebook = new Facebook(array(
'appId' => 'xxxxxxxxxxxxxxxx',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
$accessToken = $facebook->getAccessToken();
} catch (FacebookApiException $e) {
error_log($e);
}
}
if ($me) {
echo "<img src=\"images/logoutFB.gif\">";
} else {
echo "<img src=\"images/loginFB.gif\">";
}
Is the parameter $me can't be used if I use the OAuth Dialog to connects to facebook?
So how do I know that I am already logged into facebook or not if I use the OAuth Dialog?
Please help if you guys have the solution.
Looks like you need to use the updated PHP-SDK
getSession() has been replaced by getUser():
require './src/facebook.php';
$facebook = new Facebook(array(
'appId' => '135669679827333',
'secret' => 'xxxxxxxxxxxxxxxx',
));
$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) {
$user = null;
}
}

Categories