Codeigniter - Facebook使用PHP SDK登录和注册

时间:2013-03-15 11:28:41

标签: codeigniter facebook-php-sdk

我正在使用Facebook PHP SDK和Codeigniter来使用Facebook进行登录和注册。为此,我使用This教程

我的控制器'facebooktest.php'代码是:

<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

class Facebooktest extends CI_Controller {
    function __construct() {
        parent::__construct();
        $this->load->library('input');
        $this->load->library('session');
        $this->load->helper(array('form', 'url'));
        $this->load->library('form_validation');
        $this->load->library('security');
    }

    function Facebooktest() {
        parent::Controller();
        $cookies = $this->load->model('facebook_model');
    }
    function index() {
        $this->load->view('facebooktest/test2');
    }

    function test1() {
        $data = array();
                $data['user'] = $this->facebook_model->get_facebook_cookie();
        $this->load->view('facebooktest/test1', $data);
    }

    function test2() {    
        $data['friends'] = $this->facebook_model->get_facebook_cookie();
        $this->load->view('facebooktest/test2', $data);
    }
}
/* End of file welcome.php */
/* Location: ./application/controllers/welcome.php */
?>

view'test2.php'的代码为:

<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:fb="http://www.facebook.com/2008/fbml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
    <title>Kent is Learning CodeIgniter - Test 2</title>
</head>
<body>
<fb:login-button autologoutlink="true" 
            onlogin="window.location.reload(true);"></fb:login-button>
<div style="width:600px;"> 
<? 
if(isset($friends)){
foreach($friends as $friend){ 
?>
        <img src="http://graph.facebook.com/<?=$friend['id'];?>/picture" title="<?=$friend['name'];?>" />
<? 
    }   
}
?>
</div>
<p><?=anchor('facebook_connect/page4','Go back to page 4 of the tutorial');?></p>
<div id="fb-root"></div>
<script src="http://connect.facebook.net/en_US/all.js"></script>
<script>
  FB.init({appId: '457228377680583', status: true, cookie: true, xfbml: true});
  FB.Event.subscribe('auth.sessionChange', function(response) {
    if (response.session) {
      // A user has logged in, and a new cookie has been saved
    window.location.reload(true);
    } else {
      // The user has logged out, and the cookie has been cleared
    }
  });
</script>
</body>
</html>

模型'facebook_model.php'有代码:

<?php

class Facebook_model extends CI_Model {

    function __construct() {
        parent::__construct();
    }
    function get_facebook_cookie() {
        $app_id = '457228377680583';
        $application_secret = '01f660b73bc085f0b78cd22322556495';

        if (isset($_COOKIE['fbs_' . $app_id])) {
            echo "dfgdsf";exit;
            $args = array();
            parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
            ksort($args);
            $payload = '';
            foreach ($args as $key => $value) {
                if ($key != 'sig') {
                    $payload .= $key . '=' . $value;
                }
            }
            if (md5($payload . $application_secret) != $args['sig']) {

                return null;
            }
            return $args;
        } else {
            return null;
        }
    }

    function getUser() {
        $cookie = $this->get_facebook_cookie();
        $user = @json_decode(file_get_contents(
                                'https://graph.facebook.com/me?access_token=' .
                                $cookie['access_token']), true);
        return $user;
    }

    function getFriendIds($include_self = TRUE) {
        $cookie = $this->get_facebook_cookie();
        $friends = @json_decode(file_get_contents(
                                'https://graph.facebook.com/me/friends?access_token=' .
                                $cookie['access_token']), true);
        $friend_ids = array();
        foreach ($friends['data'] as $friend) {
            $friend_ids[] = $friend['id'];
        }
        if ($include_self == TRUE) {
            $friend_ids[] = $cookie['uid'];
        }

        return $friend_ids;
    }

    function getFriends($include_self = TRUE) {
        $cookie = $this->get_facebook_cookie();
        print_r($cookie);

        $friends = @json_decode(file_get_contents(
                                'https://graph.facebook.com/me/friends?access_token=' .
                                $cookie['access_token']), true);

        if ($include_self == TRUE) {
            $friends['data'][] = array(
                'name' => 'You',
                'id' => $cookie['uid']
            );
        }         
        return $friends['data'];
    }

    function getFriendArray($include_self = TRUE) {
        $cookie = $this->get_facebook_cookie();
        $friendlist = @json_decode(file_get_contents(
                                'https://graph.facebook.com/me/friends?access_token=' .
                                $cookie['access_token']), true);
        $friends = array();
        foreach ($friendlist['data'] as $friend) {
              $friends[$friend['id']] = array(
                'name' => $friend['name'],
                'picture' => 'http://graph.facebook.com/'.$friend['id'].'/picture'
        );
        }
        if ($include_self == TRUE) {
            $friends[$cookie['uid']] = 'You';
        }
        return $friends;
    }

}

?>

问题在于,在模型中,它会进入'getFriends()'。从那里进入'get_facebook_cookie()'。但它不会进入if(isset($_COOKIE['fbs_' . $app_id]))) 因此,它也不显示我想要的数据。

所以,如果可能的话,请告诉我我的代码有什么问题。 提前谢谢....

0 个答案:

没有答案