I am new in fbdevelopment, so I downloaded php sdk 4 and extated in component with name facebook-sdk and then I configured in main.php
as
'import'=>array(
'application.models.*',
'application.components.*',
'application.components.facebook-sdk.*',
Then in my site controller I want to call:
$session = new FacebookSession($_POST['accessToken']);
But even thought I have an access token, it returns:
include(FacebookSession.php): failed to open stream: No such file or directory
where we have to configuration php-sdk 4 in yii
I had this problem like you and it took me hours to debug. Finally, I found that I missed the namespace "Facebook\" before the class name.
Here is my code it works well:
require_once 'facebook-php-sdk/autoload.php';
require_once 'facebook-php-sdk/src/Facebook/FacebookSession.php';
require_once 'facebook-php-sdk/src/Facebook/FacebookRequest.php';
require_once 'facebook-php-sdk/src/Facebook/GraphObject.php';
require_once 'facebook-php-sdk/src/Facebook/GraphUser.php';
require_once 'facebook-php-sdk/src/Facebook/FacebookSDKException.php';
require_once 'facebook-php-sdk/src/Facebook/FacebookRequestException.php';
Facebook\FacebookSession::setDefaultApplication(FACEBOOK_APP_ID, FACEBOOK_APP_SECRET);
$helper = new Facebook\FacebookRedirectLoginHelper('http://mywebsite.com');
try {
$session = $helper->getSessionFromRedirect();
} catch(Facebook\FacebookRequestException $ex) {
// When Facebook returns an error
} catch(\Exception $ex) {
// When validation fails or other local issues
}
if ( isset($session) ) {
// Logged in
$me = (new Facebook\FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
var_dump($me);
}
else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl); exit;
}
Related
I downloaded Phalcon from official website
I copied php_phalcon.dll file to my xampp's php/ext directory
Edited the php.ini file located at D:\xampp\php\php.ini. and add there line extension=php_phalcon.dll at the end of the file.
I Restarted apache server and computer several times.
When I write phpinfo() to my code it seems phalcon was installed
Unfortunatelly Whan I try to run some code like
<?php
try {
// Autoloader
$loader = new \Phalcon\Loader();
$loader->registerDirs([
'../app/controllers/',
'../app/models/'
]);
$loader->register();
// Dependency Injection
$di = new \Phalcon\DI\FactoryDefault();
$di->set('view', function() {
$view = new \Phalcon\Mvc\View();
$view->setViewsDir('../app/views');
return $view;
});
// Deploy the App
$app = new \Phalcon\Mvc\Application($di);
echo $app->handle()->getContent();
} catch(\Phalcon\Exception $e) {
echo $e->getMessage();
}
?>
I get this error
Fatal error: Uncaught Error: Class 'Phalcon\Loader' not found in D:\xampp\htdocs\php-learning\public\index.php:4 Stack trace: #0 {main} thrown in D:\xampp\htdocs\php-learning\public\index.php on line 4
I also tried to follow the steps from tutorial on Phalcon ofical website where code looks somehow like this
<?php
use Phalcon\Di\FactoryDefault;
use Phalcon\Loader;
use Phalcon\Mvc\View;
use Phalcon\Mvc\Application;
use Phalcon\Url;
// Define some absolute path constants to aid in locating resources
define('BASE_PATH', dirname(__DIR__));
define('APP_PATH', BASE_PATH . '/app');
// Register an autoloader
$loader = new Loader();
$loader->registerDirs(
[
APP_PATH . '/controllers/',
APP_PATH . '/models/',
]
);
$loader->register();
$container = new FactoryDefault();
$container->set(
'view',
function () {
$view = new View();
$view->setViewsDir(APP_PATH . '/views/');
return $view;
}
);
$container->set(
'url',
function () {
$url = new Url();
$url->setBaseUri('/');
return $url;
}
);
$application = new Application($container);
try {
// Handle the request
$response = $application->handle(
$_SERVER["REQUEST_URI"]
);
$response->send();
} catch (\Exception $e) {
echo 'Exception: ', $e->getMessage();
}
But didnĀ“t help. What I am doing wrong?
You have Phalcon installed successfully. However the namespace should be changed:
Moved Phalcon\Loader to Phalcon\Autoload\Loader #15797
please refer to the change log
enter link description here
I'm making a PHP application running on Google App Engine, and I'm trying to implement a Facebook Login.
After accessing to the login webpage, I am redirected to Facebook, and after I accept to log in for the application and I get redirected, I get the error "Error : Option 10065 is not supported by this curl implementation."
I have curl_lite enabled, and I'm running it on localhost for the time being. Here's my code:
<?php
session_start(); //Session should be active
#region Settings
$app_id = 'xxxxxxx'; //Facebook App ID
$app_secret = 'yyyyyyy'; //Facebook App Secret
$required_scope = 'public_profile'; //Permissions required
$redirect_url = 'http://localhost:8080/signup_fb.php'; //FB redirects to this page with a code
#endregion
#region Imports
//include autoload.php from SDK folder, just point to the file like this:
require_once "../libraries/facebook-php-sdk-v4-4.0-dev/autoload.php";
//import required class to the current scope
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookRequestException;
use Facebook\GraphUser;
use Facebook\FacebookRedirectLoginHelper;
#endregion
FacebookSession::setDefaultApplication($app_id , $app_secret);
$helper = new FacebookRedirectLoginHelper($redirect_url);
//try to get current user session
try {
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex) {
die(" Error : " . $ex->getMessage());
} catch(\Exception $ex) {
die(" Error : " . $ex->getMessage());
}
if ($session){ //if we have the FB session
$user_profile = (new FacebookRequest($session, 'GET', '/me'))->execute()->getGraphObject(GraphUser::className());
//do stuff below, save user info to database etc.
echo $user_profile->getProperty('name');
}else{
//display login url
$login_url = $helper->getLoginUrl( array( 'scope' => $required_scope ) );
echo 'Login with Facebook';
}
Thank you for your answers :)
It's understandable that with curl_lite enabled (curl interface backed by urlfetch and with fewer features) you would see such an issue. Check the docs as another commenter mentioned, and you should definitely be using the real curl if you intend to run such specific advanced functionality with your box's devserver as the client to facebook's login flow.
after various tries, i decided to write here my problem with FB PHP login with SDK 4.0.0.
The problem is that I cannot retrieve the session, and I don't know why.
My code (placed in a file named "social.php") is the follow:
session_start();
define('fbsdk', "/fbsdk/src/Facebook/");
require __DIR__ . '/fbsdk/autoload.php';
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphUser;
if(isset($_GET['login'])){
if($_GET['login'] == "fb"){
$appid = "677652872357474";
$appsecret = "******";
FacebookSession::setDefaultApplication($appid, $appsecret);
$helper = new FacebookRedirectLoginHelper('http://localhost/ffideasbox/');
try{
$session = $helper->getSessionFromRedirect();
} catch(FacebookRequestException $ex){
print_r($ex);
} catch(Exception $ex){
print_r($ex);
}
if(isset($session)){
echo "Session defined: ".$session;
} else {
echo "Session not defined";
}
} elseif($_GET['login'] == "tw"){
} elseif($_GET['login'] == "gp"){
} else {
...
}
} else {
....
}
?>
If I go to the documentation page of Session (https://developers.facebook.com/docs/php/FacebookSession/4.0.0), it says me that I can get session by:
// If you already have a valid access token:
$session = new FacebookSession('access-token');
// If you're making app-level requests:
$session = FacebookSession::newAppSession();
Eventually, which is the access token? And how can I get it?
Then, after have posted here, I added before the session TRY block, this other one:
$session = FacebookSession::newAppSession();
try{
$session->validate();
} catch(FacebookRequestException $ex){
echo $ex->getMessage();
} catch(\Exception $ex){
echo $ex->getMessage();
}
But I still get the same message: "Session not defined" (wrote by me in the session IF block).
Could someone tell me more about the FBSDK? I read the documentation but I don't understand much.
Thank you all.
EDIT: I add also the "map" of the sdk folder
-mainfolder
|-fbsdk
|-autoload.php
|-src
|-Facebook
|-All the complete facebook sdk
After have looked for examples and more, I looked that I didn't had added the piece
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
in the isset($session) else. This because I didn't understand why it was needed and when the session is not defined, I didn't redirect anywhere.
if(isset($session)){
echo "Session defined: ".$session;
} else {
$loginUrl = $helper->getLoginUrl();
header("Location: ".$loginUrl);
}
I am trying to integrate facebook SDK with a codeigniter website.
I have used composer to install the files with the composer.json file with the code provided in the facebook documentation. I have looked at multiple other potentially similar problems on stackoverflow and around and came across examples like this:
http://metah.ch/blog/2014/05/facebook-sdk-4-0-0-for-php-a-working-sample-to-get-started/#post-937
CodeIgniter Facebook SDK 4 with Composer
Error: Class 'Facebook\FacebookSession' not found with the facebook PHP SDK
The error I recieve is:
Fatal error: Uncaught exception 'Facebook\FacebookSDKException' with message 'You must provide or set a default application id.' in ....
I have the following code in my controller:
public function login()
{
$fb_config = array(
'appId' => 'xxxxx',
'secret' => 'xxxx'
);
$this->load->library('facebook', $fb_config);
$user = $this->facebook->getUser();
if ($user) {
try {
$data['user_profile'] = $this->facebook
->api('/me');
} catch (FacebookApiException $e) {
$user = null;
}
}
if ($user) {
$data['logout_url'] = $this->facebook
->getLogoutUrl();
} else {
$data['login_url'] = $this->facebook
->getLoginUrl();
}
$this->load->view('templates/header');
$this->load->view('user/login',$data);
}
I have tried adding require and use and session start at the top of the class but the issue persists.
I've a problem with facebook sdk 4.0
After I clear session/cookies it works fine. But sometimes, and I cannot determine when, if I go to the app, it starts an endless redirect loop!
I've put all my code on git, since documentation doesn't provide exact ansswers:
https://github.com/sandrodz/facebook-canvas-app-sample-sdk-4.0/blob/master/index.php
<?php
// Working canvas APP, FB SDK 4.0
session_start();
// Load SDK Assets
// Minimum required
require_once 'Facebook/FacebookSession.php';
require_once 'Facebook/FacebookRequest.php';
require_once 'Facebook/FacebookResponse.php';
require_once 'Facebook/FacebookSDKException.php';
require_once 'Facebook/FacebookCanvasLoginHelper.php';
require_once 'Facebook/GraphObject.php';
require_once 'Facebook/GraphUser.php';
require_once 'Facebook/GraphSessionInfo.php';
require_once 'Facebook/HttpClients/FacebookHttpable.php';
require_once 'Facebook/HttpClients/FacebookCurl.php';
require_once 'Facebook/HttpClients/FacebookCurlHttpClient.php';
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Faceboob\FacebookSDKException;
use Facebook\FacebookCanvasLoginHelper;
use Facebook\GraphObject;
use Facebook\GraphUser;
use Facebook\GraphSessionInfo;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookCurlHttpClient;
// Facebook APP keys
FacebookSession::setDefaultApplication('XXX','XXXXX');
// Helper for fb canvas authentication
$helper = new FacebookCanvasLoginHelper();
// see if $_SESSION exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
// create new fb session from saved fb_token
$session = new FacebookSession($_SESSION['fb_token']);
// validate the fb_token to make sure it's still valid
try
{
if (!$session->validate())
{
$session = null;
}
}
catch (Exception $e)
{
// catch any exceptions
$session = null;
}
}
else
{
// no $_SESSION exists
try
{
// create fb session
$session = $helper->getSession();
}
catch(FacebookRequestException $ex)
{
// When Facebook returns an error
print_r($ex);
}
catch(\Exception $ex)
{
// When validation fails or other local issues
print_r($ex);
}
}
// check if 1 of the 2 methods above set $session
if (isset($session))
{
// Lets save fb_token for later authentication through saved $_SESSION
$_SESSION['fb_token'] = $session->getToken();
// Logged in
$fb_me = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject();
// We can get some info about the user
$fb_location_name = $fb_me->getProperty('location')->getProperty('name');
$fb_email = $fb_me->getProperty('email');
$fb_uuid = $fb_me->getProperty('id');
}
else
{
// We use javascript because of facebook bug https://developers.facebook.com/bugs/722275367815777
// Fix from here: http://stackoverflow.com/a/23685616/796443
// IF bug is fixed this line won't be needed, as app will ask for permissions onload without JS redirect.
$oauthJS = "window.top.location = 'https://www.facebook.com/dialog/oauth?client_id=1488670511365707&redirect_uri=https://apps.facebook.com/usaidgeorgia/&scope=user_location,email';";
}
?>
I went ahead to debug line by line, and these are my findings:
// see if a existing session exists
if (isset($_SESSION) && isset($_SESSION['fb_token']))
{
echo '$_SESSION and $_SESSION["fb_token"] are set';
// create new session from saved access_token
$session = new FacebookSession($_SESSION['fb_token']);
// validate the access_token to make sure it's still valid
try
{
if (!$session->validate())
{
$session = null;
echo 'access_token is not valid';
}
echo 'access_token is valid';
}
catch (Exception $e)
{
// catch any exceptions
$session = null;
echo 'something error happened ' . $e;
}
}
I get error:
$_SESSION and $_SESSION["fb_token"] are setsomething error happened exception 'Facebook\FacebookSDKException' with message 'Session has expired, or is not valid for this app.' in /home2/nakaidze/public_html/mesamoqalaqo_app/Facebook/FacebookSession.php:247 Stack trace: #0 /home2/nakaidze/public_html/mesamoqalaqo_app/Facebook/FacebookSession.php(221): Facebook\FacebookSession::validateSessionInfo(Object(Facebook\GraphSessionInfo), '148867051136570...') #1 /home2/nakaidze/public_html/mesamoqalaqo_app/user-functions.php(56): Facebook\FacebookSession->validate() #2 /home2/nakaidze/public_html/mesamoqalaqo_app/index.php(2): require('/home2/nakaidze...') #3 {main}
The access token you're using in $_SESSION['fb_token'] has expired. By default the access token returned by Facebook lasts for 2 hours and then it expires.
After you get the FacebookSession for the first time, you need to extend the access token it returned and save it in your $_SESSION['fb_token']:
$session = $helper->getSession();
$accessToken = $helper->getAccessToken();
$longLivedAccessToken = $accessToken->extend();
$_SESSION['fb_token'] = (string) $longLivedAccessToken;
Also, when you validate the access token with validate(), it will throw if it's not valid:
// validate the access_token to make sure it's still valid
try
{
$session->validate();
echo 'access_token is valid';
}
catch (FacebookSDKException $e)
{
$session = null;
echo 'Access token is no longer valid, need to get a new token';
}
This might help clarify info about Facebook access tokens.