Graphic API
在 Facebook 中每一個對象(人物、照片、活動及頁面)都有一個唯一的 ID。你可以連結對象的屬性要 https://graph.facebook.com/ID 如 Facebook 平台的官方網頁的 ID 為 19292868552 ,所以你可以獲取對象 https://graph.facebook.com/19292868552 的:
{
"name": "Facebook Developers",
"is_published": true,
"website": "http://developers.facebook.com",
"username": "FacebookDevelopers",
"company_overview": "Facebook Platform enables anyone ...\n\n",
"about": "Build and distribute amazing social apps on Facebook. ...",
"talking_about_count": 16821,
"category": "Product/service",
"id": "19292868552",
"link": "https://www.facebook.com/FacebookDevelopers",
"likes": 222477,
"cover": {
"cover_id": "10151124971508553",
"source": "http://sphotos-f.ak.fbcdn.net/.../...png",
"offset_y": 0
}
}
另外,也可以使用用戶名稱作為 ID ( https://graph.facebook.com/platform ),所有回傳的資料都為 JSON。
在 Facebook 上的所有對象可以用相同的方式連結:
- Users(用戶): https//graph.facebook.com/btaylor
- Pages(粉絲專頁): https://graph.facebook.com/btaylor
- Events(活動) : https://graph.facebook.com/251906384206
- Groups(群組) : https://graph.facebook.com/195466193802264
- Applications(應用程式) : https://graph.facebook.com/2439131959
- Status messages(狀態訊息) : https://graph.facebook.com/367501354973
- Photos(照片) : https://graph.facebook.com/98423808305
- Photo albums(相簿): https://graph.facebook.com/99394368305
- Profile pictures(大頭貼相片) : https://graph.facebook.com/jheng.winrex/picture
- Videos(影片) : https://graph.facebook.com/817129783203
- Notes : https://graph.facebook.com/122788341354
- Checkins : https://graph.facebook.com/414866888308
Facebook 社交網中所有的對象都有彼此連接的關係,把這些關係在我們 API 的連接。您可以檢查對象的 URL 結構 https://graph.facebook.com/ID/CONNECTION_TYPE 之間的連接。支緩使用者和網頁的連接包括:
- Friends : https://graph.facebook.com/me/friends?access_token=...
- News feed : https://graph.facebook.com/me/home?access_token=...
- Profile feed (Wall): https://graph.facebook.com/me/feed?access_token=...
- Likes : https://graph.facebook.com/me/likes?access_token=...
- Movies(影片) : https://graph.facebook.com/me/movies?access_token=...
- Music(音樂) : https://graph.facebook.com/me/music?access_token=...
- Books(書) : https://graph.facebook.com/me/books?access_token=...
- Notes : https://graph.facebook.com/me/notes?access_token=...
- Permissions(權限) : https://graph.facebook.com/me/permissions?access_token=...
- Photo Tags(照片標籤) : https://graph.facebook.com/me/photos?access_token=...
- Photo Albums(相簿) : https://graph.facebook.com/me/albums?access_token=...
- Video Tags(影片標籤) : https://graph.facebook.com/me/videos?access_token=...
- Video Uploads(上傳的影片) : https://graph.facebook.com/me/videos/uploaded?access_token=...
- Events(活動) : https://graph.facebook.com/me/events?access_token=...
- Groups : https://graph.facebook.com/me/groups?access_token=...
- Checkins : https://graph.facebook.com/me/checkins?access_token=...
- Objects with Location(對像的位置) : https://graph.facebook.com/me/locations?access_token=...
選擇回傳資料
在預設情況下,可以對大多數對象的屬性進行查詢並反回。你可以選擇要反回的"fields"查詢參數的 fields 。
例如:URL 回傳 ID, name, picture
https://graph.facebook.com/bgolub?fields=id,name,picture
也可以查詢中使用 "ids" 查詢參數要求多個對象,例如: https://graph.facebook.com?ids=arjun,vernal 會回傳二個相同 id 的資料。
"ids" 查詢參數,也可以接受網址。例如:
https://graph.facebook.com/?ids=http://www.imdb.com/title/tt0117500/
此外, "me"是一個特殊的標識符號,它指的是當前使用者。所以 https://graph.facebook.com/me 可以回傳的當前用戶的個人資料。
圖片
可以呈現尚前的任何對象的個人資料照片。例如。這是你的大頭照片:
- 人物:<img src="https://graph.facebook.com/{用戶名稱}/picture"/>
- 活動:<img src="https://graph.facebook.com/331218348435/picture"/>
- 群組:<img src="https://graph.facebook.com/69048030774/picture"/>
- 頁面:<img src="https://graph.facebook.com/DoloresPark/picture"/>
- 應用程式:<img src="https://graph.facebook.com/2318966938/picture"/>
- 相簿:<img src="https://graph.facebook.com/platform/picture"/>
| 人物 | 活動 | 群組 | 頁面 | 應用程式 | 相簿 |
|---|---|---|---|---|---|
https://graph.facebook.com/me/picture?type=largesquare | small | normal | large
如果需要使用安全傳輸取得圖片,可以設置 return_ssl_resources=1 參數:
https://graph.facebook.com/me/picture?return_ssl_resources=1
分頁
使用查詢時,有幾個有用的參數,可以透過連結過濾資料:
- limit(筆數), offset(指標,從第幾比開始): https://graph.facebook.com/me/likes?limit=3 (limit(筆數):10, offset(指標,從第幾比開始):n)
- until, since (a unix timestamp or any date accepted by strtotime): https://graph.facebook.com/search?until=yesterday&q=orange ( until:yesterday(昨天),q=關鍵字 ,type=搜尋的資料,until 和 since 值是一個時間間格)
日期
所有的日期字串將以ISO-8601格式回傳。你可以指定一個"date_format"查詢參數修改日期格式。可接受的格式字串需PHP日期函數所接受的相同: https://graph.facebook.com/platform/feed?date_format=U 。
Introspection
Graph API 支援 introspection 的對象,從而可以看到所有連接對象,前提是不知道它的類型。
https://graph.facebook.com/331218348435?metadata=1
Real-Time updates
搜尋(Searching)
您可以使用 https://graph.facebook.com/search 搜尋在facebook中的所有公共對象。其格式為:
https://graph.facebook.com/search?q=QUERY&type=OBJECT_TYPE支援搜尋對象的類型:
- All public posts: https://graph.facebook.com/search?q=watermelon&type=post
- People: https://graph.facebook.com/search?q=mark&type=user
- Pages: https://graph.facebook.com/search?q=platform&type=page (requires access_token)
- Events: https://graph.facebook.com/search?q=conference&type=event
- Applications: https://graph.facebook.com/search?q=endomondo&type=application
- Groups: https://graph.facebook.com/search?q=programming&type=group
- Places: https://graph.facebook.com/search?q=coffee&type=place. You can narrow your search to a specific location and distance by adding the center parameter (with latitude and longitude) and an optional
- Checkins: https://graph.facebook.com/search?type=checkin (This request returns you or your friend's latest checkins, or checkins where you or your friends have been tagged; currently, it does not accept a q= parameter.)
- Event Names: https://graph.facebook.com/search?fields=name&q=conference&type=event
- News Feed: https://graph.facebook.com/me/home?q=facebook
發佈
你可以向 Facebook graph 發出 HTTP POST 請求適當的URL(必須使用 access token 參數)。例如:你可以發布一個新的訊息 https://graph.facebook.com/arjun/feed :
curl -F 'access_token=...' -F 'message=Hello, Arjun. https://graph.facebook.com/arjun/feedpost 相對應值: https://developers.facebook.com/docs/reference/api/post/
你也可以發表留言或類似的對象,如 /comments or /likes connection by posting to https://graph.facebook.com/OBJECT_ID/comments and https://graph.facebook.com/OBJECT_ID/likes, respectively :
curl -F 'access_token=...' https://graph.facebook.com/313449204401/likes大多數寫入操作需要更多的權限。
支援以下類型的對象:
| 方法 | 描述 | 參數 |
|---|---|---|
| /PROFILE_ID/feed | Publish a new post on the given profile's feed/wall | message, picture, link, name, caption, description, source, place, tags |
| /OBJECT_ID/comments | Comment on the given object (if it has a /comments connection) | message |
| /OBJECT_ID/likes | Like the given object (if it has a /likes connection | none |
| /PROFILE_ID/notes | Publish a note on the given profile | message, subject |
| /PROFILE_ID/links | Publish a link on the given profile | link, message, picture, name, caption, description |
| /PROFILE_ID/events | 新增活動 | name, start_time, end_time |
| /EVENT_ID/attending | 回覆"出席"參加活動 | none |
| /EVENT_ID/maybe | 回覆"也許"參加活動 | none |
| /EVENT_ID/declined | 回覆"拒絕"參加活動 | none |
| /PROFILE_ID/albums | 新增相簿 | name, message |
| /ALBUM_ID/photos | 上傳照片到指定相簿 | message, source, place(multipart/form-data) |
| PROFILE_ID/checkins | Create a checkin at a location represented by a Page | coordinates, place, message, tags |
刪除
你可以在 graph 中刪除對象,即通過 HTTP DELETE 請求,
DELETE https://graph.facebook.com/ID?access_token = ... HTTP/1.1為了支援用戶端不支援所有HTTP 方法(如JavaScript客戶端),你也可以發出一個 POST 請求額外加入 method=delete :
您可以刪除這條評論發出POST請求,https://graph.facebook.com/COMMENT_ID?method=delete。
您可以刪除一個像發出DELETE請求,/的OBJECT_ID /喜歡(因為喜歡沒有ID)。
結論:
簡單的說就是使用 URL 下參數取得資料。
參考資料: