用Facebook Query Language查出讚的點擊率
越來越多網站會使用Facebook的讚當作人氣指標
而Facebook也提供了許多種API讓使用者可以得到這些統計的資訊
今天來介紹用FQL(Facebook Query Language)來取得某一網址讚的點擊數
FQL的格式跟SQL很像,是以SELECT [fields] FROM [table] WHERE [conditions]這樣
的格式運作。
例如以今天的範例來說,要下的語法就是:
SELECT like_count, share_count, click_count, total_count
from link_stat where url="目標的URL"
官網的API還有提供許多用法,請參考Facebook Query Language (FQL)
將組合的FQL配上
https://api.facebook.com/method/fql.query?query=[FQL]
而返回的XML就會將所查詢的資訊帶回。
下面的範例我用黃偉榮的學習筆記的[C#]列舉切割器(IEnumerable Split)這篇文章來作範例
查詢這篇文章的讚及分享等統計
string api="https://api.facebook.com/method/fql.query?query=";
//FQL,where url為目標網址
string url ="SELECT like_count, share_count, click_count, total_count from link_stat where url=\"{0}\"";
//要統計的網址
string targetUrl="http://www.dotblogs.com.tw/wadehuang36/archive/2011/08/02/ienumerablesplit.aspx";
//做UrlPathEncode
string fql = HttpUtility.UrlPathEncode(String.Format(url,targetUrl));
組合出來的連結會是下面這樣
https://api.facebook.com/method/fql.query?query=SELECT%20like_count,%20share_count,%20click_count,%20total_count%20 |
接著建立一個WebRequest,將Url送出,然後接收WebResponse
WebRequest request= WebRequest.Create(String.Concat(api,fql));
using(WebResponse response = request.GetResponse())
using(Stream stream=response.GetResponseStream())
{
//...處理返回的Stream
}
如果將返回的Stream讀出來的話,可看到是一個XML的格式
<?xml version="1.0" encoding="UTF-8"?>
<fql_query_response xmlns="http://api.facebook.com/1.0/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" list="true">
<link_stat>
<like_count>5</like_count>
<share_count>1</share_count>
<click_count>0</click_count>
<total_count>6</total_count>
</link_stat>
</fql_query_response>
從XML看到這邊文章被點了5次讚,然後1人分享連結。
返回來的Stream可以用Linq to Xml來處理,也可以將Class訂好,用反序列化的方式轉為Class
[XmlRootAttribute("fql_query_response", Namespace="http://api.facebook.com/1.0/")]
public class FBQuery
{
[XmlElementAttribute("link_stat")]
public LinkState State { get; set; }
}
public class LinkState
{
[XmlElementAttribute("like_count")]
public int LikeCount { get; set; }
[XmlElementAttribute("share_count")]
public int ShareCount { get; set; }
[XmlElementAttribute("click_count")]
public int ClickCount { get; set; }
[XmlElementAttribute("total_count")]
public int TotalCcount { get; set; }
}
處理Stream
XmlSerializer xml = new XmlSerializer(typeof(FBQuery));
var result = xml.Deserialize(stream) as FBQuery;
結果
完整的Code
void Main()
{
string api="https://api.facebook.com/method/fql.query?query=";
//FQL,where url為目標網址
string url ="SELECT like_count, share_count, click_count, total_count from link_stat where url=\"{0}\"";
//要統計的網址
string targetUrl="http://www.dotblogs.com.tw/wadehuang36/archive/2011/08/02/ienumerablesplit.aspx";
//做UrlPathEncode
string fql = HttpUtility.UrlPathEncode(String.Format(url,targetUrl));
//建立WebRequest
WebRequest request= WebRequest.Create(String.Concat(api,fql));
using(WebResponse response = request.GetResponse())
using(Stream stream=response.GetResponseStream())
{
XmlSerializer xml = new XmlSerializer(typeof(FBQuery));
var result = xml.Deserialize(stream) as FBQuery;
}
}
[XmlRootAttribute("fql_query_response", Namespace="http://api.facebook.com/1.0/")]
public class FBQuery
{
[XmlElementAttribute("link_stat")]
public LinkState State { get; set; }
}
public class LinkState
{
[XmlElementAttribute("like_count")]
public int LikeCount { get; set; }
[XmlElementAttribute("share_count")]
public int ShareCount { get; set; }
[XmlElementAttribute("click_count")]
public int ClickCount { get; set; }
[XmlElementAttribute("total_count")]
public int TotalCcount { get; set; }
}