2012-06-21 44 views
-3

我嘗試了很多方式..我使用php-sdk和許多其他方式...但我無法連接我的網站與Facebook ...實際上。我在codeigniter中做我的項目。任何人都可以建議我連接到Facebook登錄並使用codeigniter共享。我發現沒有辦法建立與我的網站的Facebook集成

<?php 
      include "libs/facebook.php"; 
      $facebook = new Facebook(array(
       'appId' => '376406812408828', 
       'sec`enter code here`ret' => 'ca1eb65bde82a4009c31b4a5adb047b5', 
       'cookie' => true 
      )); 
      print_r($facebook); 
      $session = $facebook->getUser(); 
      echo $session; 
      $me=null; 
      if($session) 
      { 
       try 
       { 

        $me = $facebook->api('/me'); 
        echo $me; 
        $facebook->api('/me/feed','post',array('message' => 'Hello World!')); 
       } 
       catch(FacebookApiException $e) 
       { 
        echo $e->getMessage(); 
       } 
      } 
      if($me) 
      { 
       $logoutUrl = $facebook ->getLogoutUrl(); 
       echo "<a href=".$logoutUrl.">Logout</a>"; 
      } 
      else 
      { 

       $loginUrl = $facebook ->getLoginUrl(array(  
        'req_perms' => 'publish_stream,read_friendlists' 
       )); 
       echo "<a href=".$loginUrl.">Login</a>"; 
      } 
     ?> 
+3

沒有什麼是不可能的哥們! ! – AlphaMale

回答

2

丹尼Tran還提供A Simple & Easy Facebook Library for CodeIgniter

CI_FacebookFacebook Library for CodeIgniter

只需將所有文件複製/合併到相應的位置,並將其設置爲config/facebook.php。

有爲每個功能/類編寫的單元測試。您將需要 PHPUnit來執行它們。

訪問Facebook對象很容易,因爲掛鉤自動加載 它。

e.g. $user_data = $this->facebook->fb->api_client->fql_query("select name from user where uid = TARGETUSERID"); 

鏈接到庫上Github上https://github.com/dannybtran/CI_Facebook

希望這有助於。

0

幾個星期,我不得不這樣做,之後,我想出了這個。我需要它用於ajax登錄,但它大多適合每個人的需要。

這是第一步,它會提示Facebook登錄(如果您尚未登錄),然後重定向到您之前設置的uri。

$this->load->library('facebook_handler'); 
$this->facebook_handler->loginBegin($this->config->item('endpointfacebook')); 

Facebook_handler在庫文件夾

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


class Facebook_handler extends ML_index 
{ 

     var $scope = "email, user_about_me, user_birthday, user_hometown, user_website,read_stream, publish_stream, read_friendlists"; 


    function __construct() 
    { parent::__construct(); 

     $this->CI =& get_instance(); 
     $this->CI->config->load('mlogin_config'); //Config where I have the keys 

     if (! $this->CI->config->item('facebook_api_key') || ! $this->CI->config->item('facebook_api_key_secret')) 
     { 
      throw new Exception("Your application id and secret are required in order to connect to {$this->providerId}.", 4); 
     } 
     include_once(APPPATH.'libraries/Facebook/base_facebook.php'); //Place where I've situated the facebook libraries 
     include_once(APPPATH.'libraries/Facebook/facebook.php'); 

     $this->fb = new Facebook(ARRAY('appId' => $this->CI->config->item('facebook_api_key'),'cookie'=>true, 'secret' => $this->CI->config->item('facebook_api_key_secret'))); 
     $this->user = $this->fb->getUser(); 

    } 
    /*The login process starts here, endpoint is the redirect_url for facebook*/ 
    function loginBegin($endpoint) 
    { 
     $scope=$this->CI->config->item('facebook_scope'); 
     if(isset($scope) && ! empty($scope)) 
     { 
      $this->scope = $scope; 
     } 
     $this->logout(); 
     $url = $this->fb->getLoginUrl(array('domain'=>base_url(),'scope' => $this->scope, 'redirect_uri' => $endpoint,'display' => 'popup')); 

     redirect($url); 
    } 

    /*Function to get user data*/ 
    function getUser(){ 
     if ($this->user) { 
      try { 
      // Proceed knowing you have a logged in user who's authenticated. 
      $user_profile = $this->fb->api('/me'); 
      return $user_profile; 
      } catch (FacebookApiException $e) { 
      error_log($e); 
      return false; 
      } 
     }else{ 
      $this->output->set_output('no logueado'); 
     } 
    } 

    /*Facebook logout, it destroys facebook sessions. I dont really use this*/ 
    function logout(){ 
     $this->fb->destroySession(); 
    } 
} 

?> 

功能重定向到

function closewindowfacebook(){ 
      $this->load->library('facebook_handler'); 
      $userprofile=$this->facebook_handler->getUser(); 


      if ($userprofile!=false){ 
       $fb_uid = $this->facebook_handler->fb->getUser(); 
       $fb_email=$userprofile['email']; 
       $fb_name=$userprofile['name']; 

       /*My function to connect to the website, that you'd do it yourself*/ 
       //$this->auth->try_fb_login($fb_uid,$fb_email,$fb_name); 

       /*I use this to close the popup window*/ 
          die('<script type="text/javascript"> 
        window.opener.everythingready(); 
        window.close(); 
       </script>'); 

      } else{ 
       echo 'error '; 
      } 


    } 

詢問如果您有任何進一步的問題

相關問題