Easily manage test accounts for Facebook app developers

Recently, I had to use Facebook API for a web site using social networks. When testing these APIs, having test accounts is necessary. Facebook provides an API to create, delete and modify these friends. Here is a handy code snippet which makes it easy to batch create, delete, list test accounts and connect them as friends.

A zipped version is attached below.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html>
<head>
<title>Facebook test users</title>
</head>
<body>
<ul>
<li><a href="/">Home</a></li>
<li>Create test users
<ul>
<li><a href="?op=create&amount=1">1</a></li>
<li><a href="?op=create&amount=10">10</a></li>
<li><a href="?op=create&amount=50">50</a></li>
<li><a href="?op=create&amount=99">99 (max)</a></li>
</ul>
</li>
<li><a href="?op=list">List test users</a></li>
<li><a href="?op=friends">Make test users friends</a></li>
<li><a href="?op=delete">Delete all test users</a></li>
</ul>
<pre>
<?php
if (isset($_GET['op'])) {
require_once('facebook.php');
$app_id = 'YOUR_APP_ID';
$secret = 'YOUR_SECRET_KEY';
$facebook = new Facebook(array(
'appId' => $app_id,
'secret' => $secret,
'cookie' => true,
));
$session = $facebook->getSession();
$data = file_get_contents("https://graph.facebook.com/oauth/access_token?type=client_cred&client_id=$app_id&client_secret=$secret&redirect_uri=http://playground.localhost/");
$access_token = str_replace('access_token=', '', $data);
$attachment = array('access_token' => $access_token);
$users = $facebook->api("/$app_id/accounts/test-users", 'GET', $attachment);
switch ($_GET['op']) {
case 'create':
$amount = (int) $_GET['amount'];
if (!$amount) {
$amount = 10;
}
else if ($amount > 99) {
$amount = 99;
}
for ($i=0; $i<$amount; $i++) {
try {
$response = $facebook->api("/$app_id/accounts/test-users?installed=true&permissions=read_friendlists,user_about_me,email", 'POST', $attachment);
print_r($response);
}
catch (Exception $e) {
print $e->getMessage()."\n";
}
flush();
}
break;
case 'friends':
$users = $facebook->api("/$app_id/accounts/test-users", 'GET', $attachment);
foreach ($users['data'] as $user) {
foreach ($users['data'] as $friend) {
if ($user['id'] == $friend['id']) {
continue;
}
try {
$response = $facebook->api("/".$user['id']."/friends/".$friend['id'], 'POST', array('access_token' => $user['access_token']));
print "Success: ".$response."\n";
print_r($user);
}
catch (Exception $e) {
print $e->getMessage() . "\n";
}
try {
$response = $facebook->api("/".$friend['id']."/friends/".$user['id'], 'POST', array('access_token' => $friend['access_token']));
print "Success: ".$response."\n";
print_r($user);
}
catch (Exception $e) {
print $e->getMessage()."\n";
}
flush();
}
}
break;
case 'list':
try {
$response = $facebook->api("/$app_id/accounts/test-users", 'GET', $attachment);
print_r($response);
}
catch (Exception $e) {
print $e->getMessage()."\n";
}
flush();
break;
case 'delete':
$users = $facebook->api("/$app_id/accounts/test-users", 'GET', $attachment);
foreach ($users['data'] as $user) {
try {
$response = $facebook->api("/".$user['id'], 'DELETE', array('access_token' => $user['access_token']));
print "Success: ".$response."\n";
}
catch (Exception $e) {
print $e->getMessage()."\n";
}
flush();
}
break;
default:
print 'Please choose an operation';
}
}
?>
</pre>
</body>
</html>

AttachmentSize
fb-test-users.php_.zip1.63 KB