My apps launched to date have all been PHP/Javascript on the server side and Flash Clients. Right off the bat I realized Heroku is a Ruby on Rails shop and should have thought then "Oh I'm in for it." Apparently they made an exception for Facebook and support PHP applications, but ONLY if they are Facebook apps generated from the Facebook Developer app.
So I get started by simply following the instructions at the Facebook blog page.
This all worked flawlessly of course, if I were a Web site and not an APP! /doh So I need to modify the thing! Facebook tells me to go to Heroku's website and follow their instructions.
Following their instructions I break down at the "git clone" step of their command line interface, failing with the following:
Permission denied (publickey).
fatal: The remote end hung up unexpectedly
Wonderful! Long story short, the public key files created by the initial log on step were saved off in Users/User/.ssh and not in the /Git subdirectories. So I simply copied the private and public rsa files to Git/.ssh and it was finally able to do pushes to my new app on Heroku! Yay!
But now, on to the Facebook integration. I haven't done any Facebook coding since I first integrated Cruise Time last year so it felt like a time warp back to the desperate "oh shit" moments when I had to launch Cruise Time in 2 months and had not the faintest idea about facebook haha.
I cut and pasted alot of different, and conflicting examples from many places in Facebook's developer documentation with little success. I finally decided to stop beating around the bush and go right to the source, literally, and just headed to https://github.com/facebook/php-sdk to get the offical and latest Facebook PHP SDK. From the examples there I was able to publish my shiny new bootstrapping index.php to Heroku as follows:
<?php
// Enforce https on production
//if ($_SERVER['HTTP_X_FORWARDED_PROTO'] == "http" && $_SERVER['REMOTE_ADDR'] != '127.0.0.1') {
// header("Location: https://" . $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"]);
// exit();
//}
require_once("php-sdk/facebook.php");
$app_id = getenv('FACEBOOK_APP_ID');
$app_secret = getenv('FACEBOOK_SECRET');
$my_url = "http://pottw.herokuapp.com/index.php";
$config = array();
$config['appId'] = $app_id;
$config['secret'] = $app_secret;
$config['fileUpload'] = false; // optional
$facebook = new Facebook($config);
$user = $facebook->getUser();
if ($user) {
try {
$user_profile = $facebook->api('/me');
} catch (FacebookApiException $e) {
error_log($e);
$user = null;
}
} else {
$loginUrl = $facebook->getLoginUrl();
// redirect to login
}
?>
<html>
<head>
</head>
<body>
<?php if ($user): ?>
<h3>You</h3>
<img src="https://graph.facebook.com/<?php echo $user; ?>/picture">
<h3>Your User Object (/me)</h3>
<pre><?php print_r($user_profile); ?></pre>
<?php else: ?>
<strong><em>You are not Connected.</em></strong>
<?php endif ?>
</body>
</html>
But when I tried to run this, PHP threw the ol' "invalid file" error. I had copied the Facebook sdk into a subdirectory of my app tree and it was not automatically included in the commits. More web browsing and I found this gem of a resource: http://spheredev.org/wiki/Git_for_the_lazy . There I learned the command-line options to add a directory to my git source control (I'm a windows guy for 20+ years, so getting me to do command line work is really something! haha).
To my sincerest pleasure, Git was able to commit and push all of my new lovely source files, including the Facebook SDK to my shiny new Heroku app and on running it was rewarded with the output:
You
Your User Object (/me)
Array ( [id] => 1021302301 [name] => Dean Dino Gibson [first_name] => Dean [middle_name] => Dino [last_name] => Gibson [link] => http://www.facebook.com/DrumDinoX [username] => DrumDinoX [location] => Array ( [id] => [name] => ) [bio] => The Philanthropy of Metal! Drummer/Producer for Age & Treachery by night... On-line coder-extraordinaire by day! [work] => Array ( [0] => Array ( [employer] => Array ( [id] => 140261279380556 [name] => Dracos Software ) [location] => Array ( [id] => 107602962595896 [name] => Cypress, Texas ) [position] => Array ( [id] => 108323085888596 [name] => Principal Engineer ) [description] => Independant Gaming Contractor focused on the Facebook space. [start_date] => 2011-09 [projects] => Array ( [0] => Array ( [id] => 250060951707316 [name] => Code name "PvP" ) ) ) [1] => Array ( [employer] => Array ( [id] => 105336196171423 [name] => A Little Entertainment ) [location] => Array ( [id] => 106308306067511 [name] => The Woodlands, Texas ) [position] => Array ( [id] => 108323085888596 [name] => Principal Engineer ) [with] => Array ( [0] => Array ( [id] => 1597972162 [name] => Van Collins ) ) [description] => Facebook games! [start_date] => 2010-01 [end_date] => 2011-08 [projects] => Array ( [0] => Array ( [id] => 159463290798105 [name] => Space Trader Joe [start_date] => 2011-03 [end_date] => 2011-08 ) [1] => Array ( [id] => 105315876173199 [name] => Cruise Time [start_date] => 2010-09 [end_date] => 2011-03 ) ) ) [2] => Array ( [employer] => Array ( [id] => 106032226094178 [name] => New World Computing ) [start_date] => 1998-08 [end_date] => 2003-08 ) ) [education] => Array ( [0] => Array ( [school] => Array ( [id] => 106362256066529 [name] => Point Loma Senior High ) [type] => High School ) [1] => Array ( [school] => Array ( [id] => 108727889151725 [name] => George Washington University ) [year] => Array ( [id] => 142673469093081 [name] => 1993 ) [type] => College ) ) [gender] => male [timezone] => -5 [locale] => en_US [verified] => 1 [updated_time] => 2011-09-29T15:40:25+0000 )
So there you have it. My hair-greying attempt at bootstrapping a new Facebook app. The next step is to improve the redirect logic to handle the uninstalled user and automatically perform the OAuth2.0 authentication step and then I can move on and think about a better program flow for the PHP application for my new game code-named "PvP."
how to create tables i mean where to create in heroku
ReplyDeleteArray
(
[id] => 1021302301
[name] => Dean Dino Gibson
[first_name] => Dean
[middle_name] => Dino
[last_name] => Gibson
[link] => http://www.facebook.com/DrumDinoX
[username] => DrumDinoX
[location] => Array
like this
What i nead is i nead to put facebook registration plugin in my application,after user registers i nead to store that in database,so i nead to create tables like username..etc,so how can i....www
Deleteis it possible through heroku......(www.facebook.com/pr.amrita)
ReplyDeleteAt the time of this writing, no. Heroku requires you to use their own database solution which is most conveniently accessible through their Ruby on Rails stack... Their PhP stack, at the time of this writing, had no native access to a database layer...
ReplyDelete