PHP Facebook Connect

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了PHP Facebook Connect相关的知识,希望对你有一定的参考价值。

<?php
//include_once 'fbconnect/config.php'; //has $api_key and $secret defined.
include_once 'facebook/facebook.php';
//global $api_key,$secret;
$appapikey = '';
$appsecret = '';
//Start Facebook
$fb=new Facebook($appapikey,$appsecret);
$fb_user=$fb->get_loggedin_user();

?>
<html>
<head>
<!-- Call Javascript For FBML -->

<script src="http://static.ak.connect.facebook.com/js/api_lib/v0.4/FeatureLoader.js.php/en_US" type="text/javascript"></script><script type="text/javascript">FB.init("<?= $appapikey ?>");</script>

<title>Facebook Connect</title>
</head>
<body>
<?php
if(!$fb_user)
{
?>
	<!-- Login Button -->
	<fb:login-button v="2" size="medium" onlogin="window.location.reload(true);">Connect with Facebook</fb:login-button>
	
<?php
}
else
{
	echo "Facebook";
	
	//User
	//http://wiki.developers.facebook.com/index.php/Users.getInfo
	//can retrive many user information except user's email , check wiki
	$user_details=$fb->api_client->users_getInfo($fb_user, array('uid','last_name','first_name','proxied_email','email_hashes'));  
	$uid=$user_details[0]['uid'];
	$firstName=$user_details[0]['first_name']; 
	$lastName=$user_details[0]['last_name'];
	$proxemail=$user_details[0]['proxied_email'];
	$email=$user_details[0]['email_hashes'];
	echo $uid;
	echo "<br>";
	echo $email;
	echo "<br>";
	echo $proxemail;
	echo "<br>";
	echo $firstName;
	echo "<br>";
	echo $lastName;


	//Friend
	//http://wiki.developers.facebook.com/index.php/Friends.get
	echo "<p>Friends:";
	$friends = $fb->api_client->friends_get();
	//slice the friend list
	$friends = array_slice($friends, 0, 5);
	foreach ($friends as $friend) {
	  echo "<br>$friend";
	}
	echo "</p>";
	?>
	
	<!--
	http://developers.facebook.com/tools.php?connect_wizard
	-->
	
	<!-- Profile Picture -->
	<fb:profile-pic uid="<?= $uid ?>" size="square" facebook-logo="true"></fb:profile-pic>
	<br/>
	<!-- Friend Pic -->
	<div id="profile_pics"></div>
	<script type="text/javascript">
	var widget_div = document.getElementById("profile_pics");
	FB.ensureInit(function () {
	  FB.Facebook.get_sessionState().waitUntilReady(function() {
	  FB.Facebook.apiClient.friends_get(null, function(result) {
	    var markup = "";
	    var num_friends = result ? Math.min(5, result.length) : 0;
	    if (num_friends > 0) {
	      for (var i=0; i<num_friends; i++) {
	        markup += 
	          '<fb:profile-pic size="square" uid="'+result[i]+'" facebook-logo="true"></fb:profile-pic>';
	      }
	    }
	    widget_div.innerHTML = markup;
	    FB.XFBML.Host.parseDomElement(widget_div);
	  });
	  });
	});
	</script>

	<?
	
	//status
	
	$status=$fb->api_client->call_method("facebook.status.get", array('uid'=>$uid, 'limit'=>'5'));
	
	for($i=0;$i<5;$i++)
	{
		echo $status[$i]['message'];
		echo "<hr>";
	}
	//you can try
	//print_r($status);
	
	echo "<br>";
	

	//Check Permission
	$permission = $fb->api_client->Users_hasAppPermission('status_update');
	if($permission)
	{
		//If permission have status update
		$msg=$fb->api_client->call_method("facebook.status.set",array('status'=>'I can update stataus from fb connect'));
	}
	else
	{
	?>
		<script>
		/*
		extended permission are
		status_update, photo_upload, video_upload, create_note, and share_item	 
	 	*/
	
		//Call facebook and return to ornagai.com
		//if not get permission , next for Allow, next_cancel for not Allow
		location.href="http://www.facebook.com/authorize.php?api_key=<?= $appapikey ?>&v=1.0&ext_perm=status_update&next=http://www.yoursite.com/fbconnect.php&next_cancel=http://www.yoursite.com"
		
		
		//You can also use dialog messagebox
		/*
		 FB.Connect.showPermissionDialog("status_update", function(perms) {
	   		if (!perms) {
	     		continue_without_permission();
	  		} else {
	     		save_session();
	   		}
	 	});
		*/
		</script>
	<?
	}
	?>
	<!--
	with PHP 
	$fb->logout( "http://site.com/returnAfterLogout.php" )
	-->
	
	<a href="javascript:FB.Connect.logout()" >Logout</a>
<?
}//End Facebook user
?>
</body>
</html>

以上是关于PHP Facebook Connect的主要内容,如果未能解决你的问题,请参考以下文章

Facebook Connect - 同时使用 FBML 和 PHP 客户端

Facebook-connect:无法使用PHP SDK + JS SDK注销

Facebook 登录:如何将 JavaScript 与 PHP SDK 结合起来?

如何在Facebook上分享标题,图片和帖子?

iOS Phonegap Like 按钮 (phonegap-plugin-facebook-connect)

如何通过 Facebook Connect 在 Facebook 墙上分享分数?