I'm building an iframe app that will have a few mentions of the Facebook Page is lives on. My app will be added to multiple pages with different names. What I need is something like this..
$page_name = "Bob's Toys";
Thank you for visiting the <?php echo $page_name; ?> page!
Is there a way to do this?
Yes there is. Decode the signed_request sent to the page by Facebook.
if (!empty($_REQUEST['signed_request'])) {
$signedRequest = $_REQUEST['signed_request'];
list($sig, $payload) = explode('.', $signedRequest, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
}
From that, you can get the page's id.
array (size=4)
'algorithm' => string 'HMAC-SHA256' (length=11)
'issued_at' => int 1321635439
'page' =>
array (size=3)
'id' => string '19292868552' (length=15)
'liked' => boolean false
'admin' => boolean true
'user' =>
array (size=3)
'country' => string 'gb' (length=2)
'locale' => string 'en_US' (length=5)
'age' =>
array (size=1)
'min' => int 21
Then you can use the Graph API to return the page object that would look like this: https://graph.facebook.com/19292868552
Just get the page id, query the graph with name
field only. something like:
<?php
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (!empty($data["page"])) {
$page_info = json_decode(file_get_contents("https://graph.facebook.com/{$data['page']['id']}?fields=name"));
echo $page_info->name;
}
?>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With