且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何使用Codeigniter将Twitter登录和Facebook登录集成到网站中?

更新时间:2022-05-18 08:27:51

步骤1:下载facebook库。 https://github.com/facebook/facebook-php-sdk/tree/master/src

Step 1: Download facebook library.https://github.com/facebook/facebook-php-sdk/tree/master/src

第2步:在\application 库中上传

Step 2: Upload it in \application\libraries

第3步:创建一个Facebook应用程序您将获得appId和secretcode。将以下代码粘贴到控制器facebook()函数中。设置appId和secret。

Step 3:Create a facebook application.You wil get appId and secretcode.Paste the below code in your controller facebook() function.Set the appId and secret.

$config = array(
'appId' => '234353465466',
'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'fileUpload' => true, // Indicates if the CURL based @ syntax for file uploads is enabled.
);
$this->load->library('Facebook',$config);

$user = $this->facebook->getUser();
if($user){
try {
$profile = $this->facebook->api('/me?fields=id,name,link,email');
}
catch (FacebookApiException $e) {
$user = null;
}
echo '<pre>';
print_r($profile);

第4步:将以下代码粘贴到视图页面中,并更改appId和域名。

Step 4:Paste the below code in the view page and change the appId and domain name.

<div id="fb-root"></div>
<script type="text/javascript">// <![CDATA[
window.fbAsyncInit = function() {
FB.init({appId: '234463456', status: true, cookie: true,xfbml: true,oauth: true});};
(function() {
var e = document.createElement('script');
e.type = 'text/javascript';
e.src = document.location.protocol+'//connect.facebook.net/en_GB/all.js';
e.async = true;
document.getElementById('fb-root').appendChild(e);
}());
function login()
{
FB.getLoginStatus(function(response) {
if (response.status === 'connected') {
document.location.href = "http://www.mydomain.com/facebook?accessToken="+response.authResponse.accessToken;
}
});
}
// ]]></script> 
  <fb:login-button scope="email" onlogin="login();" >Sign in with Facebook</fb:login-

了解更多- http://www.livey.info/blog/category/php/codeigniter/