我一直在搜索代码示例并阅读Facebook PHP API文档,但在谈到会话和存储令牌时,我仍然有点困惑。
这是我拥有的以下代码(在submit.php下):
<?php
session_start();
require_once('Facebook_SDK/Facebook/FacebookSession.php');
require_once('Facebook_SDK/Facebook/FacebookRedirectLoginHelper.php');
require_once('Facebook_SDK/Facebook/FacebookRequest.php');
require_once('Facebook_SDK/Facebook/FacebookResponse.php');
require_once('Facebook_SDK/Facebook/FacebookSDKException.php');
require_once('Facebook_SDK/Facebook/FacebookRequestException.php');
require_once('Facebook_SDK/Facebook/FacebookAuthorizationException.php');
require_once('Facebook_SDK/Facebook/GraphObject.php');
require_once('Facebook_SDK/Facebook/HttpClients/FacebookCurl.php');
require_once('Facebook_SDK/Facebook/HttpClients/FacebookHttpable.php');
require_once('Facebook_SDK/Facebook/HttpClients/FacebookCurlHttpClient.php');
require_once('Facebook_SDK/Facebook/Entities/AccessToken.php');
require_once('Facebook_SDK/Facebook/GraphUser.php');
use Facebook\FacebookSession;
use Facebook\FacebookRedirectLoginHelper;
use Facebook\FacebookRequest;
use Facebook\FacebookResponse;
use Facebook\FacebookSDKException;
use Facebook\FacebookRequestException;
use Facebook\FacebookAuthorizationException;
use Facebook\GraphObject;
use Facebook\HttpClients\FacebookCurl;
use Facebook\HttpClients\FacebookHttpable;
use Facebook\HttpClients\FacebookCurlHttpClient;
use Facebook\Entities\AccessToken;
use Facebook\GraphUser;
$app_id = 'xxxxx';
$app_secret = 'xxxxx';
FacebookSession::setDefaultApplication($app_id, $app_secret);
// Use one of the helper classes to get a FacebookSession object.
// FacebookRedirectLoginHelper
// FacebookCanvasLoginHelper
// FacebookJavaScriptLoginHelper
// or create a FacebookSession with a valid access token:
//$session = new FacebookSession('access-token-here');
?>
<html>
<head>
<title></title>
</head>
<body>
<?php
$app_url = 'http://1.1.1.1/submit.php';
$helper = new FacebookRedirectLoginHelper($app_url, $app_id, $app_secret);
try {
$session = $helper->getSessionFromRedirect();
}
catch(FacebookRequestException $ex) { }
catch(\Exception $ex) { }
$loggedIn = false;
if (isset($session)){
if ($session) {
$loggedIn = true;
try {
// Logged in
$request = new FacebookRequest($session,'POST','/me/feed', array ('message' => 'This is a test message', 'scope' => 'publish_actions',));
$response = $request->execute();
$graphObject = $response->getGraphObject();
/* handle the result */
} catch(FacebookRequestException $e) {
echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();
}
}
}
if (!$loggedIn){
$loginUrl = $helper->getLoginUrl();
echo "<a href='$loginUrl'>Login</a>";
}
?>
</body>
</html>
然后,我在index.php下面有这个代码:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<title>Facebook API Post</title>
<link rel="stylesheet" href="styles/styles.css">
</head>
<body>
<form action="submit.php" method="post" accept-charset="utf-8">
<h2>Post to Facebook!</h2>
<label for="postContent">Post: </label>
<textarea name="postContent" placeholder="Enter your post here..." rows="5"></textarea>
<button type="submit">Post To Wall!</button>
</form>
</body>
</html>
目前,我还没有将textarea内容提取到submit.php中,因为我只想在submit.php中测试该脚本可以发布到用户的Facebook墙上。
代码不起作用,直到此时我的想法已经用完了。
该脚本确实被重定向到Facebook以供用户登录,但是当它被转发回http://104.236.247.219/index.php时,它只显示一个空白屏幕,并且没有任何内容发布到用户的墙上。
任何想法或建议以及我应该做些什么?我觉得我很近,但是我有一些缺失的东西,我的猜测与会话有关吗?
非常感谢!