开发者

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 ar开发者_运维技巧e 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!


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();


$returnArray = array();
$returnArray['fb'] = $fb;
$returnArray['me'] = $me;
return $returnArray;
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