Facebook PHP SDK Connection

450

I've been searching this site for the answer for a while and I haven't been able to find one for this problem so I'm hoping someone can give me a hand. I'm not exactly an experience PHP programmer so there could be something severely wrong with what I'm doing.

Anyways I am working with another developer who doesn't code in PHP at all and needs access to Facebook information. That being said I'm trying to develop a facebook class which encapsulates all of the Facebook graph calls, allowing for my buddy to call a single function and get all of the information he needs without having to worry about any of the Facebook PHP SDK functions at all. This class I've put in a separate file called CFacebook.php. The code for this class can be found below (I've simply copied the example off of the facebook website)

<?php
require_once 'facebook.php';
class CFacebook
{

//private variables
private $Config = "";
private $FBObject = "";
private $Code = "";
private $Session = "";
private $AppId = '*************';
private $ApiSecret = '*******************'; 
//I have the API secret and AppID in 
//here but for privacy reasons I've taken them out
//constructor
public function CFacebook()
{
    $this->FBObject = new Facebook();
    $this->FBObject->setApiSecret($this->ApiSecret);
    $this->FBObject->setAppId($this->AppId);
}

//Get profile information
public function GetFBProfileInformation()
{
    $FBProfile = "";
    $ID = $this->FBObject->getUser();
    echo "<br />";
    if($ID)
    {
        try {
            $FBProfile = $this->FBObject->api('/me','GET');
            return $FBProfile;
        }catch (FacebookApiException $e)
        {
            //send back to UI to have user sign in

            // If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    echo "has ID <br />";
    $login_url = $this->FBObject->getLoginUrl(); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
    echo $this->FBObject->getAccessToken()."<br />";
    echo $this->FBObject->getApiSecret()."<br />";
    error_log($e->getType());
    error_log($e->getMessage());
        }
    }else
        {
            //return to UI taht user isn't logged in and have user re-sign in

            //If the user is logged out, you can have a 
    // user ID even though the access token is invalid.
    // In this case, we'll get an exception, so we'll
    // just ask the user to login again here.
    $Page = "http://fratlabs.com/FacebookTest.php";
    $login_url = $this->FBObject->getLoginUrl(array('scope'=>'email,publish_stream,user_likes,user_hometown','redirect_uri'=>$Page));
    //$logout_url = $this->FBObject->getLogoutUrl(array('next'=>'http://fratlabs.com/FacebookTest.php')); 
    echo 'Please <a href="' . $login_url . '">login.</a>';
    //echo 'Logout Url: <a href="'. $logout_url.'">Logout</a>';
    error_log($e->getType());
    error_log($e->getMessage());

        }

    //echo $this->FBObject->getLogoutUrl(array('next'=>'http://fratlabs.com/FacebookTest.php'));
}

};

?>

This class is included and instantiated in a test page which is where the Facebook login is returned too. Maybe I need to include all of these calls in the same page as the one the facebook login is returned to? The only thing that is confusing me is that once every blue moon the code will work and I will actually create a link to Facebook, but every other time I'm left staring at the login page with the "please login" link on it.

481

Answer

Solution:

While I am not a php expert myself, I believe it is advised to use try/catch objects for catching exceptions usually as message output not as a logic operator (like if/else) with well-defined pathways of the script like you seem to do. Consider a different approach like initializing the fb session and authenticating the user in the first class and presenting the user information in a second class. But for your partner who only wants fb user info, a message can be delivered in second class if user did not successfully login or authenticate.

For illustration (without functions), I use the code included here.

People are also looking for solutions to the problem: javascript - JQuery UI Autocomplete with PHP file source

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.