Php 使用Facebook注册CodeIgniter

Php 使用Facebook注册CodeIgniter,php,facebook,codeigniter,facebook-graph-api,Php,Facebook,Codeigniter,Facebook Graph Api,我正在尝试注册Facebook,我正在使用插件。它会将我重定向到facebook登录页面,但当我登录facebook时,它不会将我返回到我的网站 控制器 class UserController extends CI_Controller { public function fbSignup() { $this->load->library('fb'); if (!$this->fb->is_connected()) { red

我正在尝试注册Facebook,我正在使用插件。它会将我重定向到facebook登录页面,但当我登录facebook时,它不会将我返回到我的网站

控制器

class UserController extends CI_Controller { 
    public function fbSignup() {
    $this->load->library('fb');
    if (!$this->fb->is_connected()) {

        redirect($this->fb->login_url(current_url()));
    }

    $fb_user = $this->fb->client->api('/me');
    echo "<pre>";
    print_r($fb_user); exit;
    if (empty($fb_user)) {
        $error = "FACEBOOK LOGIN FAILED - USER US EMPTY. FILE: " . __FILE__ . " LINE: " . __LINE__;
        $this->session->set_flashdata('register_error', $error);
    } else {
        $this->user->set_facebook_id($fb_user['id']);
        $user = $this->user->get_by_facebook();
        if (!empty($user) && !empty($user->id) && is_numeric($user->id)) {
            //TODO: Make things a bit more secure here
            //Login & Redirect home
            $this->_login($user->id, 'facebook');
            $this->load->view('users/redirect_home');
            return;
        }
    }
}

}
class UserController扩展CI_控制器{
公共功能注册(){
$this->load->library('fb');
如果(!$this->fb->已连接(){
重定向($this->fb->login_url(当前的_url());
}
$fb_user=$this->fb->client->api('/me');
回声“;
打印(fb用户);退出;
if(空($fb_用户)){
$error=“FACEBOOK登录失败-用户US为空。文件:”。\uuuu文件。\uuuu行:”。\uuuu行;
$this->session->set_flashdata('register_error',$error);
}否则{
$this->user->set_facebook_id($fb_user['id');
$user=$this->user->get_by_facebook();
如果(!empty($user)&&!empty($user->id)&&是数值($user->id)){
//TODO:让这里更安全一点
//登录并重定向主页
$this->_登录($user->id,'facebook');
$this->load->view('users/redirect_home');
返回;
}
}
}
}

所有的应用程序sercrets和API键都已设置

您不需要使用任何CodeIgniter插件,只需使用Javascript库和JS库就可以做到这一点。使用JS库有两个优点--

01)你可以在任何框架下使用这个脚本,比如CodeIgniter、CakePHP、Laravel等。我的意思是,当你开始在新框架上工作时,你不需要浪费时间,只需使用这个脚本

02)使用JS,您可以在弹出窗口中打开网站上facecbook的登录表单,它不会重定向到facebook.com页面

剧本--


window.fbAsyninit=函数(){
FB.init({
appId:'717505658366417',//设置你的应用ID
状态:true,//检查登录状态
cookie:true,//启用cookie以允许服务器访问会话
xfbml:true//解析xfbml
});
};
函数登录()
{
FB.登录(功能(响应){
if(response.authResponse)
{
getUserInfo();
}否则
{
log('用户取消登录或未完全授权');
}
},{范围:'电子邮件、用户照片、用户视频'});
}
函数getUserInfo(){
FB.api('/me',函数(响应){
var str=“Name:”+response.Name+“
”; str+=“Link:+response.Link+”
”; str+=“性别:”+response.Gender+“
”; str+=“id:”+response.id+“
”; str+=“Email:+response.Email+”
”; str+=”; str+=”; document.getElementById(“status”).innerHTML=str; }); } 函数getPhoto() { FB.api('/me/picture?type=normal',函数(响应){ var str=“
Pic:”; document.getElementById(“status”).innerHTML+=str; }); } 函数注销() { 注销(函数(){document.location.reload();}); } //异步加载SDK (职能(d){ var js,id='facebook jssdk',ref=d.getElementsByTagName('script')[0]; if(d.getElementById(id)){return;} js=d.createElement('script');js.id=id;js.async=true; js.src=“//connect.facebook.net/en_US/all.js”; ref.parentNode.insertBefore(js,ref); }(文件); Facebook OAuth Javascript演示 日志:

谢谢,但它没有返回任何东西,它已登录,但我看不到任何东西返回
<html>
<body>
<div id="fb-root"></div>
<script>
  window.fbAsyncInit = function() {
    FB.init({
      appId      : '717505658366417', // Set YOUR APP ID
      status     : true, // check login status
      cookie     : true, // enable cookies to allow the server to access the session
      xfbml      : true  // parse XFBML
    });

  };

    function Login()
    {

        FB.login(function(response) {
           if (response.authResponse) 
           {
                getUserInfo();
            } else 
            {
             console.log('User cancelled login or did not fully authorize.');
            }
         },{scope: 'email,user_photos,user_videos'});

    }

  function getUserInfo() {
        FB.api('/me', function(response) {
      var str="<b>Name</b> : "+response.name+"<br>";
          str +="<b>Link: </b>"+response.link+"<br>";
          str +="<b>Gender:</b> "+response.gender+"<br>";
          str +="<b>id: </b>"+response.id+"<br>";
          str +="<b>Email:</b> "+response.email+"<br>";
          str +="<input type='button' value='Get Photo' onclick='getPhoto();'/>";
          str +="<input type='button' value='Logout' onclick='Logout();'/>";
          document.getElementById("status").innerHTML=str;

    });
    }
    function getPhoto()
    {
      FB.api('/me/picture?type=normal', function(response) {

          var str="<br/><b>Pic</b> : <img src='"+response.data.url+"'/>";
          document.getElementById("status").innerHTML+=str;

    });

    }
    function Logout()
    {
        FB.logout(function(){document.location.reload();});
    }

  // Load the SDK asynchronously
  (function(d){
     var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
     if (d.getElementById(id)) {return;}
     js = d.createElement('script'); js.id = id; js.async = true;
     js.src = "//connect.facebook.net/en_US/all.js";
     ref.parentNode.insertBefore(js, ref);
   }(document));

</script>
<div align="center">
<h2>Facebook OAuth Javascript Demo</h2>

<div id="message">
Logs:<br/>
</div>
</div>
</body>
</html>