Start uncommenting each part of the code you will understand more easily how everything works. I think it will help people who are anxious to "get their hands dirty" right away I will include three(3) files: First we have the header. I guess this is standard code, changed slightly between applications, and necessary on every page to ensure facebook login etc. Code: <?php require_once $_SERVER["DOCUMENT_ROOT"] . '/facebook-php-sdk/src/facebook.php'; $nl = "<br/>"; //newline $appId = 'xxxxxxxxxxxxxxxxxxxxxxx'; //change as necessary $appUrl = "http://apps.facebook.com/app/"; //change as necessary $baseUrl = "http://example.com/app/"; //change as necessary // Create our Application instance. $facebook = new Facebook(array( 'appId' => $appId, 'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //change as necessary 'cookie' => true, )); $session = $facebook->getSession(); $fbme = null; // Session based graph API call. if (!$session) { $login_url = $facebook->getLoginUrl ( array ( "canvas" => true, "fbconnect" => false,
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Start uncommenting each part of the code you will understand more easily how everything works.I think it will help people who are anxious to "get their hands dirty" right away
I will include three(3) files:
First we have the header. I guess this is standard code, changed slightly between applications, and necessary on every page to ensure facebook login etc.
Code:
<?phprequire_once $_SERVER["DOCUMENT_ROOT"] . '/facebook-php-sdk/src/facebook.php';$nl = "<br/>"; //newline$appId = 'xxxxxxxxxxxxxxxxxxxxxxx'; //change as necessary$appUrl = "http://apps.facebook.com/app/"; //change as necessary$baseUrl = "http://example.com/app/"; //change as necessary
// Create our Application instance.$facebook = new Facebook(array( 'appId' => $appId, 'secret' => 'xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx', //change as necessary 'cookie' => true,));
Secondly we have the CSS which includes a lot of facebook styles which a lot of you will find it very usefull if you wish to create facebook look-alike applications.
Code:
/* Facebook styles *//*Facebook uses four major colors in its colorpalette. As you design your UI, you want to besure to use these to better emulate the look ofFacebook. These include white and four shadesof blue. Here are the colors’ hex values: hexvalues: #ffffff (white), #d8dfea (lightest blue),#afbdd4 (lighter blue), #6d84b4 (darker blue),and #3b5998 (darkest blue).*//**********************************************************BASIC FORMATTING************************************************************/body{ background: #fff; font-family: "Lucida Grande", Tahoma, Verdana, Arial, sans-serif; font-size: 11px; margin: 0px; padding: 0px; text-align: left;}
.canvasContent { margin: 10px 20px 5px 10px; } /* here is defined a new class */ /*THIS LINE FORCES US TO LOSE SPACE FROM CANVAS, MAYBE IT IS REDUNDANT AND SHOULD BE REMOVED*/
a.link_btn_style div, a.link_btn_style span /*declare bottom left*/{ cursor:pointer; float:left; line-height:15px; padding: 0px 0px 2px 0px; background-repeat: no-repeat; background-position: bottom left;}a.link_btn_style div div /*declare top right*/{ /*here we have nested in two levels. under the link_btn_style we should have two divs */ padding:0px 2px 0px 0px; /*top, left, right, bottom*/ background-position: top right;}a.link_btn_style div div div /*declare top left*/{ padding:0px; background-position: top left;}a.link_btn_style span.btn_text /*declare bottom right*/{ display:block; /* notice that the bottom right position is block because we want a break line for the next html element after this button */ margin:2px -2px -2px 2px; padding:2px 19px 5px 17px; /*top, left, right, bottom*/ background-position: bottom right; }
* html a.link_btn_style span { position:relative; }
a.reg_btn_style div /* default view */{
background-image: url(http://www.facebook.com/images/welcome/btn_register_signup_active_bg.gif);}a.reg_btn_style span.btn_text{ color:#fff; font-weight:normal;}a.reg_btn_style:link div, a.reg_btn_style:link span, a.reg_btn_style:visited div, a.reg_btn_style:visited span /* visited view */{ background-image: url(http://www.facebook.com//images/welcome/btn_register_signup_bg.gif);}a.reg_btn_style:active div, a.reg_btn_style:active span /* active view (when we press down the click button) */{ background-image: url(http://www.facebook.com//images/welcome/btn_register_signup_active_bg.gif);}
.fb-tabitems li{ display: inline; padding: 2px 0px 3px; /*top, left and right, bottom*/ background: #f1f1f1 url(http://www.facebook.com/images/components/toggle_tab_gloss.gif) top left repeat-x;}
<div> <table class="lists" cellspacing="0" border="0"> <tr> <th> <!-- special table row element which is Table Header. normally on top of tables --> <h4>List 1</h4> <a href="#">See All1</a> </th> <th class="spacer"> </th> <th> <h4>List 2</h4> <a href="#">See All2</a> </th> </tr> <tr> <td class="list"> <div class="list_item clearfix"> Aenean felis purus, ullamcorper sed, laoreet quis, porttitor et, mauris. Donec blandit dictum dui. Cras magna erat, sagittis vitae, ornare quis, dignissim eget, orci. Sed viverra nisl nec erat. Ut vulputate. Sed vitae elit sed nisi condimentum fringilla. Proin dapibus dui ac tellus. </div> </td> <td class="spacer"> </td> <td class="list"> <div class="list_item clearfix"> Nulla in tellus tempor mauris euismod bibendum. Vestibulum metus quam, tincidunt sed, gravida pretium, vulputate a, augue. Maecenas tempus metus a nulla. Nullam sollicitudin, lorem ut ultricies ultricies, est ipsum vulputate nunc, vel feugiat mi sem in erat. </div> </td> </tr> <tr> <td class="see_all"> <div>
<a href="#">See all List 1’s</a> </div> </td> <td class="spacer"> </td> <td class="see_all"> <div> <a href="#">See all List 2’s</a> </div> </td> </tr> </table></div>
<?phptry{ //$uid = $facebook->getUser(); $fbme = $facebook->api("/me?fields=name,picture"); /* // login or logout url will be needed depending on current user state. if($fbme) { $logoutUrl = $facebook->getLogoutUrl(); echo "<a href='". $logoutUrl . "'>click here to logout</a>" .$nl; } else { $loginUrl = $facebook->getLoginUrl(); echo "<a href='". $loginUrl . "'>click here to login</a>" .$nl; } if($fbme) { //https://graph.facebook.com/oauth/authorize?client_id=174209505930964&redirect_uri=http://apps.facebook.com/giorgosapp/&scope=user_likes $movies = $facebook->api('/me/movies'); //http://bit.ly/gODpya <--- here is the solution how to check for permission with FQL echo $fbme["id"].$nl; echo $fbme["name"].$nl; } //$arr = array("one", "two", "three"); //echo $movies["data"][0]["name"]; foreach ($movies["data"] as $mov) { echo $mov["name"] . $nl; } echo "This is my email: ";
if (!$fbme["email"]) //if we do not have the permission then this is zero { $emailPerm = getPermUrl($appUrl,"email"); echo "<a href='' onclick=\"topLink('" . $emailPerm . "')\">click here to get email</a>" .$nl; } else { echo $fbme["email"] .$nl; } //update user's status using graph api if(isset($_POST['tt'])) { $msg = array ( //'access_token' => "YUR ACCESS TOKEN", //'name' => "This is the title of my post", //'link' => "http://blogs.canalplan.org.uk/steve/2010/04/28/hitting-a-moving-target/", //'description' => "this is the body of the post with lots of wiffly woffly text in it, lets see if this all works ok!", //'picture'=>"http://blogs.canalplan.org.uk/steve/files/2009/12/13742_1291940983817_1389037839_836473_2130235_n.jpg", 'message' => $_POST['tt'], 'cb' => '', //to callback function protimoume na einai null, ektos an theloume na kanoume refresh thn selida h na metavoume se kapoia allh ); $facebook->api('/me/feed', 'POST', $msg); } */}catch (FacebookApiException $e) { echo '<pre>'; print_r($e); echo '</pre>'; }
<!--<form name="" action="<?=$baseUrl?>" method="post"> <label for="tt">Status update using Graph API</label><br /> <textarea id="tt" name="tt" cols="50" rows="2">Write your status here and click 'submit'</textarea>
<br /> <input type="submit" value="update status" /></form>--><?phptry //status{ /* $curstatus = $facebook->api('/me/statuses?limit=1'); //check last status if(!$curstatus) { //echo "no feed" .$nl; $feedPerm = getPermUrl($appUrl,"publish_stream"); echo "<a href='' onclick=\"topLink('" . $feedPerm . "')\">click here to grand access to your status</a>" .$nl; } else { echo "my last status is: "; echo $curstatus["data"][0]["message"] .$nl; } */ //get status of all friends of current user $myfriends = $facebook->api("/me/friends"); if($myfriends["data"][0]) { $count=0; foreach($myfriends["data"] as $friend) { /* //GRAPH API //$curStatus = $facebook->api("/$friend[id]/statuses?limit=1"); $curStatus = $facebook->api("statuses?ids=$friend[id]&limit=1"); //$curStatus = $curStatus[$friend["id"]]; echo "$friend[name] says $curStatus[data]" .$nl; print_r($curStatus); echo $nl.$nl; */ //WE DO NOT HAVE ACCESS TO STATUSES OF OTHER USERS DIRECTLY. CHECK IT FOR YOURSELF AND LET ME KNOW IF YOU FIND A WORKAROUND at [email protected] /* //legacy api $desired_details = array('last_name','first_name', 'status'); $param = array( 'method' => 'users.getInfo', 'uids' => $friend["id"],
try //facebook pages{ /* $likesPerm = getPermUrl($appUrl,"user_likes,friends_likes"); echo "<a href='' onclick=\"topLink('" . $likesPerm . "')\">allow application to access your pages and the pages of your friends</a>" .$nl; */ /* echo "facebook pages" .$nl; $myPages = $facebook->api("/$uid1/likes?fields=name"); //friend's user id here */ /* if($myPages["data"][0]) { foreach($myPages["data"] as $page) { echo "The page " . $page["name"] . " has " . $page["fan_count"] . " members" . $nl; } } */ /* $json = array(); $json["name"] = "EasyJet"; //$json["fan_count"] = "42971"; $json["id"] = "11936918897"; $json["created_time"] = "2010-09-14T21:20:11+0000"; //print_r($myPages["data"]); $key = array_search($json,$myPages["data"]); //array search using objects. cool! echo "the key is: " . $key . $nl; //print_r($myPages["data"]); */ }catch (FacebookApiException $e) { echo '<pre>'; print_r($e); echo '</pre>'; }?>
<?php //FQLtry{ /*//name of single user
$query = " SELECT name FROM user WHERE uid=$fbme[id] "; $FQL = array ( "method" => "fql.query", "query" => $query); $data = $facebook->api($FQL); print_r($data); echo $nl; */ /*//all members of a group $query = " SELECT uid FROM group_member WHERE gid=$thraknes "; $FQL = array ( "method" => "fql.query", "query" => $query); $data = $facebook->api($FQL); print_r($data); echo $nl; */ /*//all movies of a user $query = " SELECT name, movies FROM user WHERE uid=$fbme[id] "; $FQL = array ( "method" => "fql.query", "query" => $query); $datas = $facebook->api($FQL); foreach($datas[0] as $data) { echo $data .$nl; } */ /*//info about an event $query = " SELECT eid, venue.city FROM event WHERE eid=$snowball "; $FQL = array ( "method" => "fql.query", "query" => $query); $datas = $facebook->api($FQL); foreach($datas[0] as $data) { if(!is_array($data)) echo $data .$nl; else { $key = array_keys($datas[0],$data); echo $key[0] .$nl; foreach($data as $dt) { echo "...." . $dt .$nl; } }
} print_r($datas); */ /*//using operators in where clause $query = " SELECT eid FROM event_member WHERE uid=$fbme[id] AND eid>=7630890908 "; $FQL = array ( "method" => "fql.query", "query" => $query); $datas = $facebook->api($FQL); if($datas[0]) foreach($datas[0] as $data) { if(!is_array($data)) echo $data .$nl; else { $key = array_keys($datas[0],$data); echo $key[0] .$nl; foreach($data as $dt) { echo "...." . $dt .$nl; } } } */ /*//IN clause $query = " SELECT name, location FROM event WHERE eid IN ( SELECT eid FROM event_member WHERE uid=$fbme[id] ) "; $FQL = array ( "method" => "fql.query", "query" => $query); $datas = $facebook->api($FQL); if($datas[0]) foreach($datas[0] as $data) { if(!is_array($data)) echo $data .$nl; else { $key = array_keys($datas[0],$data); echo $key[0] .$nl; foreach($data as $dt)
{ echo "...." . $dt .$nl; } } } */ //the friend map // $query = "// SELECT uid1, uid2// FROM friend// WHERE uid1 IN (SELECT uid1 FROM friend WHERE uid2=$fbme[id])// AND uid2 IN (SELECT uid1 FROM friend WHERE uid2=$fbme[id])// AND uid1 < uid2// ";
//all of my friends who are females// $query = "// SELECT name FROM user// WHERE sex='female' AND uid IN// (// SELECT uid1 FROM friend// WHERE uid2=$fbme[id]// )// "; //all the pages that i am fan of// $query = "// SELECT name FROM page// WHERE page_id IN// (// SELECT page_id FROM page_fan// WHERE uid=$fbme[id]// )// "; /* //concatenation - all the movies that my friends like $query = " SELECT name, location FROM event WHERE strpos(name, \"KAZ\")>=0 AND eid IN ( SELECT eid FROM event_member WHERE uid=$fbme[id] ) "; $FQL = array ( "method" => "fql.query", "query" => $query); $datas = $facebook->api($FQL); if($datas[0]) foreach($datas as $data)
</div> <!-- end canvas -->Hi,i did just as you said - 3 files1. header.php with the upper code2. fbstyle.css with the middle code3. index.php with the lower code
i changed the api, secret & url. i'm using Iframebut it show me nothing in the appany idea why?what about the <html>&<body> tags, i don't need them?