我发现无法与我的网站建立Facebook集成

时间:2012-06-21 13:24:53

标签: php facebook codeigniter

我试过很多方面..我使用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>";
            }
        ?>

2 个答案:

答案 0 :(得分:2)

Danny Tran提供了A Simple & Easy Facebook Library for CodeIgniter

  

CI_Facebook Facebook Library for CodeIgniter

     

只需将所有文件复制/合并到相应的位置即可   setup 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

希望这有帮助。

答案 1 :(得分: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 ';
            }


    }

询问您是否还有其他问题