Facebook-Connect

Hello all,

i used extention of facebook connect : http://www.yiiframew…cebook-connect/

but when i login i get the information of session :

Array

(

[session_key] =>

[uid] =>

[expires] =>

[secret] =>

[base_domain] =>

[access_token] =>

[sig] =>

)

But i need to redirect to my site when connect with facebook not get the information of session

[b]I add this function in SiteController :

[/b]

protected $_identity;

public $_logouturl;

public function actionFblogin(){

if($this->_identity===null)

{

$this->_identity=new FBIdentity($_GET[‘username’],$_GET[‘userid’]);

$this->_identity->authenticate();

$this->_logouturl = $_GET[‘logout’];

$error = 0;

if($error===FBIdentity::ERROR_NONE){

$duration=36002430;

Yii::app()->user->login($this->_identity,$duration);

$this->redirect(Yii::app()->homeUrl);

}else{

return false;

}

}

}

But i can’t read result of this function .like i add it like don’t add it !!

so where is the problem ?

I need to your help

Thanks for all.

Few days ago i had to create a facebook and twitter connect for one of my clients, so i will share the code and the steps i made.

My facebook app is meant to retrieve and display all the user friends in a page, so here it is:




public function actionFacebook()

{

    $alias=Yii::getPathOfAlias('custom.components.facebook');

    require($alias.DIRECTORY_SEPARATOR.'facebook.php');

    

    $facebook = new Facebook(array(

      'appId'  => Yii::app()->settings->get('facebook','appId'),

      'secret' => Yii::app()->settings->get('facebook','appSecret'),

    ));

    

    $this->layout='//layouts/facebook_connect';

    

    $user=$facebook->getUser();

    $user_friends=$user_profile=$login_url=$logout_url=$status_update=null;

    

    if(empty($user))

    {

       $login_url=$facebook->getLoginUrl(array(

        'scope'=>'friends_photos, publish_stream',

        'display'=>'popup',

        'next'=>Yii::app()->createAbsoluteUrl('connect/default/facebook'),

       ));

       Yii::app()->request->redirect($login_url); 

    }


    $logout_url=$facebook->getLogoutUrl();

    

    try

    { 

        $user_profile=$facebook->api('/me');

        $user_friends=$facebook->api('/me/friends'); 

    } 

    catch(FacebookApiException $e){}


    $this->render('_facebook',array(

        'user'          => $user,

        'user_friends'  => $user_friends,

        'user_profile'  => $user_profile,

        'login_url'     => $login_url,

        'logout_url'    => $logout_url, 

    ));

}



In the view, you have access to all the info you requested in the controller, as a sample, here is how i display user friends:




<table cellpadding="0" cellspacing="0" style="margin-top: 10px;">

<tr>

<?php if(!empty($user_friends['data'])):?>

<?php $i=0; foreach($user_friends['data'] AS $friend): ++$i;?>

<td align="left" valign="top" width="120" style="padding-bottom: 10px;">

    <table cellpadding="0" cellspacing="0" width="120">

        <tr>

            <td align="center" valign="top">

                <a href="javascript:;" onclick="submitFriendForm(<?php echo (int)$friend['id'];?>);">

                    <img src="http://graph.facebook.com/<?php echo (int)$friend['id'];?>/picture" style="border:2px solid #fff; padding:3px;"/>

                </a>

            </td>

        </tr>

        <tr>

            <td align="center" valign="top" style="font-size:10px">

                <?php 

                    if(strpos($friend['name'],' ')!==false)

                    {

                        $part=explode(" ",$friend['name']);

                        if(strlen($part[0])>10)

                            echo CHtml::encode(substr($part[0],0,10)).'...';

                        else

                            echo CHtml::encode($part[0]);

                    }

                    else

                    {

                        if(strlen($friend['name'])>10)

                            echo CHtml::encode(substr($friend['name'],0,10)).'...';

                        else

                            echo CHtml::encode($friend['name']);

                    }

                ?>

            </td>

        </tr>

    </table>

