Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Create Controller TestUsersController

  • Loading branch information...
commit 44480cfa6f3ba7bd782e5a0c0f299104d29951c1 1 parent 65e0561
@aizatto authored
View
87 Controller/TestUsersController.php
@@ -0,0 +1,87 @@
+<?php
+
+namespace Aizatto\Bundle\FacebookBundle\Controller;
+
+use Symfony\Bundle\FrameworkBundle\Controller\Controller;
+use Symfony\Component\HttpFoundation\Response;
+
+/**
+ * https://developers.facebook.com/docs/test_users/
+ */
+class TestUsersController extends Controller
+{
+
+ CONST
+ APC_KEY = 'facebook:test-users',
+ APC_CREATED_AT_KEY = 'facebook:test-users:created-at';
+
+ public function indexAction() {
+ $success1 = $success2 = false;
+ $users = apc_fetch(self::APC_KEY, $success1);
+ $created_at = apc_fetch(self::APC_CREATED_AT_KEY, $success2);
+
+ if (!$success1 || !$success2) {
+ $users = $this->fetchTestUsers();
+ $created_at = time();
+ apc_store(self::APC_KEY, $users, 600);
+ apc_store(self::APC_CREATED_AT_KEY, $created_at);
+ }
+
+ $content = $this->renderView(
+ 'AizattoFacebookBundle:TestUsers:index.html.php',
+ array(
+ 'users' => $users,
+ 'created_at' => $created_at,
+ )
+ );
+ return new Response($content);
+ }
+
+ public function fetchTestUsers() {
+ $facebook = $this->get('facebook');
+ $app_id = $facebook->getAppID();
+ $access_token = $app_id.'|'.$facebook->getApiSecret();
+ $query = array('access_token' => $access_token);
+ $data = $facebook->api($app_id.'/accounts/test-users', 'GET', $query);
+
+ $users = array();
+ $batch = array();
+ foreach ($data['data'] as $user) {
+ $query = '';
+ if (isset($user['access_token'])) {
+ $query = '?access_token='.$user['access_token'];
+ }
+
+ $users[$user['id']] = $user;
+ $batch[] = array(
+ 'method' => 'GET',
+ 'relative_url' => $user['id'].$query
+ );
+ }
+
+ // facebook allows you to batch 20 requests at a time
+ $batches = array_chunk($batch, 20);
+ foreach ($batches as $batch) {
+ $responses = $this->get('facebook')->api(
+ '',
+ 'POST',
+ array('batch' => $batch));
+
+ foreach ($responses as $response) {
+ if ($response['code'] != 200) {
+ continue;
+ }
+
+ $json = json_decode($response['body'], true);
+ $id = $json['id'];
+ if (!isset($users[$id])) {
+ continue;
+ }
+ $users[$id] = array_merge($users[$id], $json);
+ }
+ }
+
+ return $users;
+ }
+
+}
View
14 Resources/views/TestUsers/base.html.php
@@ -0,0 +1,14 @@
+<!DOCTYPE html>
+<html xmlns:fb="http://www.facebook.com/2008/fbml" lang="en">
+ <head>
+ <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
+ <meta http-equiv="Content-Language" content="en-US">
+ <link rel="stylesheet" href="http://twitter.github.com/bootstrap/assets/css/bootstrap.css" />
+ <title>Facebook</title>
+ </head>
+ <body>
+ <div class="container">
+ <?php $view['slots']->output('_content') ?>
+ </div>
+ </body>
+</html>
View
51 Resources/views/TestUsers/index.html.php
@@ -0,0 +1,51 @@
+<?php $view->extend('AizattoFacebookBundle:TestUsers:base.html.php'); ?>
+
+<h1>Facebook Test Users (<?php echo count($users); ?>)</h1>
+<p>Last Fetched: <?php echo date(DATE_RFC1036, $created_at); ?></p>
+<p>Remember to <strong>login from a new browser</strong>.</p>
+<p>Else you will be logged out of your Facebook session.</p>
+<p>
+ These Login URLs only exist for 10 minutes at a time, and can only be used once.
+</p>
+<?php foreach ($users as $id => $user): ?>
+ <?php if (isset($user['name'])): ?>
+ <h2><?php echo $user['name']; ?> (<em><?php echo $id; ?></em>)</h2>
+ <?php else: ?>
+ <h2><?php echo $id; ?></h2>
+ <?php endif; ?>
+ <table class="zebra-striped">
+ <tbody>
+ <tr>
+ <td style="width: 100px">id</td>
+ <td><?php echo $id; ?></td>
+ </tr>
+ <?php if (isset($user['name'])): ?>
+ <tr>
+ <td>name</td>
+ <td><?php echo $user['name']; ?></td>
+ </tr>
+ <?php endif; ?>
+ <tr>
+ <td>login url</td>
+ <td>
+ <input type="text" value="<?php echo $user['login_url']; ?>" style="width: 80%" />
+ <a href="<?php echo $user['login_url']; ?>">Switch to user</a>
+ </td>
+ </tr>
+ <?php if (isset($user['access_token'])): ?>
+ <tr>
+ <td>name</td>
+ <td>
+ <input type="text" value="<?php echo $user['access_token']; ?>" style="width: 80%" />
+ </td>
+ </tr>
+ <?php endif; ?>
+ <tr>
+ <td>keys</td>
+ <td>
+ <?php echo implode(', ', array_keys($user)); ?>
+ </td>
+ </tr>
+ </tbody>
+ </table>
+<?php endforeach; ?>
Please sign in to comment.
Something went wrong with that request. Please try again.