@Hardik Mistry
Hardik Mistry, on 31 December 2012 - 04:58 AM, said:
PLzzz help me m new to yii and want to integrate facebook sign in i have downloaded extension from yiiframework.com named:facebook_connect but i dnt know how to do it working????you cn reply me @hardikmist@gmail.com
use this component.place this file in protected/components folder with Facebook.php name
<?php
class Facebook extends CApplicationComponent
{
public $signedRequest;
public $permissions;
public $appId;
public $appSecret;
public $tabUrl;
protected $curl;
protected $me;
/**
* Initializes this application component.
* This method is required by the IApplicationComponent interface.
*/
public function init()
{
parent::init();
// Get the signed request
if(isset($_REQUEST['signed_request']))
$this->signedRequest = $this->parseSignedRequest($_REQUEST['signed_request']);
// Initialize the cURL session
$this->curl = curl_init();
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, 1);
}
/**
* Session storage for the most utilised function
*/
public function getMe($params = array())
{
if(!$this->me)
$this->me = $this->getCurl('me', $params);
return $this->me;
}
/**
* Wrapper cURL function
*/
public function getCurl($query, $params = array(), $jsonDecode = true) {
// Get the access token
$accessToken = '';
if(!isset($params['access_token']))
{
preg_match('/accessToken=[^&]+/', Yii::app()->user->model->session, $matches);
$accessToken = str_replace('accessToken', 'access_token', $matches[0]);
}
// Form the query
$paramString = '';
foreach($params as $key => $val)
$paramString .= $key.'='.rawurlencode($val).'&';
$request = 'https://graph.facebook.com/'.$query.'?'.$paramString.$accessToken;
// Execute the cURL session
curl_setopt($this->curl, CURLOPT_URL, $request);
$contents = curl_exec($this->curl);
// Parse the response
switch(curl_getinfo($this->curl, CURLINFO_HTTP_CODE)) {
case 400: // Error
return false;
case 200: // Success
default:
return $jsonDecode ? json_decode($contents) : $contents;
}
}
/**
* See http://developers.facebook.com/docs/authentication/signed_request/
*/
public function parseSignedRequest($signedRequest) {
list($encodedSig, $payload) = explode('.', $signedRequest, 2);
// Decode the data
$sig = $this->base64UrlDecode($encodedSig);
$data = json_decode($this->base64UrlDecode($payload), true);
if(strtoupper($data['algorithm']) !== 'HMAC-SHA256')
{
error_log('Unknown algorithm. Expected HMAC-SHA256');
return null;
}
// Check sig
$expectedSig = hash_hmac('sha256', $payload, $this->appSecret, $raw = true);
if($sig !== $expectedSig)
{
error_log('Bad Signed JSON signature!');
return null;
}
return $data;
}
/**
* See http://developers.facebook.com/docs/authentication/signed_request/
*/
protected function base64UrlDecode($input)
{
return base64_decode(strtr($input, '-_', '+/'));
}
/**
* Clean-up
*/
public function __destruct() {
curl_close($this->curl);
}
}
Add some thing like this In your config/main.php components array like this:
........
........
'facebook'=>array(
'class'=>'Facebook',
'permissions'=>'',
'appId'=>'xxxxxxxxxxxxxxx',
'appSecret'=>'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx',
'tabUrl'=>'http://www.facebook.com/appname/app_xxxxxxxxxxxxxxx',
),
........
........
--hope this helps