Hey guys can I have a problem relating to Graph API, I want to get user profile info but it seems I can't generate a session, I don't know what is the problem. Here is my code.....please help me guys

session_start(); 
    require_once "facebook/autoload.php";
    use Facebook\FacebookSession;
    use Facebook\FacebookRequest;
    use Facebook\GraphUser;
    use Facebook\FacebookRequestException;
    use Facebook\FacebookRedirectLoginHelper;

    $api_key = "452545828240757";
    $api_secret = "30be684049158093c0de24025a4fec69";
    $redirect_login_url = "http://localhost/fullsoka/index.php";

    FacebookSession::setDefaultApplication($api_key,$api_secret);

    $helper=new FacebookRedirectLoginHelper($redirect_login_url);
    try{
        $session = $helper->getSessionFromRedirect();
    } 
    catch (Exception $ex) 
    {

    }

    if($session)
    {
        echo "Successfully login......";
    }

Recommended Answers

All 11 Replies

Add echo $ex->getMessage(); to the catch statement:

catch(Exception $ex)
{
    echo $ex->getMessage();
}

If something is wrong you should get some information that will help you to debug.

I tried that but nothing displayed. It's more than a week now I didn't get the solution. Please help me

Your code works fine for me with my app settings, try to add error_reporting(-1); to the top of your script and see if it returns any error, warning or notice.

Does your meaning of 'getting user infomation' refered to the basic information? if it is basic user info, then you can direct get data from graph.facebook.com/username and json_decode() the data.

error_reporting(-1)

Your code works fine for me with my app settings, try to add error_reporting(-1); to the top of your script and see if it returns any error, warning or notice.

It doesn't return any error, it's just nothing displays. Please can you give an example on that, because I don't know where am I wrong

What about App settings? When testing in localhost you don't have to define a domain. If you do so, the login process will fail.

A part that, when you click the login link do you reach Facebook at least? The problem starts when you are redirected to this link?

http://localhost/fullsoka/index.php

Quoted Text Here

What about App settings? When testing in localhost you don't have to define a domain. If you do so, the login process will fail.

A part that, when you click the login link do you reach Facebook at least? The problem starts when you are redirected to this link?

Yes I'm testing in the local host, but this is my App settings
Capture.PNGCapture.PNG

What about App settings? When testing in localhost you don't have to define a domain. If you do so, the login process will fail.

A part that, when you click the login link do you reach Facebook at least? The problem starts when you are redirected to this link?

Or should I include at the top all files relating to these?

use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookRequestException;
use Facebook\FacebookRedirectLoginHelper;

Ok, go on Facebook > My Apps > happ > Settings click on Add Platform, choose Website and set Site URL to:

http://localhost/

In case you're using a port different from 80, then add it, for example:

http://localhost:8000/

Once you have saved the changes your script should work fine.

Ok, go on Facebook > My Apps > happ > Settings click on Add Platform, choose Website and set Site URL to:

http://localhost/
In case you're using a port different from 80, then add it, for example:

http://localhost:8000/
Once you have saved the changes your script should work fine.

After doing the above settings, it returns the following results

"Facebook (post) request error: An active access token must be used to query information about the current user."

It happens if you browse to another page and do not save the access token you got from the login request. If you're not using the Javascript SDK along with PHP, then you have to save the token in server side, for example in PHP Session:

# after login
$_SESSION['access_token'] = $session->getToken();

So when you browse to another page and load the request you can do:

$session = new FacebookSession($_SESSION['access_token']);

$user_profile = (new FacebookRequest($session, 'GET', '/me'))
                ->execute()
                ->getGraphObject(GraphUser::className());

echo "Name: " . $user_profile->getName();

Note that for POST request you need specific permissions, docs:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.