At first download facebook sdk for php from here:
https://docs.google.com/file/d/0B5nZNPW48dpFbjZiSHNybEh0czg/edit?usp=sharing
App id and secret code goes here as
$this->facebookObject = new Facebook(array(
'appId' => $this->appId,
'secret' => $this->secret,
));
this will return facebook object.
To get signed request by pages,
$this->signedRequest = $this->facebookObject->getSignedRequest();
If signed request dont come from a page you can die immediately
if (!isset($this->signedRequest["page"]["id"])) {
die();
}
Get facebook user id if need
$this->facebookUser = $this->facebookObject->getUser();
If user is not logged in then retrieve login url
$loginMainUrl = $this->facebookObject->getLoginUrl();
** retrieve login return url
$loginReturnUrl = $this->getLoginReturnUrl($this->signedRequest["page"]["id"]);
$loginReturnUrl = urlencode($loginReturnUrl."?sk=app_".$this->appId);
$loginMainUrls = explode("&", urldecode($loginMainUrl));
$loginMainUrlNew = "";
foreach($loginMainUrls as $temp) {
if($loginMainUrlNew != "") {
$loginMainUrlNew .= "&";
}
if(substr($temp, 0, 12) == "redirect_uri") {
$loginMainUrlNew .= "redirect_uri=".$loginReturnUrl;
} else {
$loginMainUrlNew .= ($temp);
}
}
<a target="_parent" href="<?php echo $loginMainUrlNew; ?>"><img src="<?php echo Router::url('/', true); ?>img/fb_login.jpg" alt="Login With Facebook"></a>
Get facebook pages link by implementing this function:
function getLoginReturnUrl($pageId) {
$url="https://graph.facebook.com/".$pageId;
$ch = curl_init();
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch,CURLOPT_URL,$url);
curl_setopt($ch,CURLOPT_RETURNTRANSFER,1);
curl_setopt($ch,CURLOPT_CONNECTTIMEOUT,2);
$content = curl_exec($ch);
$content = json_decode($content);
if(isset($content->link) && strlen($content->link) > 0) {
return $content->link;
}
return "";
}
When anyone add this apps to his/her page then do the following:
foreach($_GET as $key=>$value) {
if($key == "tabs_added") {
if(count($value) > 0) {
foreach($value as $key2=>$value2) {
if($key2 != null && strlen($key2) > 0) {
$url = $this->getLoginReturnUrl($key2)."?sk=app_".$this->appId;
if($url != null && substr($url, 0, 4) == "http") {
header("Location: ".$url);
}
}
}
}
}
}
this will redirect you to proper application page under the page which you add add this apps.
Add this html to your page to make user to add the apps to there pages
<?php
$urlToAddThisApp = "http://www.facebook.com/dialog/pagetab?app_id=".Configure::read("facebookAppId")."&next=".urlencode(Router::url("/", true)."apps/facebook/thank_you");
$urlToAddThisApp .= "&skip_api_login=1&display=popup";
$urlToAddThisApp .= "&cancel_url=".urlencode(Router::url("/", true)."apps/facebook/cancel");
?>
<div>
<a href="javascript:void(0);" onclick="open_win();">Add this app</a>
</div>
<script>
function open_win() {
var width = 500;
var height = 350;
var left = (screen.width/2)-(width/2);
var top = 50;
myWindow=window.open('<?php echo $urlToAddThisApp; ?>','','width='+width+', height='+height+', top='+top+', left='+left);
myWindow.focus();
}
</script>