我已经在这个网站上搜索了一段时间的答案,但我无法找到解决这个问题的答案,所以我希望有人能帮帮我。我不是一个经验丰富的 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 的链接,但每隔一次我就会盯着登录页面,上面有“请登录”链接。