Facebook api - in a function

I have been trying to set up facebook on a page via a function that is in functions file. However I want to return multiple arrays, so I can get every bit of info.

Here's how I'm doing it:

functions.php:

require 'src/facebook.php';

function fb_setup($app_id, $app_secret){
$facebook = new Facebook(array(
  'appId'  => $app_id,
  'secret' => $app_secret,
  'cookie' => true,
));
$session = $facebook->getSession();
$me = null;
// Session based API call.
if ($session) {
  try {
    $uid = $facebook->getUser();
    $me = $facebook->api('/me');
  } catch (FacebookApiException $e) {
    error_log($e);
  }
}
// login or logout url will be needed depending on current user state.
if ($me) {
  $logoutUrl = $facebook->getLogoutUrl();
} else {
  $loginUrl = $facebook->getLoginUrl();
}
// This call will always work since we are fetching public data.
$fb = array(
            "logoutUrl" => $logoutUrl,
            "loginUrl" => $loginUrl
            );

return $fb;
return $me;
}

test.php

require("../functions.php");
$fb = fb_setup('************','*****************************');
echo $fb['logoutUrl'];
//but I also want to get the $me info

I want to get the $me information as well.

Thanks!

Answers


return array('fb' =>$fb, 'me' => $me);


echo $fb['fb']['logoutUrl'];
echo $fb['me']['name'];**

This uses a multi-dimensional array although I'd recommend refactoring into a class.

e.g.

class FacebookController
{
    private $facebook;
    public function __construct($app_id, $app_secret)
    {
         $this->facebook = new Facebook(array(
          'appId'  => $app_id,
          'secret' => $app_secret,
          'cookie' => true,
        ));
    }

    public function getMe()
    {
        if ($this->isLoggedIn)
        {
            // Session based API call.
            if ($session)
            {
                try
                {
                   return $this->facebook->api('/me');
                }
                catch (FacebookApiException $e)
                {
                  error_log($e);
                  return false;
                }
            }
        }
    }

    public function getUrl()
    {
         if($this->isLoggedIn)
         {
             return $this->facebook->getLogoutUrl();
         }
         else
         {
             return $this->facebook->getLoginUrl();
         }
    }

    private function isLoggedIn()
    {
        return $facebook->getSession();
    }
}



$FacebookController = new FacebookController(APPID, SECRET);
$me = $FacebookController->getMe();
$url = $FacebookController->getUrl();

Need Your Help

How come the image is not displayed?

php html mysql mysqli

I'm learning php and mysqli. I wrote a program that allows users to upload name and image to a database. They can click on a name in the database and clicking on a name, they'll see a picture of ...

How to get a Users Messages with Mailboxer

ruby-on-rails messaging

I'm building a simple messaging system into my app, and using mailboxer to do this. I'm trying to list out all of the messages that a user has received including the body, and name of the sender. I...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.