Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to get a Facebook page's name with PHP?

Tags:

php

facebook

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?

like image 490
Dustin Avatar asked Nov 18 '11 16:11

Dustin


2 Answers

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

like image 200
Moz Morris Avatar answered Nov 13 '22 00:11

Moz Morris


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;
}
?>
like image 26
ifaour Avatar answered Nov 13 '22 00:11

ifaour