Easy-to-follow video tutorials help you learn software, creative, and business skills.Become a member
We've been learning a lot about paths and how to use the Graph API Explorer. Let's take a look at how we can integrate a simple path in an application and how to work with photos. We'll start off with the document we made in the movie understanding Graph API paths. I'll start by changing the title, and I'm going to add a line to link this file to a CSS document I'll create later. I'm also going to delete the line printing the user's ID. Now let's delete the part where we print out the user's information, and we'll start our output by creating a new list group.
We've added an lgrid for left grid and a group class so we can style these later. Now we need to iterate through each piece of data. Let's go over to the Graph API Explorer, and I'm going to modify this path by adding /friends to the end of the default path. This is going to give me this user's current list of friends. I can also use /me as the path, which is what I do on the application. This path is returning the name of the friend, as well as an ID. Let's go through each friend by using the foreach statement.
Now I'm passing along this data field right here, because if we look at the Open Graph API Explorer, this object returns an array of data. Now let's create the code for putting the user's data inside a list. And now we're ready to output an image. You don't have to do this from the Open Graph. Default images of objects are public, so the easiest way to get an image is to create an img tag calling the Graph API, then adding the ID, plus the /picture path afterwards.
So I need to make sure I add the friends path into the API call. Then I'll switch over to my app and refresh, and I just see the list of photos come up. You may notice that the app is no longer printing the user's name right next to the word Hello. When you change the paths so that it points to /me/friends, you lose access to the first name of the user, since now the data from Facebook shows only your friends info. It's not a big deal, since what we are most interested in is experimenting with friends' data.
If your server is set to show notices, you may want to delete line 22 printing Hello to the screen. Now these photos aren't clickable to the user's profile, so let's go ahead and add that. To get the URL of the current object, you simply type in facebook.com/the ID number of the object. You want to make sure you use _top as the target so that the browser doesn't try to load the page into the canvas's iFrame. So we'll save this and go back into our application, we'll refresh the browser, and now these icons should be clickable to each user's account.
It'd also be nice to have the user's name under each photo. So let's go ahead and add that. So I'm going to go ahead and save this, and I'll switch over to the browser, refresh, and now the name is underneath each user. This could look a lot better so I'm going to go ahead and add some CSS. I'm going to create a new CSS file, I am using transmit as my FTP Application. So I'm going to right-click, select New File make sure you same name that I used earlier when I created my link to my style sheet, and I'll right-click and select Open With > Espresso, which is my editor.
It opens open in a new window, but I can just click and drag it right here to enter it as a new tab. So this is a list, I need to start by addressing that when you float list item tags, the Container loses track of the height of its content. This is known as a Clear Fix, but I like to use the word Group, instead of Clear Fix. I've created a gist with the code you need, so you can just copy and paste from right here. And I'll paste that into my CSS file. If you want to learn more about working with Floats and the Clear Fix technique, check out CSS page layouts in the online training library.
So now let's create a style for the body tag making sure that we establish some defaults for our document. And now we'll add three simple styles to make our grid of photos look better. So let's go ahead and save this, and we'll go back to our project and refresh. And you can see our grid looks a lot better. So applying paths in your projects takes a little bit more set up, but it's pretty easy, and you work with the foreach command to iterate through each of the different elements returned by the Open Graph API.
Get unlimited access to all courses for just $25/month.Become a member
82 Video lessons · 101575 Viewers
61 Video lessons · 88344 Viewers
71 Video lessons · 72193 Viewers
56 Video lessons · 103942 Viewers
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.
Your file was successfully uploaded.