我正在尝试获取用户最喜欢的电影和其他详细信息以及 FB JS SDK。以下对我来说是正确的。我曾尝试使用 ajax 来删除页面重新加载。但我没有找到为什么在登录按钮出现之前页面被重新加载两次。在用户输入登录凭据并单击登录按钮后,在数据出现之前页面重新加载了两次。我想删除这个多重页面重新加载。有人可以检查代码并使其更容易吗?
登录.php:
<?php
session_start();
$appId = '66';
$appSecret = 'sdsds3'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/'; //path to script folder
$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/
?>
<!DOCTYPE html>
<html xmlns:fb="http://www.facebook.com/2008/fbml" xml:lang="en-gb" lang="en-gb" ><head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.2/jquery.min.js"></script>
<title>JS/Ajax Facebook Connect</title>
<script>
function AjaxResponse()
{
var myData = 'connect=1';
$.ajax({
type: "POST",
url: "process_facebook.php",
data: myData
}).done(function(result) {
$("#fb-root").html(result);
});
}
function LodingAnimate() //Show loading Image
{
$("#LoginButton").hide(); //hide login button once user authorize the application
}
</script></head><body>
<?php
if(!isset($_SESSION['logged_in']))
{
?>
<div id="results"> </div>
<div id="LoginButton">
<div class="fb-login-button" onlogin="javascript:CallAfterLogin();" size="medium" scope="<?php echo $fbPermissions; ?>">Connect With Facebook</div>
</div>
<?php
}
?>
<div id="fb-root"></div>
<script type="text/javascript">
window.fbAsyncInit = function() {
FB.init({
appId: '<?php echo $appId; ?>',
cookie: true,
xfbml: true,
channelUrl: '<?php echo $return_url; ?>channel.php',
oauth: true});};
(function() {
var e = document.createElement('script');
e.async = true;
e.src = document.location.protocol +'//connect.facebook.net/en_US/all.js';
document.getElementById('fb-root').appendChild(e);}());
function CallAfterLogin(){
FB.login(function(response) {
if (response.status === "connected")
{
LodingAnimate(); //Animate login
FB.api('/me', function(data) {
// $("#fb-root").append(data.);
console.log(data);
if(data.email == null)
{
//Facbeook user email is empty, you can check something like this.
alert("You must allow us to access your email id!");
ResetAnimate();
}else{
AjaxResponse();
} });
} }); }
</script>
</body>
</html>
process_facebook.php:
<?php
session_start();
$appId = '66549'; //Facebook App ID
$appSecret = '3944'; // Facebook App Secret
$return_url = 'http://yoursite.com/connect_script/'; //path to script folder
//$fbPermissions = 'publish_stream,email'; //more permissions : https://developers.facebook.com/docs/authentication/permissions/
if(isset($_POST["connect"]) && $_POST["connect"]==1)
{
if (!class_exists('FacebookApiException')) {
require_once('facebook.php' );
}
$facebook = new Facebook(array(
'appId' => $appId,
'secret' => $appSecret,
));
$fbuser = $facebook->getUser();
if ($fbuser) {
try {
$user_interest = $facebook->api('/me/movies');
//print user facebook data
echo '<pre>';
//print_r($user_interest);
foreach ($user_interest['data'] as $var)
{
$ID=$var['id'];
$Name=$var['name'];
echo '<br/>';
print_r($Name); echo '<br/>';
print_r($ID);
}
echo '</pre>';
}
catch (FacebookApiException $e) {
echo '<pre>'.htmlspecialchars(print_r($e, true)).'</pre>';
$user = null;
}}}
?>