Get Facebook fan page ID

前端 未结 9 1689
名媛妹妹
名媛妹妹 2021-02-04 08:32

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 Face

9条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-04 09:12

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

提交回复
热议问题