2013-03-15 187 views
1

我正在使用Facebook PHP SDK与Codeigniter使用Facebook进行登录和注册。对于这个我使用This教程Codeigniter - Facebook登录和注册sing PHP SDK

我控制器 '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 */ 
?> 

视图 '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

您发布了两次模型代码,您可以发布控制器代码。你正在使用的另一种浏览器? – 2013-03-15 12:16:55

+0

@FabioAntunes对不起哥.. – V15HM4Y 2013-03-15 12:31:22

+0

facebook的sdk已经有了这个逻辑,你应该用它 – 2013-03-15 12:38:15

回答

0

我正在寻找一些很好的教程,发现this