Getting started with a facebook application
I'm really new to Facebook application development, and I'm quite confused about application permissions and why the php sdk is so limiting and has so few precoded functions.
First off, how do I check if a user has set proper permissions for my application, and if not, display that standard dialog immediately instead of using FBML and making them click the link?
Secondly, how do I publish to a user's stream with the php apis?
Finally, are there any good tutorial sites for making a php based canvas application that use the latest versions of all the sdks?
Thank you for your help! I really don't see why so much of this is Javascript, it would really make much more sense to me to have things like: if(!$facebook->appHasPerm('publish_stream'){$facebook->showPermDialog('publish_stream');}
I'd feel like I have much more control over the application if it worked like that.
I just started with fb apps a week ago and the best and most helpfull guide I found was this one:
http://thinkdiff.net/facebook/graph-api-iframe-base-facebook-application-development/
You can also find a great login function in the fbmain.php of that guide.
-michael
I used this code for the authentication.
<?php
$app_id = 'API_ID';
$app_sec = 'SECRETE';
$canvas_page = 'CANVAS_URL';
$scope = '&scope=user_photos,email,publish_stream';
$auth_url = "http://www.facebook.com/dialog/oauth?client_id="
. $app_id . "&redirect_uri=" . urlencode($canvas_page).$scope;
$signed_request = $_REQUEST["signed_request"];
list($encoded_sig, $payload) = explode('.', $signed_request, 2);
$data = json_decode(base64_decode(strtr($payload, '-_', '+/')), true);
if (empty($data["user_id"])) {
echo("<fb:redirect url='".$auth_url."' />");
}
$access_token = $data['oauth_token'];
$user_id = $data["user_id"];
$user = json_decode(file_get_contents(
'https://graph.facebook.com/me?access_token=' .
$access_token));
function get_facebook_cookie($app_id, $application_secret) {
$args = array();
parse_str(trim($_COOKIE['fbs_' . $app_id], '\\"'), $args);
ksort($args);
$payload = '';
foreach ($args as $key => $value) {
if ($key != 'sig') {
$payload .= $key . '=' . $value;
}
}
if (md5($payload . $application_secret) != $args['sig']) {
return null;
}
return $args;
}
$cookie = get_facebook_cookie($app_id, $app_sec);
//facebook application
//set facebook application id, secret key and api key here
$fbconfig['appid' ] = "APP_ID";
$fbconfig['api' ] = "API";
$fbconfig['secret'] = "SECRETE";
//set application urls here
$fbconfig['baseUrl'] = "BASE_URL";
$fbconfig['appBaseUrl'] = "APP_BASE_URL";
$uid= null; //facebook user id
try{
require_once "facebook.php";
}
catch(Exception $o){
echo '<pre>';
print_r($o);
echo '</pre>';
}
// Create our Application instance.
$facebook = new Facebook(array(
'appId' => $fbconfig['appid'],
'secret' => $fbconfig['secret'],
'cookie' => true,
));
?>
To publish to the wall
require_once 'authentication.php';
require_once "facebook.php";
$attachment = array('message' => 'some message',
'name' => 'name',
'caption' => "caption",
'link' => 'http://example.com/',
'description' => 'description',
'actions' => array(array('name' => 'name', 'link' => 'http://example.com/')) );
$result = $facebook->api('/me/feed?access_token='.$access_token, 'post', $attachment);
精彩评论