Start learning with our library of video tutorials taught by experts. Get started
Viewed by members. in countries. members currently watching.
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.
Integrating FQL queries into your application is pretty similar to feeding paths to the API method. Let's take a look at some of the differences in a very simple example. So I have a document that looks pretty much like the examples we've been using, it requests a Facebook object with your appID and secret, than checks to see if the user is logged in and creates a logout and login URL. Now, if you remember from the chapter on accessing Open Graph data, this page in the Facebook PHP documentation has instructions for creating paths.
If you scroll all the way to the bottom and then come up a little bit, you can see that we pass a path to the Facebook API object and optionally a method. This is how you make FQL calls, you simply add a method variable with the value fql.query to the array we send to the API method, plus the query that you want. So let's take a look at how this is going to work on a simple example. I'm going to create a call to the Graph API called $moviefriends_graph. This is going to be fed an array with a couple of variables.
One is going to be the method fql.query and the other one is going to be our query, and I'm going to want double quotes instead of single quotes here, because some of thing I'm going to pass will include single quotes. So let me delete this comma and just delete that right there, and now we're ready to insert the query that we want. I've already prepared this query on the Graph API Explorer. This query returns the name, user ID, the movies, and a picture from our friends.
You may notice that the movies information that we get doesn't look like what we got when we did a path to query movies, it's just a list of the movies, not the user ID and the picture, and all the information we had. And that's one of the issues with FQL, you don't always get the same data, and you more than likely don't get the richness of the information coming back as you did with the Graph API. So let me go ahead and copy this query, and I'm going to switch back and paste that query right here. So I was using double quotes because this query includes a check to see if the movies field is empty, and in there I use single quotes, so this just makes it easier since the single quotes of my query right here will not get confused with these single quotes.
So now that we got this, let's just go ahead and print out the path on the browser so we can see what it looks like. Let me go Save this, and I'll switch back over here, and I'll go to my application, and I'm going to make sure I type in the name of my page, which is fqlquery.php at the end of the normal path to my application. So let me Refresh that page, and now we can see the data that we get back. Now, notice that there's a slight difference between what we're seeing here and what you see on the Graph API Explorer.
When you query with the Graph API Explorer, you get an item called data. When we get the results from doing the PHP SQL query, we do not get an item called data. So you have to remember to not include that, like you did when we query it with paths. Now that we know that, we can print out just a list of images. So we'll do a foreach statement, and we'll pass it the $moviefriends_graph. Now, in previous movies we included data as the object that we passed to this foreach statement, but we don't need to do that here because the FQL query doesn't return that, and all we have to do is just output the image.
So let me go ahead and Save that, and I'll go back into my application and Refresh this page, and we just see a little grid of images right here. Let me go back and get rid of the printout of our object. So I'll Save this, come back here, Refresh this page, and now you see the grid of images. Now, the cool thing about this list is that it's only the list of friends that have recommended movies. So working with FQL is pretty similar to using paths with the Graph API. I'm not sure if you can tell, but you might notice that FQL calls tend to be a little bit slower than regular path calls.
However, it was very easy for us to get only the list of users with movies, which was a problem with our previous application. It does let you perform some queries that you couldn't otherwise, so sometimes you have to make tradeoffs between speed and functionality when choosing whether to use paths or FQL.
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.