Skip to content
This repository was archived by the owner on Oct 16, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
vendor
.DS_*
3 changes: 0 additions & 3 deletions .gitmodules

This file was deleted.

15 changes: 15 additions & 0 deletions AppInfo.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,4 +46,19 @@ public static function getUrl($path = '/') {
return $protocol . $_SERVER['HTTP_HOST'] . $path;
}



/**
* @return hardcoded appName for this app
*/
public static function appName() {
$name=getenv('APP_NAME');
if($name)
return $name;
else
return "APP_NAME";
}



}
1 change: 1 addition & 0 deletions Procfile
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
web: vendor/bin/heroku-php-apache2
6 changes: 6 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"require" : {
"facebook/php-sdk-v4" : "4.0.*",
"ext-mbstring": "*"
}
}
76 changes: 76 additions & 0 deletions composer.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

159 changes: 80 additions & 79 deletions index.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@

// This provides access to helper functions defined in 'utils.php'
require_once('utils.php');

use Facebook\FacebookCanvasLoginHelper;

/*****************************************************************************
*
Expand All @@ -31,52 +31,72 @@
*
****************************************************************************/

require_once('sdk/src/facebook.php');

$facebook = new Facebook(array(
'appId' => AppInfo::appID(),
'secret' => AppInfo::appSecret(),
'sharedSession' => true,
'trustForwarded' => true,
));

