-3

我尝试了很多方式..我使用了 php-sdk 和许多其他方式...但我无法将我的网站与 facebook 连接...实际上..我正在使用 codeigniter 做我的项目。任何人都可以建议我连接到 facebook 登录并使用 codeigniter 共享。

<?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>";
            }
        ?>
4

2 回答 2

2

Danny Tran 为 CodeIgniter 提供了一个简单易用的 Facebook 库

CI_Facebook是一个Facebook 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

希望这可以帮助。

于 2012-06-21T13:35:16.277 回答
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 ';
            }


    }

询问您是否还有其他问题

于 2012-06-21T13:36:58.443 回答