I have searched Google for Libraries for Facebook, and have found this one: http://www.haughin.com/code/facebook/,
but it seems a bit outdated.
I wanted something for this one: https://github.com/facebook/php-sdk/
I have written my own wrapper for it meanwhile, but it seems like I'm having some issues with $_REQUEST['...']
You can use Official PHP Facebook SDK in Codeigniter easily. The only problem is Facebook SDK needs $_REGISTER and in Codeigniter you don't have it.(Because of the mod_rewrite)
Here's the little solution:
1- This is for the $_REGISTER problem. Use this before loading Facebook class:
parse_str( $_SERVER['QUERY_STRING'], $_REQUEST );
2- Facebook SDK has 2 files. Put them into one file and save it to your application/helper folder as facebook_helper.php. Then loading and using is such an ease like this:
$this->load->helper( 'facebook' );
$facebook = new Facebook( array( 'appId' => 'XXX', 'secret' => 'xxx' ) );
// ... The rest is not different. You can read Facebook SDK examples
So here's a trick I used to use facebook PHP sdk with my CodeIgniter app. From the SDK code, take Facebook.php and take out the FacebookApiException class, and put it in a new file called FacebookApiException.php. Now, I put facebook.php and FacebookApiException.php into the models folder, and used them as regular models.
Here is the code I used for authenticating users and providing access to an application via Facebook.
function facebook_login(){
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'XXXXXXXXXXXXXXXXX',
'secret' => 'XXXXXXXXXXXXXXXXX',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getSession();
if(!empty($session)) {
# Active session, let's try getting the user info
try{
$uid = $facebook->getUser();
$param = array(
'method' => 'users.getinfo',
'uids' => $uid,
'fields' => 'uid, username, name, profile_url, pic_big',
'callback' => ''
);
$user = $facebook->api($param);
} catch (Exception $e){
$url = $facebook->getLoginUrl(array(
'req_perms' => 'user_about_me, email, status_update, publish_stream, user_photos',
'next' => site_url() . 'user/facebook_login',
'cancel' => site_url()
));
redirect($url);
}
if(!empty($user)){
# User info ok?
print_r($user);
// Add user oauth token and info to DB, and then redirect to some controller in your application.
redirect('/'); // redirect to homepage
} else {
# For testing purposes, if there was an error, let's kill the script
die("There was an error.");
}
} else {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
'req_perms' => 'user_about_me,email,status_update,publish_stream,user_photos',
'next' => site_url() . 'user/facebook_login',
'cancel' => site_url()
));
redirect($url);
}
}
Hope this helps.
You can handle it in clear way by writing a small library by inheriting Facebook SDK class. Please follow my post on http://www.betterhelpworld.com/codeigniter/how-to-use-facebook-php-sdk-v-3-0-0-with-codeigniter to get it working.
There is also Facebook-Ignited.
Related
I have been reading the Facebook documentation and I must be missing something, as I just cant understand how to get the access token for a page without actually logging in first. I am trying to create a PHP function using the PHP facebook API so that when I add new stories or tutorials on my site, my site's apps can then automatically post as the page a blurb about them on myy facebook page.
I have this function working but only when I get the access token from the Graph API Explorer, though the access tokens expire in about an hour. I can't seem to figure out how to programatically obtain the access_token for the page and query for a new one each time from within my PHP scripts so they don't expire and do not require user interaction.
function post_to_facebook($title, $message, $link, $picture) {
require '../facebook/src/facebook.php';
$page_token = 'xxx';
$page_id = 'xx';
$facebook = new Facebook(array(
'appId' => '<app_id>',
'secret' => '<app_secret>',
'cookie' => false,
));
$facebook->setAccessToken($page_token);
try {
$ret_obj = $facebook->api('/'.$page_id.'/feed', 'POST', array(
'caption' => $title,
'link' => $link,
'message' => $message,
'picture' => $picture
));
} catch(FacebookApiException $e) {
error_log($e->getType());
error_log($e->getMessage());
return false;
}
return true;
}
Can someone explain how I can go about retrieving the access token without manually having to look it up via graph api explorer or having a user login?
It's not possible.
The only correct (and legal) way to achieve the access token is with user interaction (through login process).
I am using the latest PHP-SDK(3.11) and i have issues when users come on my app for the first time. The application make infinite loops.
When the user have to give permissions to the application, he is redirected to :
https://www.facebook.com/connect/uiserver.php?app_id=**myappId**&method=permissions.request&display=page&next=http%3A%2F%2Fapps.facebook.com%2F**myApp**%2F&response_type=code&state=**theSate**&canvas=1&perms=user_birthday%2Cuser_location%2Cuser_work_history%2Cuser_about_me%2Cuser_hometown
and when he accept i have the following link returned :
http://apps.facebook.com/**myApp**/?error_reason=user_denied&error=access_denied&error_description=***The+user+denied+your+request.***&state=**theSate**#_
i don't understand why the access is denied when the user click on "allow".
if ($this->fbUser) {
.... Do Somthing
} else {
$this->loginUrl = $this->fb->facebook->getLoginUrl(array(
'scope' => implode(',', sfConfig::get('app_facebook_perms')
), 'next' => 'http://apps.facebook.com'. sfConfig::get('app_facebook_app_url')));
$this->logMessage($this->loginUrl, 'info');
sfConfig::set('sf_escaping_strategy', false);
}
<script type='text/javascript'>
top.location.href = "echo $this->loginUrl ";
</script>
Try something like this, since you need to store the access token, one way or another. Hard to know what you are doing (or not doing) from that snippet.
<?php
# We require the library
require("facebook.php");
# Creating the facebook object
$facebook = new Facebook(array(
'appId' => 'APP_ID_HERE',
'secret' => 'APP_SECRET_HERE',
'cookie' => true
));
# Let's see if we have an active session
$session = $facebook->getUser();
if(empty($session)) {
# There's no active session, let's generate one
$url = $facebook->getLoginUrl(array(
"response_type"=>"token", //Can also be "code" if you need to
"scope" => 'email,user_birthday,status_update,publish_stream,user_photos,user_videos' ,
"redirect_uri"=> "http://test.com" //Your app callback url
));
header("Location: $url");
exit;
}
// user is logged in
I am having problems with Facebook connect.
Once I have the connect with Facebook button how do i retrieve the info with php?
There are loads of examples in developer.facebook.com but I couldn't get any of them to work.
Is it POST or GET or what? (I'm kinda new to php... )
Thanks.
DEMO: http://so.devilmaycode.it/facebook-login-connect-help-php/
Setup new App here
Download the PHP SDK here
Copy & Paste the code you'll find here into a file called index.php
Edit the first lines of the code above and provide the needed informations like below:
require 'sdk/facebook.php'; //the path to the downloaded PHP SDK
$facebook = new Facebook(array(
'appId' => '105810212821284', //App ID you find once created the app
'secret' => '3d6fdaa377cd4ca9...', //Secret Key you find once created the app
'cookie' => true,
));
you have done ;)
Application Settings:
Web Site -> Site URL -> http://so.devilmaycode.it/facebook/
Web Site -> Site Domain -> so.devilmaycode.it
Facebook Integration -> Canvas URL -> http://so.devilmaycode.it/facebook/
Requiriment
PHP5
CURL Lib
Check if you have the requirement
<? echo phpinfo(); ?>
Demo Source
http://pastebin.com/KyQ3CHV0
Use https://github.com/facebook/php-sdk/
Example
if ($session) {
try {
$uid = $facebook->getUser();
$me = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
}
}
Display the data
echo $me['email']
http://developers.facebook.com/docs/reference/api/user/
More info about Graph API
To get the email or aditional data from users, you need to request it first(if not you only can get the basic data, like name / picture or only the public data), check the api facebook file in the function called "getLoginUrl".
'api_key' => $this->getAppId(),
'cancel_url' => $currentUrl,
'display' => 'page',
'req_perms' => 'email', <------------- email
'fbconnect' => 1,
'next' => $currentUrl,
'return_session' => 1,
'session_version' => 3,
'v' => '1.0',
I need my app to request the offline_access permission (detailed here), but as ever I'm baffled by the Facebook documentations.
It says I need a comma separated list of my permission demands, like 'publish_stream,offline_access' etc
where do i put this list in the interface below????
It's used with the API, as follows
$facebook = new Facebook(array(
'appId' => FACEBOOK_APP_ID,
'secret' => FACEBOOK_SECRET,
'cookie' => false,
));
$facebook_login_url = $facebook->getLoginUrl(array(
'next' => '',
'cancel_url' => '',
'req_perms' => 'email,publish_stream,status_update'
));
Where $facebook_login_url is the URL tha the user needs to follow to grant you access.
Does that help?
You can't do this with that interface. You need to set scope parameter when redirecting user to facebook.
x=y&scope=email,user_about_me,user_birthday,user_photos,publish_stream,offline_access
First define which permissions you will need:
$par['req_perms'] = "friends_about_me,friends_education_history,friends_likes,friends_interests,friends_location,friends_religion_politics,
friends_work_history,publish_stream,friends_activities,friends_events,
friends_hometown,friends_location,user_interests,user_likes,user_events,
user_about_me,user_status,user_work_history,read_requests,read_stream,offline_access,user_religion_politics,email,user_groups";
$loginUrl = $facebook->getLoginUrl($par);
Then, check if the user has already subscribed to app:
$session = $facebook->getSession();
if ( is_null($session) ) {
// no he is not
//send him to permissions page
header( "Location: $loginUrl" );
}
else {
//yes, he is already subscribed, or subscribed just now
echo "<p>everything is ok";
// write your code here
}
I'm working on my new project, Woobook. I used the Old REST API until now but i should learn to work with latest one (with OAuth). First i want to get extended permissions but the attached code redirect me to a wrong page (instead of permissions dialog directly).
My app link: http://apps.facebook.com/woobook/
and the source code:
<?php
// Facebook API inc
require_once "inc/facebook.php";
// API init
$cid = "162513840463126";
$asi = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx";
$facebook = new Facebook(array(
'appId' => $cid,
'secret' => $asi,
'cookie' => true,
));
// Session
$session = $facebook->getSession();
if(!$facebook->getUser()) {
header("Location:".$facebook->getLoginUrl(array("next" => "http://apps.facebook.com/woobook/", "canvas" => 1, "req_perms" => "user_status,publish_stream,user_photos"))."");
exit;
}
?>
You're using an iframe app, which means you need to do this a little differently.
See my answer here