有時希望可以讓使用者在分享文字或連結時,能自動帶出一些效果,這時候我們需要的就是 Publisher 服務。例如當輸入 YouTuBe 影片連結時,系統會自動幫我們嵌入 YouTuBe 的顯示頁面,而不再只是簡單的 link 樣貌,此時原理即是將使用者輸入的資料,進行分析再轉換成新的呈現方式。
然而,目前 Facebook 平台開放的狀態還沒讓開發者如此便利,但有個間接使用的方式,那就是發表時以 Attach(附件) 來呈現。關於 Attach 的設定方式如下:
Setup
- My Applications > Edit Settings > Profiles > Profile Publisher
- Publish
- Text
- Callback URL
- Self-Publish
- Text
- Callback URL
- Publish
其中可分成兩類,一種是在別人的塗鴉牆留言時,另一種則是在自己的塗鴉牆留話。分別都需要填寫兩項資訊,其中 Text 代表在 Attach(附件) 顯示的按鈕名稱,如:加增表情符號。 Callback URL 則是當使用者按下按鈕時,系統會去這個位置要資料來呈現。實作上要留意,那個 Callback URL 回傳的資料要以 JSON 格式回來。
至於 Facebook 去 Callback URL 進行撈資料時,帶有以下參數資訊:
- fb_sig_api_key: Your application's API key.
- fb_sig: A signature using your application's secret key to encrypt the request.
- fb_sig_in_canvas: This parameter is always false or 0 because the Publisher is not a canvas page.
- fb_sig_request_method: This is always a POST method.
- fb_sig_locale: This is the user's locale.
- fb_sig_time: The time that the request was made.
- fb_sig_added: Indicates whether the user has authorized (agreed to your Terms of Service) the application.
- fb_sig_profile_user: Indicates the user whose profile is being published to.
- fb_sig_user: The ID of the user interacting with the Publisher.
- method: Indicates if the Publisher is getting the application's Publisher interface or getting the Feed story.
- 等等‥
只是,依舊沒有使用者當下輸入的資訊,這讓我有一點小失望,這也可能基於一些隱私或安全性的考量吧?剩下的我只好有空再來摸索啦!但是,目前開放的平台特色仍可以做出不錯的效果喔!
簡單的 Callback 程式碼,其中 base.php 則是處理 Facebook API 的初始化,而 publisher_getFeedStory 部份則是直接用上頭 Facebook - Publisher 提到的範例。
<?php
require( 'base.php' );
$data = array();
$data['content'] = array();
$out = '';
foreach( $_REQUEST as $k => $v )
$out .= "[$k] => [$v]<br>";
if( !isset( $_REQUEST['method'] ) || $_REQUEST['method'] == 'publisher_getInterface' )
{
$data['content']['fbml'] = json_encode( $out );
$data['content']['oublishEnabled'] = true;
$data['method'] = 'publisher_getInterface';
}
else
{
$data['content']['attachment'] = array();
$data['content']['attachment']['name'] = 'ninja cat';
$data['content']['attachment']['href'] = 'http://www.youtube.com/watch?v=muLIPWjks_M';
$data['content']['attachment']['caption'] = '{*actor*} uploaded a video to www.youtube.com';
$data['content']['attachment']['description'] = 'a sneaky cat';
$data['content']['attachment']['properties'] = array();
$data['content']['attachment']['properties']['category'] = array();
$data['content']['attachment']['properties']['category']['text'] = 'pets';
$data['content']['attachment']['properties']['category']['href'] = 'http://www.youtube.com/browse?s=mp&t=t&c=15';
$data['content']['attachment']['properties']['ratings'] = '5 stars';
$data['content']['attachment']['media'] = array();
$data['content']['attachment']['media'][0]['type'] = 'video';
$data['content']['attachment']['media'][0]['video_src'] = 'http://www.youtube.com/v/fzzjgBAaWZw&hl=en&fs=1';
$data['content']['attachment']['media'][0]['preview_img'] = 'http://i2.ytimg.com/i/uZp-cf0yOTqQEOuKGGnocg/1.jpg';
$data['content']['attachment']['media'][0]['video_link'] = 'http://www.youtube.com/watch?v=muLIPWjks_M';
$data['content']['attachment']['media'][0]['video_title'] = 'ninja cat';
$data['method'] = 'publisher_getFeedStory';
}
echo json_encode( $data );
?>
其中,上述發佈用的 template 允許四種 type ,分別為 image , flash, mp3, vedio ,細節請參考 Facebook - Attachment (Streams)
參考資料
到哪設定 attach ?
回覆刪除我找不到 My Applications ..
版主回覆:(05/04/2010 02:14:38 AM)
在"開發人員"那邊就可進去囉,意思是去設定自己的應用軟體。
http://www.facebook.com/developers/
瞭解.. 3q ..
回覆刪除請問一下,若是上傳照片到FB上,需要取得他的資料夾嗎???
回覆刪除因為發現製作一個 <input type="file"..... 來尋找自己電腦上的照片,
上傳之後會找不到檔案說,請問這有什麼方法可以解決呢?
我是使用它的API photos_upload 。
版主回覆:(11/04/2009 10:41:09 AM)
很抱歉,你說的 API 我沒用過,最近很忙無法再幫忙測試你提到的 API 囉,你可以再請教別人看看囉。