Dustin
Dustin

Reputation: 4459

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

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?

Upvotes: 6

Views: 5286

Answers (4)

user2251695
user2251695

Reputation: 129

this worked for me

$page_profile = $facebook->api('/' . $pageid);

$page_name = $page_profile['name'];

Upvotes: 0

Manos
Manos

Reputation: 1

You can try this:

$page_profile = $facebook->api('/' . $pageid);
echo "<a href='" . $page_profile['link'] . "'>" . $page_profile['link'] . "</a>";

Upvotes: 0

ifaour
ifaour

Reputation: 38135

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;
}
?>

Upvotes: 1

Moz Morris
Moz Morris

Reputation: 6771

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

Upvotes: 5

Related Questions