From the archive: first published in September 2013 on my old blog, Tux86’s Web Tech Blog. Versions, links and screenshots are from that time.
In this article, I would like to share my solution for getting a Facebook group ID number from the group’s URL.
Right now, this cannot be done with the Facebook Graph API or FQL. I have no idea why such an old issue still hasn’t been fixed by Facebook!
My solution uses a bot that logs in to Facebook, goes to the target group and extracts the group ID from somewhere in the HTML content.
Here is the source code of a working solution based on the PHP cURL library and DOMXPath.
FacebookBot.php
<?php
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;
touch($this->cookies);
}
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);
curl_close($curl);
unset($curl);
} catch (Exception $e) {
echo $e->getMessage();
}
}
public function login() {
$url = "https://m.facebook.com/login.php";
$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("https://m.facebook.com/", self::$HTTP_GET, null, $html);
$dom = new DOMDocument;
@$dom->loadxml($html);
$links = $dom->getElementsByTagName('a');
foreach ($links as $link) {
if (strpos($link->getAttribute('href'), 'logout.php')) {
$logout = $link->getAttribute('href');
break;
}
}
$url = 'https://m.facebook.com' . $logout;
$this->callUrl($url, self::$HTTP_GET, null, $html);
}
public function destroyCookies() {
unlink($this->cookies);
}
public function getGroupIdByUrl($url) {
try {
$this->callUrl(self::toFbMobileUrl($url), self::$HTTP_GET, null, $html);
$dom = new DOMDocument;
@$dom->loadHTML($html);
$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;
}
}
}
GetGroupIdFromUrl.php
<?php
$group_url=<a facebook group url>;
$email=<your facebook login>;
$pass=<your facebook password>;
try {
include_once './FacebookBot.php';
$fbbot = new FacebookBot($email, $pass);
$fbbot->login();
$gid = $fbbot->getGroupIdByUrl($group_url);
$fbbot->logout();
$fbbot->destroyCookies();
echo "Group id: ".$gid;
} catch (Exception $e) {
die("An error has been occurred !");
}
If you find a bug or have an alternative solution, please don’t hesitate to let me know. Thank you.