PHP Facebook PHP SDK入门代码

Posted

tags:

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

<?php
//This is my skeleton template for a PHP SDK canvas app, use it if you find it useful.
//Lately I use iframe canvas apps so I used a javascript page redirect instead of fbjs ajax with require_login();
 
//Don't forget to change path to PHP SDK if necessary
require_once 'facebook.php';
// Create our Application instance. Don't forget to change to your AppId and Secret
 
$facebook = new Facebook(array(  
'appId'  => 'XXXXXXXXXXXXX',  
'secret' => 'XXXXXXXXXXXXXXXXXXXXXXXXXXXXXX',  
'cookie' => true,));
 
// Uncomment to debug post fields
// print_r($_REQUEST);
 
// Setting canvas to 1 and fbconnect to 0 fixes issues with being redirected back to your base domain instead of back to the canvas app on auth
$loginUrl = $facebook->getLoginUrl($params = array('canvas' => 1, 'fbconnect' => 0, 'req_perms' => 'insert required perms here'));

$session = $facebook->getSession();
$me = null;
 
if ($session) {
	try {
		$me = $facebook->api('/me');
		//If user has authed application, render a logout link.
		//echo '<a href="'.$facebook->getLogoutUrl().'">Logout</a>';
	} catch(FacebookApiException $e){
		//Uncomment the line below to have a user click to authorize the app instead of requesting permissions on page load.
		//echo '<a href="'.$loginUrl.'">Login</a>';
		//Request permissions on page load if app is not authed & cannot request user information.
		echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
		error_log($e);
		}
	} else {
	//Request permissions on page load if app is not authed & or session is invalid.
	echo "<script type='text/javascript'>top.location.href = '".$loginUrl."';</script>";
}

// If you can hit user on the Graph API, do some stuff
if($me) {
	
}


 
?>

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

Facebook PHP SDK启动程序代码

facebook PHP SDK 不能在不同的 PHP 版本中工作

如何使用php SDK在一个域内托管Facebook(作为App)运行php代码? [关闭]

使用 Facebook PHP SDK 4,但没有存储会话

在 laravel 中访问 facebook php sdk 4.0 和 Javascript sdk 上的 Token

会话总是返回 null facebook php sdk