1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
set_time_limit(0);
// Hamish - 27 july 2010
// use charles http proxy http://www.charlesproxy.com/ to get the cookie string and node_id, post_form_id, fb_dtsg for the $postFields string
// you need to have curl enabled on your web server

$fnRaw = "posh_fb_output.txt";
$fnCsv = "posh_fb_output.csv";

// set settings in these 4 lines from results of charles when getting the 2nd page of "Get All Fans" in FB ( you need to be admin of fan page to do this )
$cookie="datr=123123123123123; lo=123123123; lxs=1; locale=en_US; x-referer=http%3A%2F%2Fwww.facebook.com%2Fname%3Fref%3Dts%23%2Fhome.php; cur_max_lag=2; lsd=2wrNo; c_user=112312312313; lxe=name%40name.com; sct=123123123; xs=123123123; presence=D12312321323213";
$node_id= "12312313123";
$post_form_id = "123123123";
$fb_dtsg = "QT-pZ";
// end settings

$url= "http://www.facebook.com/ajax/social_graph/fetch.php";
$ch = curl_init();
$stop = false;
$page=0;
while(!$stop)
{
$postFields = "page=".$page."&edge_type=fan&limit=100&class=FanManager&node_id=".$node_id."&post_form_id=".$post_form_id."&fb_dtsg=".$fb_dtsg."&post_form_id_source=AsyncRequest&__a=1";

curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt ($ch, CURLOPT_POST, 1);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postFields);
curl_setopt ($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 1.1.4322; .NET CLR 2.0.50727; .NET CLR 3.0.04506.30; MDDR; .NET CLR 3.0.04506.648; .NET CLR 3.5.21022; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)");
curl_setopt($ch, CURLOPT_COOKIE, $cookie);
$r = curl_exec ($ch);
WriteOut($fnRaw,$r);

echo "got page: ".$page."";
ob_flush();
if(preg_match ('/"user_info":null/' ,$r,$matches) > 0) $stop = true;
$page++;
}
curl_close ($ch);

// decode file
WriteOut($fnCsv,"ID,PROFILE HREF,TITLE, SUBTITLE, PICTURE URL");
$fp = fopen($fnRaw,"r");
while (!feof($fp))
{
$line = fgets($fp, filesize($fnRaw));
$line = str_replace("for (;;);","",$line);
$j = json_decode($line,true);
if($j["payload"]["user_info"])
{
foreach($j["payload"]["user_info"] as $p)
{
WriteOut($fnCsv,$p["id"].",".$p["href"].",".$p["title"].",".$p["subtitle"].",".$p["pic"]);
}
}
}
fclose($fp);
echo "complete - have a great day.";

function WriteOut ($fn,$theData) {
$fp = fopen($fn,"a");
fwrite($fp,$theData."\r\n");
fclose($fp);
}
?>