php 获取 Facebook 粉丝页面 ID

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3130433/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 08:47:08  来源:igfitidea点击:

Get Facebook fan page ID

phpfacebookfacebook-graph-api

提问by kasun

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.

我有创建给定页面的用户的 Facebook 用户 ID。现在我需要获取页面 ID 以在我的网站中显示类似框。

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

不同的用户有自己的 Facebook 页面;但是,我知道如何获取他们的 Facebook 用户 ID

回答by Anton Melnikov

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).

您可以将 www 替换为页面 URL 中的图形。例如可口可乐页面http://graph.facebook.com/cocacola 的id (第一个值)。

回答by Peter Bailey

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

粉丝页面的 URL 将包含 ID。例如,Stack Overflow 粉丝页面

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

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

The ID is 105665906133609

ID是105665906133609

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

某些页面具有别名,也可用作 ID。例如,海军陆战队的粉丝页面是

http://www.facebook.com/marinecorps

http://www.facebook.com/marinecorps

So the ID is marinecorps

所以ID是海军陆战队

Alternatively, you can use the generator on the documentation pageas well.

或者,您也可以在文档页面上使用生成器。

回答by John Himmelman

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

页面 ID 包含在签名请求中。这是拜伦发布的 Facebook 页面更新的更新版本。

$facebook = new Facebook();

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

echo 'page id is ' . $page_id . ' AAAAAAAAAAWWWWWWW YYYYYYEEEEEEEEAAAAAAAAAAAA!';

回答by byron

this works using the new php sdk

这可以使用新的 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?

请注意,我对此感到不安,因为我实际上正在寻找页面创建者的用户 ID,因为我需要在选项卡上显示该用户创建的内容。这曾经是可能的,但我认为不再可能了。kasun,你如何获得页面创建者的ID?

回答by Shahzad Malik

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

以下是您可以在 facebook sdk 中添加的功能列表。适用于图形 api 和 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;
}

回答by Relaxing In Cyprus

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

我假设您是用户并且您想要您是管理员的页面的页面 ID。

To get the id is fairly straightforward.

获取 id 相当简单。

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

首先,您需要获取与用户 ID 关联的所有页面的列表:

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

$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.

这将列出与该 ID 关联的所有页面。

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

有关 $fanpage 数组元素的完整列表,请查看此处:

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

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

回答by james

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.

这是一个工作解决方案,只需要输入 facebook 页面 URL。

What this does is receives the facebook page as HTML and looks through(via regex) for a JSON entity_idthat 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

这样做是将 Facebook 页面作为 HTML 接收,并通过(通过正则表达式)查找entity_id具有数值的 JSON 。该值是页面的 ID。这个解决方案肯定不能保证永远有效,因为页面内容可能会改变

/**
 * @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;
}

回答by Brandon

$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

$uid 是 FB 用户的个人资料 ID#!使用 PHP SDK 进行 Fql 查询

回答by kite

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

登录 Facebook 后转到http://www.facebook.com/insights/

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

按右上角的绿色按钮(“您的域的洞察力”)为您的页面选择下拉值,您会看到 page_id

(I saw this on facebook forum)

(我在facebook论坛上看到了这个)