官术网_书友最值得收藏!

Time for action - finding connections in a browser

Facebook treats wall posts, photos, videos, and even statuses as separate objects within the Graph API, rather than jamming them all into a single Page object. For instance, here's an object representing a single Post by Packt Publishing:

{
"id": "204603129458_127056137323572",
"from": {
"name": "Packt Publishing",
"category": "Products_other",
"id": "204603129458"
},
"message": "The Amazon SimpleDB Developer Guide has been published! Get your copy now! http://bit.ly/blFQUG",
"picture": "http://external.ak.fbcdn.net/ safe_image.php?d=c4a7887cb52dd8f93e439aaec13c034b&w=130&h=130&url =https%3A%2F%2Fwww.packtpub.com%2Fsites%2Fdefault%2Ffiles%2Fimage cache%2Fproductview%2F7344EN_MockupCover%2520Template.jpg",
"link": "http://bit.ly/blFQUG",
"name": "Amazon SimpleDB Developer Guide | Packt Publishing Technical & IT Book Store",
"caption": "bit.ly",
"description": "Gain in-depth understanding of Amazon SimpleDB with PHP, Java, and Python examples, and run optimized database-backed applications on Amazon\\'s Web Services cloud",
"icon": "http://static.ak.fbcdn.net/rsrc.php/zB010/hash/ 9yvl71tw.gif",
"type": "link",
"created_time": "2010-06-04T12:39:44+0000",
"updated_time": "2010-06-04T12:39:44+0000",
"likes": 1
}

That object has expired now, and is no longer available through the Graph API, but as you could have guessed, it was available at https://graph.facebook.com/204603129458_127056137323572. It's in the same format as the Page object albeit with a few different fields so our Graph Object Renderer could render it just fine.

Of course, this is useless unless we know the ID of each of the Posts associated with Packt Publishing, and there's no indication of where we might find them. Or is there?

I said earlier that the Graph API was designed to be self-documenting. We can request extra, "meta" information about any Graph Object by adding a metadata=1 flag to the end of any Graph URL. Take a look at: https://graph.facebook.com/PacktPub?metadata=1 in your browser. A new property, type, appears in the JSON:

"type": "page"

That's useful; as I said, Posts and Pages (and in fact all Graph Objects) take the same format, so this gives us a way of telling them apart.

More immediately interesting, though, is the new metadata object. This contains one object, connections, and one array, fields. Let's look at fields first:

"fields": [
{
"name": "id",
"description": "The page's ID"
},
{
"name": "name",
"description": "The page's name"
},
{
"name": "picture",
"description": "The pages profile picture"
},
{
"name": "category",
"description": "The page's category"
},
{
"name": "fan_count",
"description": "\\* The number of fans the page has"
}
]

This is a list explaining what each of the fields in the main body of the Graph Object represents. At time of writing, this is still a fairly new feature, so it's possible that the list will be more complete by the time you load it.

The connections object is as follows:

"connections": {
"feed": "https://graph.facebook.com/packtpub/feed",
"posts": "https://graph.facebook.com/packtpub/posts",
"tagged": "https://graph.facebook.com/packtpub/tagged",
"statuses": "https://graph.facebook.com/packtpub/statuses",
"links": "https://graph.facebook.com/packtpub/links",
"notes": "https://graph.facebook.com/packtpub/notes",
"photos": "https://graph.facebook.com/packtpub/photos",
"albums": "https://graph.facebook.com/packtpub/albums",
"events": "https://graph.facebook.com/packtpub/events",
"videos": "https://graph.facebook.com/packtpub/videos"
}

Browse to one of the URLs from the previous list: http://graph.facebook.com/packtpub/posts. It returns a JSON containing an array called data and an object called paging. The data array contains several Post objects; we'll look at paging later in the book.

What just happened?

The metadata=1 parameter tells the Graph API to display all of the metadata about the current object, which, in this case, includes the type of object it is, an array of descriptions of the object's properties, and all of the URLs that contain lists of objects connected to this Page.

This layout is where the Graph API gets its name. In everyday usage, "graph" means the type of chart shown in the next diagram:

What just happened?

But in mathematics, "graph" refers to any set of nodes connected by edges, like the example in the next diagram:

What just happened?

The Graph API represents Facebook's data as shown in the next diagram :

What just happened?

In the previous diagram, each object is a node, and the lines represent different types of connection.

Fetching http://graph.facebook.com/packtpub/posts gets you all the nodes joined to PacktPub by a "post" connection that is, all Post objects that have been posted on Packt's wall:

What just happened?

Have a go hero - exploring connections

Now that you know about the metadata parameter, explore the different types of connections in your browser, and see what new kinds of objects you can find.

Rendering Lists

What happens if you try to load https://graph.facebook.com/packtpub/posts using the same code we used to load the Packt Publishing Page object?

We get this in the output panel:

Graph Object was null!

Not a success. The way the Graph API structures the JSON here is totally different to how it structures the JSON for a Page, Post, or any other Graph Object. The same is true of the JSON for the other connection URLs. We call this structure a Graph List.

主站蜘蛛池模板: 扬州市| 友谊县| 顺平县| 遵义县| 天全县| 团风县| 安庆市| 宝鸡市| 来凤县| 都安| 蒙自县| 弋阳县| 鹿泉市| 龙里县| 东方市| 家居| 惠来县| 南宫市| 青州市| 楚雄市| 朝阳区| 原阳县| 晴隆县| 巴里| 葵青区| 阳新县| 柳州市| 韶关市| 增城市| 西乡县| 恩平市| 连平县| 南雄市| 林州市| 平遥县| 辛集市| 页游| 万全县| 龙陵县| 荣成市| 磐石市|