woff 發表於 2012-4-2 16:26:21

Facebook API Publisher 使用 Attach 附件服務增加發佈塗鴉牆的效果

http://farm3.static.flickr.com/2626/4073369077_e646be5fbc_o.png有時希望可以讓使用者在分享文字或連結時,能自動帶出一些效果,這時候我們需要的就是 Publisher 服務。例如當輸入 YouTuBe 影片連結時,系統會自動幫我們嵌入 YouTuBe 的顯示頁面,而不再只是簡單的 link 樣貌,此時原理即是將使用者輸入的資料,進行分析再轉換成新的呈現方式。
然而,目前 Facebook 平台開放的狀態還沒讓開發者如此便利,但有個間接使用的方式,那就是發表時以 Attach(附件) 來呈現。關於 Attach 的設定方式如下:
Setup

[*]My Applications > Edit Settings > Profiles > Profile Publisher

[*]Publish

[*]Text
[*]Callback URL
[*]Self-Publish

[*]Text
[*]Callback URL
其中可分成兩類,一種是在別人的塗鴉牆留言時,另一種則是在自己的塗鴉牆留話。分別都需要填寫兩項資訊,其中 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.
[*]等等‥
只是,依舊沒有使用者當下輸入的資訊,這讓我有一點小失望,這也可能基於一些隱私或安全性的考量吧?剩下的我只好有空再來摸索啦!但是,目前開放的平台特色仍可以做出不錯的效果喔!
發佈塗鴉牆的效果
http://farm3.static.flickr.com/2456/4073431291_8d7a402215_o.png

點選播放的畫面
http://farm4.static.flickr.com/3501/4073431405_4d4ec4b8f4_o.png簡單的 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']['type'] = 'video';
      $data['content']['attachment']['media']['video_src'] = 'http://www.youtube.com/v/fzzjgBAaWZw&hl=en&fs=1';
      $data['content']['attachment']['media']['preview_img'] = 'http://i2.ytimg.com/i/uZp-cf0yOTqQEOuKGGnocg/1.jpg';
      $data['content']['attachment']['media']['video_link'] = 'http://www.youtube.com/watch?v=muLIPWjks_M';
      $data['content']['attachment']['media']['video_title'] = 'ninja cat';
      $data['method'] = 'publisher_getFeedStory';
}
echo json_encode( $data );
?>
其中,上述發佈用的 template 允許四種 type ,分別為 image , flash, mp3, vedio ,細節請參考 Facebook - Attachment (Streams) 參考資料

Facebook - Publisher [*]Facebook - Attachment (Streams)

woff 發表於 2013-2-1 19:30:58

{:5_4:}牛X海海!!!

emaremar45 發表於 2013-3-8 02:18:26

支持一下吧,確實是不錯的貼子。

cbzdy 發表於 2014-7-3 15:16:35

我喜欢,请大家鼓掌支持,谢谢











bjcars.net

hdzqg 發表於 2014-11-5 18:31:47

这是真的吗?太好了,谢谢您啊
頁: [1]
查看完整版本: Facebook API Publisher 使用 Attach 附件服務增加發佈塗鴉牆的效果