</td>

<?php if($i % 4 == 0):?></tr><tr><?php endif;?>

<?php endforeach;?>

<?php endif;?>

</tr>

</table>



I didn’t use any special YII extension for this because i really don’t see the point, instead, i used the php-sdk provided here: https://github.com/facebook/php-sdk

Thanks twisted1919.

But i don’t need to display the user friends in a page.

I want to connect by my site without the user doing inscription. so if he login with facebook can too connect directly to my site.

really i didn’t get the result

Well, it is not about displaying the friends in a page, is about understanding the workflow.

In the example i shown to you, the code authenticates a user using facebook, then you can use the $user variable to get the user_id and use it in you app.That’s the whole point of the example.

Basically, take a look here:




public function actionFacebook()

{

    $alias=Yii::getPathOfAlias('custom.components.facebook');

    require($alias.DIRECTORY_SEPARATOR.'facebook.php');

    

    $facebook = new Facebook(array(

      'appId'  => Yii::app()->settings->get('facebook','appId'),

      'secret' => Yii::app()->settings->get('facebook','appSecret'),

    ));

    

    $this->layout='//layouts/facebook_connect';

    

    $user=$facebook->getUser();

    $user_friends=$login_url=$logout_url=null;

    

    if(empty($user))

    {

       $login_url=$facebook->getLoginUrl(array(

        'scope'=>'friends_photos, publish_stream',

        'display'=>'popup',

        'next'=>Yii::app()->createAbsoluteUrl('connect/default/facebook'),

       ));

       Yii::app()->request->redirect($login_url); 

    }


    $logout_url=$facebook->getLogoutUrl();

    

    try

    { 

        $user_profile=$facebook->api('/me'); 

    } 

    catch(FacebookApiException $e){}


    echo '</pre>';

    print_r($user);

    echo '<br />';

    print_r($user_profile);

    echo '<br />';

    print_r($_SESSION);

}



When you will access this page first time, you will be forced to login with facebook, then you will be redirected to this page again, only that, this time, you will be logged in to facebook, therefore, you will see the output of the print_r() statements.

Where i do public function actionFacebook() ? in which controller ? i did that in sitecontroller but don’t works !

This is what I did: http://www.yiiframework.com/extension/facebook-connect/

  1. i added folder facebook to protected/extensions/

  2. I added this function to SiteController


 protected $_identity;

 public $_logouturl;

 

 public function actionFblogin(){

        if($this->_identity===null)

        {

            $this->_identity=new FBIdentity($_GET['username'],$_GET['userid']);

            $this->_identity->authenticate();

            $this->_logouturl = $_GET['logout'];

 

 

            $error = 0;

            if($error===FBIdentity::ERROR_NONE){

 

            $duration=3600*24*30; 

            Yii::app()->user->login($this->_identity,$duration);

            $this->redirect(Yii::app()->user->returnUrl);

 

            }else{

            return false;

            }

        }

}



  1. I added a FBIdentity under protected/components/ with this content:

<?php

class FBIdentity extends CUserIdentity{

 

    private $_id;

    public function authenticate(){

    $this->_id = $this->password;

    return $this->errorCode==self::ERROR_NONE;

    }

    public function getId()

    {

    return $this->_id;

    }

 

}

4 i added this code in layoot/main.php


<?php  

$this->widget('application.extensions.facebook.FbLogin',

array(

        'devappid'=>'',   //your appilaction id

        'devsecret'=>'', //your application secret

        'cookie'=>FALSE,   

));

Yii::app()->user->setReturnUrl($_SERVER['REQUEST_URI']);

?>

So when i connect with facebook i get just like that :

Array

(

[session_key] =>

[uid] =>

[expires] =>

[secret] =>

[base_domain] =>

[access_token] =>

[sig] =>

)

I think that i have the proble in public function actionFblogin(), maybe don’t works in sitecontroller ! :mellow: