2013-03-15 215 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