我发现没有办法与我的网站建立Facebook集成



我尝试了很多方法......我用php-sdk和许多其他方式...但是我无法将我的网站与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>";
            }
        ?>

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

CI_Facebook是一个Facebook Library for CodeIgniter.

只需将所有文件复制/合并到相应的位置,然后 设置配置/脸书.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

希望这有帮助。

几个星期我不得不做同样的事情,之后我想出了这个。我需要它来进行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 ';
            }

    }

询问您是否还有其他问题

最新更新