$user_id = $facebook->getUser();
if ($user_id) {
try {
// Fetch the viewer's basic information
$basic = $facebook->api('/me');
} catch (FacebookApiException $e) {
// If the call fails we check if we still have a user. The user will be
// cleared if the error is because of an invalid accesstoken
if (!$facebook->getUser()) {
header('Location: '. AppInfo::getUrl($_SERVER['REQUEST_URI']));
exit();
}
}

// This fetches some things that you like . 'limit=*" only returns * values.
// To see the format of the data you are retrieving, use the "Graph API
// Explorer" which is at https://developers.facebook.com/tools/explorer/
$likes = idx($facebook->api('/me/likes?limit=4'), 'data', array());

// This fetches 4 of your friends.
$friends = idx($facebook->api('/me/friends?limit=4'), 'data', array());

// And this returns 16 of your photos.
$photos = idx($facebook->api('/me/photos?limit=16'), 'data', array());

// Here is an example of a FQL call that fetches all of your friends that are
// using this app
$app_using_friends = $facebook->api(array(
'method' => 'fql.query',
'query' => 'SELECT uid, name FROM user WHERE uid IN(SELECT uid2 FROM friend WHERE uid1 = me()) AND is_app_user = 1'
));
use Facebook\FacebookSession;
use Facebook\FacebookRequest;
use Facebook\GraphUser;
use Facebook\FacebookJavaScriptLoginHelper;
FacebookSession::setDefaultApplication(AppInfo::appID(), AppInfo::appSecret());
$app_name = AppInfo::appName();
$helper = new FacebookCanvasLoginHelper();


try {
$session = $helper->getSession();
} catch(FacebookRequestException $ex) {


// When Facebook returns an error
} catch(\Exception $ex) {


// When validation fails or other local issues
}


if ($session) {
try {

$user_profile = (new FacebookRequest(
$session, 'GET', '/me'
))->execute()->getGraphObject(GraphUser::className());

//echo "Name: " . $
$user_id = $user_profile -> getId();
} catch(FacebookRequestException $e) {

echo "Exception occured, code: " . $e->getCode();
echo " with message: " . $e->getMessage();


}

$app_info = (new FacebookRequest(
$session, 'GET', '/' . AppInfo::appID()
))->execute()->getGraphObject();


$app_name = $app_info->getProperty('name');



$likes = (new FacebookRequest(
$session, 'GET', '/me/likes?limit=4'
))->execute()->getGraphObject()->getProperty('data')->asArray();


$friends = (new FacebookRequest(
$session, 'GET', '/me/friends?limit=4'
))->execute()->getGraphObject()->getProperty('data')->asArray();


$photos = (new FacebookRequest(
$session, 'GET', '/me/photos?limit=4'
))->execute()->getGraphObject()->getProperty('data')->asArray();


}

// Fetch the basic info of the app that they are using
$app_info = $facebook->api('/'. AppInfo::appID());

$app_name = idx($app_info, 'name', '');

?>
<!DOCTYPE html>
Expand Down Expand Up @@ -209,14 +229,14 @@ function (response) {
</script>

<header class="clearfix">
<?php if (isset($basic)) { ?>
<p id="picture" style="background-image: url(https://graph.facebook.com/<?php echo he($user_id); ?>/picture?type=normal)"></p>
<?php if (isset($session)) { ?>
<p id="picture" style="background-image: url(https://graph.facebook.com/<?php echo he($user_profile->getId()); ?>/picture?type=normal)"></p>

<div>
<h1>Welcome, <strong><?php echo he(idx($basic, 'name')); ?></strong></h1>
<h1>Welcome, <strong><?php echo he($user_profile->getName()); ?></strong></h1>
<p class="tagline">
This is your app
<a href="<?php echo he(idx($app_info, 'link'));?>" target="_top"><?php echo he($app_name); ?></a>
<a href="<?php echo he($app_info->getProperty('link'));?>" target="_top"><?php echo he($app_name); ?></a>
</p>

<div id="share-app">
Expand All @@ -243,7 +263,7 @@ function (response) {
<?php } else { ?>
<div>
<h1>Welcome</h1>
<div class="fb-login-button" data-scope="user_likes,user_photos"></div>
<div class="fb-login-button" data-scope="user_likes,user_friends,user_photos"></div>
</div>
<?php } ?>
</header>
Expand All @@ -254,20 +274,20 @@ function (response) {
</section>

<?php
if ($user_id) {
if ($session) {
?>

<section id="samples" class="clearfix">
<h1>Examples of the Facebook Graph API</h1>

<div class="list">
<h3>A few of your friends</h3>
<h3>Friends using this app</h3>
<ul class="friends">
<?php
foreach ($friends as $friend) {
// Extract the pieces of info we need from the requests above
$id = idx($friend, 'id');
$name = idx($friend, 'name');
$id = $friend->id;
$name = $friend->name;
?>
<li>
<a href="https://www.facebook.com/<?php echo he($id); ?>" target="_top">
Expand All @@ -288,9 +308,11 @@ function (response) {
$i = 0;
foreach ($photos as $photo) {
// Extract the pieces of info we need from the requests above
$id = idx($photo, 'id');
$picture = idx($photo, 'picture');
$link = idx($photo, 'link');


$id = $photo->id;
$picture = $photo->picture;
$link = $photo->link;

$class = ($i++ % 4 === 0) ? 'first-column' : '';
?>
Expand All @@ -309,9 +331,8 @@ function (response) {
<?php
foreach ($likes as $like) {
// Extract the pieces of info we need from the requests above
$id = idx($like, 'id');
$item = idx($like, 'name');

$id = $like->id;
$item = $like->name;
// This display's the object that the user liked as a link to
// that object's page.
?>
Expand All @@ -327,26 +348,6 @@ function (response) {
</ul>
</div>

<div class="list">
<h3>Friends using this app</h3>
<ul class="friends">
<?php
foreach ($app_using_friends as $auf) {
// Extract the pieces of info we need from the requests above
$id = idx($auf, 'uid');
$name = idx($auf, 'name');
?>
<li>
<a href="https://www.facebook.com/<?php echo he($id); ?>" target="_top">
<img src="https://graph.facebook.com/<?php echo he($id) ?>/picture?type=square" alt="<?php echo he($name); ?>">
<?php echo he($name); ?>
</a>
</li>
<?php
}
?>
</ul>
</div>
</section>

<?php
Expand Down
1 change: 0 additions & 1 deletion sdk
Submodule sdk deleted from bf9992
6 changes: 4 additions & 2 deletions stylesheets/base.css
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,10 @@ body {
color: #999;
font: 14px/1.5em "Lucida Grande", Arial, sans-serif;
-webkit-font-smoothing: antialiased;
width: 720px;
margin: 20px;
width: 100%-20;

margin: 20px

}

a:link, a:visited {
Expand Down
2 changes: 2 additions & 0 deletions utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@ function idx(array $array, $key, $default = null) {
function he($str) {
return htmlentities($str, ENT_QUOTES, "UTF-8");
}

require 'vendor/autoload.php';