Game Development with Unity3d and Facebook (part 7.1): Using official Facebook PHP SDK

wp-content/uploads/2013/02/Unity3d-and-Facebook-300x212.jpg

This article is a walk through getting Facebook PHP SDK up and running with Unity3d game and integrating example with previous tutorial to improve your Facebook SDKs knowledge.

Getting PHP SDK, configuration and Facebook login test

You should get latest code from Github. It is available at https://github.com/facebook/facebook-php-sdk. You can get the zip file or navigate to your project folder and execute git bash command git clone https://github.com/facebook/facebook-php-sdk to get the latest code. To run example code you need to setup correct application ID and SECRET. Then navigate to you your local URL and test it. But lets create own script building on the top of code samples  from SDK docs from SDK project page on Github. Create a file fb-skd-test.php in main directory pasting into it below code. Make sure that require path point to the right file and that you changed appId and secret when you should.

It should be self explanatory especially with comments. Take it for a spin. This script allows user to log in and out with Facebook account.

Making it all work together with previous example (JS SDK)

That part is only for those who followed Authorising user with Facebook JS SKD  (client-side) and Integration JavaScript and Unity3d code. In first mentioned tutorial we asked you to create fb-sdk-test.html file. Now we will integrate it with our PHP script using  file_get_contents() method. Edit fb-sdk-test.php file replacing block of code restricted with comments “render html content” with code below

And then navigate to your fb-sdk-test.html file and find line:

and replace it with:

If you would test the application now you should see something like this if you haven’t logged in yet (log out if you already did). unity-facebook-login-screen-2 Log in with “(PHP)” works as expected. But if you click “Log in with Facebook (JS)” button you will be redirected to authentication screen and after you accept Facebook will redirect you back to the application showing this unity-facebook-login-screen-2-error And yes, you right if you think “Log in with Facebook (PHP)” button shouldn’t be there. Just refresh the page and everything will look how it should. Problem occurs because cookie is being created  by JavaScript after PHP already used information stored in previous session. Normally you won’t need two buttons next to each other so that problem wouldn’t appear. You can force whole page to reload but I say: “hey! don’t bother”. This problem shouldn’t occur in your life environment as you will pick one solution over the other, right? Why would you need two “log in” buttons allowing user to authenticate himself with two different methods? Aim of this tutorial was to show you fairly practical example of what you could do with PHP SDK and compare it to JS SDK. Instead of drilling the problem lets just dive in deeper into Graph API and Facebook PHP SDK.

Accessing more information about user with Facebook PHP SDK

Getting user friend list and access_token

By default user who is logging into the application with Facebook account grants it standard permission to access basic information about and his or her friend list. To get user friend list you can add additional code to fb-sdk-test.php script which would look something like below.

You might need access token to perform more complex requests so here is how you can get it.

Requesting more information about user

To get more information about user, email for example you need to ask user to give you that extra permission. Similarly  if you want to be able to post something to user blog you need to get permission for that too. You should take your time and reed more about permissions. There are two ways you can go about it. You could either ask for all permissions while logging user with Facebook account or alternatively you can request additional permission only when it’s needed. Which in fact is better practice. From user point of view it is simply less intrusive. Imagine stranger is asking for your email without telling you what he is going to do with it. It would obviously comfort you more if you could read some kind of Privacy Policy or Terms and Conditions on that person website. Here is how you can get more permissions when user is logging with his Facebook account. Navigate to your fb-sdk-test.php file and find  $loginUrl = $facebook->getLoginUrl()  and then replace it with below snippet.

To test it remove your application from the list of application you use in “Apps Settings” section, navigate to your test URL and hit refresh. You will see that application will ask user to grant additional permissions. In case of above example that will be access to user’s email and ability to publish to user’s wall. Find more information about permissions on Facebook documention pages.

Posting to user wall using Facebook Graph SDK

This section explains how to publish example message to Facebook news feed of authenticated user. 

Navigate to your fb-sdk-test.php file, find line $user = $facebook->getUser();  and paste below code after that method to publish message to user wall when he logs into your game. 

To make it work you also need to make sure that this message is being displayed only first time user logs into the game with Facebook account or alternatively when user has revoked access to that game from his profile pages and then changed his mind and comes back to play the game again. This is why we checked if link has “login” parameter in 2nd line of above code. As we want it to be present only for the first time lets change redirection url for authentication process.

Lets have a look at the code we added in section “requesting more information about the user” and replace it with code below.

To test it revoke access to the application from your Facebook settings and navigate to your local address. You should log in and grant application access again. Now after you log in to the game first time it should post to your Facebook wall something like shown on this image.

facebook-wall-first-post

Not bad right? Even though it is good place to start you should know that Facebook Graph API actually gives you much much more than just ability of publishing to user wall.  We are not going to go into details of what you can do with API as this is quite broad topic. Instead you should check out Facebook Graph API to find out more what can be done.

 

Series Navigation<< Game Development with Unity3d and Facebook (part 7): Unity3d and PHP in action (server-side)
This entry is part 14 of 14 in the series Game Development with Unity3d and Facebook

Freelance developer, IT enthusiast, blogger with entrepreneurial spirit and passion for making games

Tagged with:
, , , , , ,
Posted in
Technical
5 comments on “Game Development with Unity3d and Facebook (part 7.1): Using official Facebook PHP SDK
  1. Hv says:

    I Want to use the Score API to post a score on FB.
    How do you send a variable(score) from unity to php script?
    I made a function on the php script that posts the score on fb, how do I call this function with the appropriate variable from unity?

    • admin says:

      Have a look at next post Integration JavaScript and Unity3d code. You should have a php script that handles storing scores, eg. in database and then JS method that will post to your php script and finally from Unity do something like that:

      Just run through next article. It is explained there. Hope it helps.

  2. Hv says:

    Thanks I know this method, but I want to use the Facebook Score API. So there is no need for databases. You just send the score to FB and then it automatically creates highscore stories on your timeline.

    I was thinking of sending to my php script by using WWW in unity and passing a “www.example.com?score=100″ and then grabing from the php script $_Get['score'].

    It worked for a moment in my localhost, but i tried in my server and doesn’t work anymore.

    I want to use the php-sdk instead of JS because I need to use the app secret and get access token

  3. Beautiful work on these tutorials! Keep up the great work!

    Will there be more?

1 Pings/Trackbacks for "Game Development with Unity3d and Facebook (part 7.1): Using official Facebook PHP SDK"

Leave a Reply

Categories