in Facebook Graph API

In this article,  I would like to share my solution to get a Facebook group ID number from its URL.

Right now, this operation could not be done using Facebook Graph API or FQL. I have no idea why that old dated issue still not fixed by Facebook !

In this solution I have used a bot which login to Facebook, then go to the target group  and extracts group id in somewhere from the HTML content.

Here is the code source of a working solution based on PHP cURL library and DOM Xpath.


class FacebookBot {

    public static $HTTP_POST = "http-post";
    public static $HTTP_GET = "http-get";
    private $email;
    private $pass;
    private $uagent = "Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.9.2) Gecko/20100115 Firefox/3.6 (.NET CLR 3.5.30729)";
    private $cookies = "./cookies.txt";

    public function __construct($email, $pass) {
        $this->email = $email;
        $this->pass = $pass;


    private static function toFbMobileUrl($url) {
        return str_replace('//www.facebook.', '//m.facebook.', $url);

    private function callUrl($url, $calltype, $data, &$output) {
        try {
            $curl = curl_init();
            curl_setopt($curl, CURLOPT_URL, $url);
            curl_setopt($curl, CURLOPT_TIMEOUT, 30);
            curl_setopt($curl, CURLOPT_FOLLOWLOCATION, 1);
            curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1); //return or not output data
            curl_setopt($curl, CURLOPT_ENCODING, "");
            curl_setopt($curl, CURLOPT_COOKIEFILE, $this->cookies);
            curl_setopt($curl, CURLOPT_COOKIEJAR, $this->cookies);
            curl_setopt($curl, CURLOPT_USERAGENT, $this->uagent);

            if ($calltype == self::$HTTP_POST) {
                $tmp = array();
                foreach ($data as $param_key => $param_value) {
                    $tmp[] = "$param_key=$param_value";
                $data_str = implode("&", $tmp);
                curl_setopt($curl, CURLOPT_POST, 1);
                curl_setopt($curl, CURLOPT_POSTFIELDS, $data_str);
            $output = curl_exec($curl);
        } catch (Exception $e) {
            echo $e->getMessage();

    public function login() {
        $url = "";
        $post_data = array();
        $post_data['email'] = $this->email;
        $post_data['pass'] = $this->pass;
        $this->callUrl($url, self::$HTTP_POST, $post_data, $output);

    public function logout() {
        $this->callUrl("", self::$HTTP_GET, null, $html);

        $dom = new DOMDocument;
        $links = $dom->getElementsByTagName('a');
        foreach ($links as $link) {
            if (strpos($link->getAttribute('href'), 'logout.php')) {
                $logout = $link->getAttribute('href');
        $url = '' . $logout;
        $this->callUrl($url, self::$HTTP_GET, null, $html);

    public function destroyCookies() {

    public function getGroupIdByUrl($url) {
        try {
            $this->callUrl(self::toFbMobileUrl($url), self::$HTTP_GET, null, $html);
            $dom = new DOMDocument;
            $xpath = new DOMXpath($dom);
            $elements = $xpath->query("//a[starts-with(@href, '/groups/')]");
            if ($elements->length > 0) {
                $element = $elements->item(0);
                preg_match("/([0-9]+)/", $element->getAttribute('href'), $matches);
            } else {
                throw new Exception("could not find group id", 1);
            return $matches[0];
        } catch (Exception $e) {
            throw $e;




$group_url=<a facebook group url>;
$email=<your facebook login>;
$pass=<your facebook password>;

try {
    include_once './FacebookBot.php';
    $fbbot = new FacebookBot($email, $pass);
    $gid = $fbbot->getGroupIdByUrl($group_url);
    echo "Group id: ".$gid;
} catch (Exception $e) {
    die("An error has been occurred !");

Try it here (Live demo) :

If  you have found  a bug or an alternative solution, please dont’t hesitate to post a comment or send me a private message. Thank you.



Write a Comment