13

I've got the Facebook user ID of the user who created a given page. Now I need to get the page ID to display like box in my website.

Different users have their own Facebook pages; however, I know how to get their Facebook user IDs

hakre
  • 193,403
  • 52
  • 435
  • 836
kasun
  • 131
  • 1
  • 1
  • 6

9 Answers9

13

You can replace www to graph in the page URL. For example id of the Coca-Cola page http://graph.facebook.com/cocacola (first value).

Anton Melnikov
  • 650
  • 4
  • 9
  • Its nice to know you can get info like this, but I do not see anywhere that it lists "pages" information. Is there another step that needs to be taken here? – James Dec 10 '12 at 18:45
  • nice link to know any of the page's page-id . +1 thanks – Esha Aug 27 '14 at 07:31
5

The URL to the fan page will have the ID in it. For example, the Stack Overflow fan page

http://www.facebook.com/pages/Stack-Overflow/105665906133609

The ID is 105665906133609

Some pages have aliases, which are also usable as the ID. For example, the Marine Corps' fan page is

http://www.facebook.com/marinecorps

So the ID is marinecorps

Alternatively, you can use the generator on the documentation page as well.

Peter Bailey
  • 105,256
  • 31
  • 182
  • 206
  • i have try to do this,there is no way to get fan page id via creater id, at the end i get id from the user who likes the page,(creater also need to likea that page) – kasun Jun 29 '10 at 06:15
  • You don't need to use the creator's ID at all. I'm not sure where you're going with this. – Peter Bailey Jun 29 '10 at 14:29
2

The page id is included in the signed request. Here an updated version of byron's post for facebook's page update.

$facebook = new Facebook();

$signed_request = $facebook->getSignedRequest();
$page_id = $signed_request['page']['id'];

echo 'page id is ' . $page_id . ' AAAAAAAAAAWWWWWWW YYYYYYEEEEEEEEAAAAAAAAAAAA!';
John Himmelman
  • 21,504
  • 22
  • 65
  • 80
1

this works using the new php sdk

$page_id = null;
$facebook = new Facebook();
try {
    $signed_request = $facebook->getSignedRequest();
    $page_id = $signed_request['profile_id'];
} catch (FacebookApiException $e) {
    error_log($e);
}

please note that i'm upset that this works, because what i'm actually looking for is the user id of the page creator since I need to display content created by that user on the tab. this used to be possible, but i don't think it is anymore. kasun, how are you getting the id of the page creator?

byron
  • 984
  • 3
  • 14
  • 26
0

I am presuming that you are the user and you want the page id of a page that you are the admin of.

To get the id is fairly straightforward.

First, you need to get a list of all pages associated with the user id:

$fanpages = $facebook->api('me/accounts?access_token='.$accessToken);

You would then do a loop like:

foreach ($fanpages['data'] as $fanpage)
{
echo "<br />Fan Page Name: ".$fanpage['name'] . " ID: ".$fanpage['id'];
}

That would list all the pages associated with the id.

For a full list of the elements of the $fanpage array, take a look here:

https://developers.facebook.com/docs/reference/api/page/

Relaxing In Cyprus
  • 1,976
  • 19
  • 25
0

These answers don't seem to work anymore since the graph now requires an access token for most if not all requests.

Here's a working solution that only requires as input the facebook page URL.

What this does is receives the facebook page as HTML and looks through(via regex) for a JSON entity_id that has a numeric value. This value is the page's ID. This solution is surely not guaranteed to work forever since page content may change

/**
 * @param string $facebookUrl
 * @return null|string
 */
function get_facebook_id($facebookUrl)
{
    $facebookId = null;
    $fbResponse = @file_get_contents($facebookUrl);
    if($fbResponse)
    {
        $matches = array();
        if (preg_match('/"entity_id":"([0-9])+"/', $fbResponse, $matches))
        {
            $jsonObj = json_decode("{" . $matches[0] . "}");
            if($jsonObj)
            {
                $facebookId = $jsonObj['entity_id'];
            }
        }
    }
    return $facebookId;
}
james
  • 26,141
  • 19
  • 95
  • 113
0
$pages = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT page_id FROM page_admin WHERE uid = '.$uid.''
));

$uid being the profile id# of the FB user! Fql query using PHP SDK

Brandon
  • 86
  • 10
0

goto http://www.facebook.com/insights/ after you logged in to your facebook

press the green button on top right ( "insight for your domain") select the drop down value for your page voila, you see the page_id

(I saw this on facebook forum)

kite
  • 1,478
  • 1
  • 15
  • 27
0

Here are list of functions which you can add in facebook sdk. Works for both graph api and rest api.

/*
    Custom functions to get fan page related information
*/
function getSignedData()
{
    if(isset($this->signedData) && !empty($this->signedData))
        return $this->signedData;

    if(!isset($_REQUEST["signed_request"]))
        return false;

    $signed_request = $_REQUEST["signed_request"];

    if(empty($signed_request))
        return false;

    list($encoded_sig, $payload) = explode('.', $signed_request, 2);
    $data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);

    if(empty($data))
        return false;

    $this->signedData = $data;



    return $data;
}

/*
    Return fan page id, Only return fanpage id for 1st (landing) page of application.
*/
function getFanPageId()
{
    if(!$data = $this->getSignedData())
    {
        return false;
    }
    if(isset($data["page"]["id"]))
        return $data["page"]["id"];

    return false;
}

/*
    Only returns userid, if user has authenticated application
*/
function getFanPageUserId()
{
    if(!$data = $this->getSignedData())
    {
        return false;
    }

    if(isset($data["user_id"]))
    {
        return $data["user_id"];
    }

    return false;
}

/*
Check if visiting user is fan page admin
*/
function checkFanPageAdmin()
{
    if(!$data = $this->getSignedData())
        return false;

    if(isset($data["page"]["admin"]) && $data["page"]["admin"] == 1)
        return true;

    return false;
}
Shahzad Malik
  • 541
  • 4
  • 6