Start learning with our library of video tutorials taught by experts. Get started
Viewers: in countries Watching now:
Learn the basics of building complex, data-driven applications with the Facebook PHP SDK and MySQL. Author Ray Villalobos first introduces the fundamentals, such as checking to see if a user is logged into an application, accessing Facebook user data through the Open Graph API, and making complex API calls with the Graph API Explorer. The course then dives into building an application with API paths and Facebook Query Language (FQL) calls. Along the way, you'll discover how to post to a user's wall, upload photos, integrate with webpages, and more.
Once users are logged in and authenticated into your application, you can start accessing Facebook's User Data, called the Open Graph, because our application will be changing often, I am going to add a way for users to log out of our application. You don't always have to do this, but it might be useful during development. So let's take a look. So in the last video we saw that there is a method of the Facebook object called getLoginUrl, so naturally you're probably thinking there's an equivalent getLogoutUrl, and you'd be right. Let's take a look at the documentation for that method, you can see that it's pretty much just the same as the getLoginUrl method, but it only takes in one parameter an optional URL to redirect the user after they logged out. So let's try adding that into our project.
Make sure we check for the user haven't been logged in, and so what I'll do is I'll create a logoutUrl variable here, and point it to the Facebook object and use the getLogoutUrl method. Then I'll just print out the link to logout. So let's go ahead and save this, and we'll switch back to our application, we'll refresh, and if you have been playing around with this application, you'll probably see the User ID showing up for your user with the logout link.
If not, just go ahead and log in and then you should see something like this. So I'm going to click on the logout link, and we'll see what happens. Nothing looked like it happened, but if you refresh the page, you'll see that you actually get locked out of Facebook, which is sort of what you'd expect to happen. But if you log in again, you'll be logged back into your app, and that is sort of okay. But what I really want to happen is to be able to logout of the application without logging out of Facebook so that I can re-authenticate the user with maybe different permissions.
So let's see how we would do that. I've actually created a separate link called the logout.php, so let me open that up and show you what that code looks like. So if you take a look at the very top of this code, it's almost exactly as the top of our index.php code right here. It loads up the facebook.php library and then creates a Facebook object. After that we're clearing a cookie by setting the timer to -100 ms at our website URL then we destroy this session, that gets rid of the temporary session that Facebook will create when you log in to an application, and then we set the location back to the original document, which is this page here.
So really all we have to do now is just modify this logOutUrl link so it just points out directly to that document. So I'm not going to use this logOutUrl variable, I'll just get rid of it. So now I'm going to Save and switch back to my application and then I'll refresh and then I'll hit the logout button. So you could see that I'm still logged into Facebook but I'm not logged into my application. So if I want to log back into my application, I can hit login, and now I can see the User ID just fine and logout when I want to.
Now that's going to be useful if you want to change the scope of what you have access to. So if you come right here, and you add something like scope and then we type in some permissions that we want here, say, for example, email, and I'm going to save this document and come back here and then I'm going to refresh this page, logout and log back in. And you'll see that it's asking me for additional permissions for this application. I've just logged out of Facebook, and I logged back in, I might not see this, so I'm going to hit Allow, and now my application is going to have the right permissions for accessing not only the basic info, but also email, and I didn't have to log out and log back into Facebook in order to do that.
So logging out users should be pretty straightforward, but you might run into some problems if you just use the getLogoutUrl method. For application, it's just easier to create a separate logout file. Remember that in production, we're not always going to need to give users the ability to logout. So you might want to take the ability to log out, out of your application before you launch it.
There are currently no FAQs about Building Facebook Applications with PHP and MySQL.
Access exercise files from a button right under the course name.
Search within course videos and transcripts, and jump right to the results.
Remove icons showing you already watched videos if you want to start over.
Make the video wide, narrow, full-screen, or pop the player out of the page into its own window.
Click on text in the transcript to jump to that spot in the video. As the video plays, the relevant spot in the transcript will be highlighted.