我在这个网站上搜索答案已经有一段时间了,但我还没有找到解决这个问题的答案,所以我希望有人能帮我一把。我并不是一个经验丰富的PHP程序员,所以我所做的可能有严重的问题。
无论如何,我正在与另一位根本不使用PHP编写代码的开发人员合作,他需要访问Facebook信息。话虽如此,我正在尝试开发一个facebook类,它封装了所有的facebook图形调用,允许我的好友调用一个函数并获得他需要的所有信息,而不必担心任何facebook PHP SDK函数。这个类我已经放在一个名为CFacebook.php的单独文件中。这个类的代码可以在下面找到(我只是从facebook网站上复制了这个例子)
<?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'));
}
};
?>
这个类包含并实例化在一个测试页面中,该页面也是返回Facebook登录名的地方。也许我需要将所有这些呼叫都包含在facebook登录返回的页面中?唯一让我困惑的是,每隔一个蓝月亮,代码就会工作,我就会创建一个到Facebook的链接,但每隔一段时间,我就会盯着带有"请登录"链接的登录页面。
虽然我自己不是php专家,但我认为建议使用try/catch对象来捕获异常,通常将其作为消息输出,而不是像您所做的那样作为具有定义良好的脚本路径的逻辑运算符(如if/else)。考虑一种不同的方法,比如初始化fb会话,在第一类中验证用户,在第二类中呈现用户信息。但对于只想要fb用户信息的合作伙伴,如果用户未成功登录或验证,则可以在第二类中发送消息。
为了便于说明(没有函数),我使用此处包含的代码。