Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3804414
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T14:30:52+00:00 2026-05-19T14:30:52+00:00

I am new to Facebook and I am trying to write my first app.

  • 0

I am new to Facebook and I am trying to write my first app.

I tried the following basic code, and I got to the ‘An error occurred’ part
I understand from that that I got the session but not the user details…
can you please assist me with what went wrong? thanks a lot :

<?php

require 'facebook.php';

//create application instance
$facebook = new Facebook(array(
'appId' => 'XXXXXX',
'secret' => 'XXXXXX',
'cookie' => true,
));

$session = $facebook->getSession();

if (!empty($session))
{
    try {
        $uid = $facebook->getUser();
    $user = $facebook->api('/me');
    } catch (Exception $e){}

    if (!empty($user)){
        if($_GET['installed']=='1'){
                header("Location: http://apps.facebook.com/lbsresearch/");
    }
    echo "Hello user";
    } 
    else {
    die ("An error occured");
    }
}

else
{
$url = $facebook->getLoginUrl();
echo "<a href='".$url."'>Click here</a> to add this Facebook application";    
}

?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-19T14:30:52+00:00Added an answer on May 19, 2026 at 2:30 pm

    Seems like you are dying before you give the user a chance to log in.
    This whole Facebook check is very confusing for me too, i can help you with the code and some comments i use (wrote it once, got it running and reused it everywhere ;))
    CALLBACK_URL is where the application is on your server.

    $facebook = new Facebook(array(
        'appId' => APP_ID,
        'secret' => APP_SECRET,
        'cookie' => true,
    ));
    $session = $facebook->getSession();
    $me = null;
    // some random permissions
    $permissions = 'publish_stream, create_event, rsvp_event, manage_pages, ads_management, user_photos';
    
    // Session based API call.
    if ($session) {
        try {
            $uid = $facebook->getUser();
            $me = $facebook->api('/' . $uid);
    
        } catch (FacebookApiException $e) {
            var_dump($e);
        }
    }
    // here, check if the user has the application added
    if ($me == null) {
        $loginUrl = $facebook->getLoginUrl(array(
            'canvas' => 1,
            'fbconnect' => 0,
            'req_perms' => $permissions,
            'next' => CALLBACK_URL . '?installed=1', // will be redirected to this URL after pressing "allow"
            'cancel_url' => CALLBACK_URL . '?cancel', // will be redirected here if app is not allowed
        )
        ); // end of array
        // redirect the user to the $loginUrl, you can place a button if you want
        echo "<script type='text/javascript'>top.location.href = '$loginUrl';</script>";
        exit;
    }
    

    I hope it will be useful to you 🙂

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to program something like facebook recent update part,so I program following code
When trying to authenticate with facebook, my app crashes. It reaches the line new
I have the following code: $facebook = new Facebook(array( 'appId' => ID, 'secret' =>
I'm trying to setup a new facebook app using the example from Facebook but
I'm trying to make an app for android using facebook's SSO, so first i
I'm trying to upload an icon to a new Facebook app (iframe) but the
I've just noticed this following , relatively new thing on Facebook, when you upload
I'm trying to familiarize myself with Facebook's new Graph API and so far I
Thanks for previous replies, I am new to Facebook sharing. I am trying to
I'm new to Facebook API and I'm currently trying to get the user friend

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.