PHP Facebook Graph API “Hello World” application
While I am preparing a new tutorial about Facebook applications (and a php FB game, maybe HTML5 based), I notice there isn’t any “Hello World” application about Facebook applications using the new Graph API with just some PHP lines.
If you don’t know what is the Graph API and don’t want to get an headache with strange words such as “access token”, ever mind… as said I’ll explain it in a few days. But if you are already messing with Graph API and Facebook and are looking for a really simple and working code, this is the post for you.
Try the application: Hello World.
… and get the source code:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
<?php include_once "facebook.php"; $facebook = new Facebook(array('appId'=>'150635898302665','secret'=>'12345678901234567890123456789012','cookie'=>true,'domain'=>'gamemummy.com')); $session = $facebook->getSession(); echo "<strong>Current session content</strong>:"; var_dump($session); try{ $me = $facebook ->api('/me'); echo "<strong>Graph data about me</strong>:"; var_dump($me); }catch(FacebookApiException $e){ echo "<strong>Facebook exception</strong>: ".$e; $url = $facebook->getLoginUrl(array('canvas'=>1,'fbconnect'=>0)); echo "<h3><a href=\"javascript:void(0)\" onclick=\"top.location.href = '".$url."'\">Get access token</a></h3>"; } ?> |
… and don’t try to steal my secret key, it’s not real…





(13 votes, average: 4.54 out of 5)






This post has 13 comments
Tony Lukasavage
I get an error when I click your HelloWorld link:
Current session content:
NULL
Facebook exception: OAuthException: An active access token must be used to query information about the current user.
Gagandeep Singh
That is how it is implemented. click on get access token and give this app required permissions. Then you will not get this error.
Dmillos
Hi Emanuele, once again, thank you very much for your help, I just had a quick question for you, I tried looking online but I couldn’t really find an answer, so hopefully you can help.
I am currently developing a game to put on facebook and I was wondering what is the best way to communicate with it, so far I found 3 possibilities: using Adobe’s Facebook – Flash API, using facebook.php file or using the Graph API released by facebook some time ago.
My first thought is that the last one is the latest, therefore must be the most useful, althou I am not sure If how to get the data from flash.
Thanks a lot for your help and your awesome Tutorials. you are the best!!
-Daniel
superman
hey mate, thanks for the tutorial. it helped alot, but can telme, how to get, users extended permissions on Graph Api, plus, how does the Publish functions works and etc etc?
need a tutorial for that too please :)
Christian Scholz-Flöter
Do you mean Emanuele’s example script just works that way or do you mean Facebook made it so that users will get error messages like the one mentioned by Tony when they are working with the new Graph API?
Because, naturally, I wouldn’t want my users to have to endure seeing a cryptic error message and then having to do another click.
My question — someone here hopefully can answer since the Facebook documentation is wasted webspace — is:
How would the script look like with the get active access token part automated?
Thanks for sharing, everyone!
Mukesh
Hi,
Did u find a way to get active access token part automated??
Thanks
Lana Michelle
Hi there, Sorry if this is not in the right place but I honestly couldn’t figure out where to send you a message. First off thank you very much for the game Glomb (on Facebook)… it has provided me with hours of entertainment trying to beat my nieces score :o) Unfortunately, it does not seem to want to record scores. In all the games I have played it has recorded only one and, of course, it was a terrible score. Please help restore my dignity and fix the score keeping lol Thank you very much! :o)
Udar
did you publish already follow up of this article as advised in begining ? I tried to find it but no succes!
Great job…it is really confusing after switching to new api! Thanks
gus
http://apps.facebook.com/hello_worldz/
Current session content:
NULL
Facebook exception: OAuthException: An active access token must be used to query information about the current user.
Get access token
Jack
facebook api is hard to use for me.
Wyand
I get Fatal error: Call to undefined method Facebook::getSession(). Which library are you using?
Eshriek
I’m using php sdk v. 3.0.0 If you are too try replacing getSession() with getUser()
WebMasterOne
Ciao Emanuele
a little question 4 u, as i can’t find into internet a complete exmple on wath i would like to have.
All the example, infact, are for website pages, where the user can do the Facebook Login.
I’ve a differrent situatuation.
At the moment i’ve a conjob that twice an hour download all the quakes accourred in italy.
And at the moment i’ve a cronjob (php script) that publish this events on my group page using the old api
infact i use:
$url_facebook = “https://api.facebook.com/method/stream.publish?message=”.$messaggio.”&attachment={\”name\”:\”".$titolo.”\”,\”href\”:\”".$action_link.”\”,\”caption\”:\”".$caption.”\”,\”description\”:\”".$testo.”\”,\”media\”:[{\"type\":\"image\",\"src\":\"".$image."\",\"href\":\"".$action_link."\"}]}&action_links=[{\"text\":\"".$link_name."\",\"href\":\"".$action_link."\"}]&target_id=”.$target_uid.”&uid=”.$source_uid.”&”.$site_token.”";
simplexml_load_file(“”.$url_facebook.”");
I’m looking an example for doing the same thing with the graph api.
The post are to be published to 3 group, and my profile (4 all the group, i’m the admin)
I’ve my app with my code and secret, of course…
I hope you will help me
thanks
Mario