How To Get Number of Facebook Likes for a URL or Website jQuery in C# and VB
Program:
.Aspx File
Program:
.Aspx File
<html xmlns="http://www.w3.org/1999/xhtml"> <head> <title>facebook</title> <script type="text/javascript" src="http://code.jquery.com/jquery-1.8.2.js"></script> <script type="text/javascript"> $(document).ready(function () { $('#btnurl').click(function () { var url = $('#txturl').val(); document.getElementById('tbDetails').style.display = 'block'; $.ajax({ type: "POST", url: "WebService.asmx/BindDatatable", data: "{urltxt:'" + url + "'}", contentType: "application/json; charset=utf-8", dataType: "json", success: function (data) { for (var i = 0; i < data.d.length; i++) { $("#tbDetails").append("<tr><td>" + data.d[i].Url + "</td><td>" + data.d[i].SharedCount + "</td><td>" + data.d[i].LikeCount + "</td><td>" + data.d[i].CommentCount + "</td><td>" + data.d[i].ClickCount + "</td><td>" + data.d[i].TotalCount + "</td></tr>"); } }, error: function (result) { alert("Error"); } }); }); }); </script> </head> <body> <form id="form1" runat="server"> <div> <table> <tr> <td><b>Enter Url:</b></td> <td><input type="text" id="txturl" /> </td> </tr> <tr> <td></td> <td><input type="button" id="btnurl" value="Get Url Count" /> </td> </tr> </table> </div> <div> <table id="tbDetails" cellpadding="1" cellspacing="1" style="border:solid 1px #000000; display:none"> <thead style="background-color:#DC5807; color:White; font-weight:bold"> <tr> <td>URL</td> <td>Shared Count</td> <td>Likes Count</td> <td>Comments Count</td> <td>Clicks Count</td> <td>Total Count</td> </tr> </thead> </table> </div> </form> </body> </html> |
Aspx.cs
using System.Collections.Generic; using System.Data; using System.IO; using System.Net; using System.Web.Services; [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] [System.Web.Script.Services.ScriptService] public class WebService : System.Web.Services.WebService { [WebMethod] public UrlDetails[] BindDatatable(string urltxt) { List<UrlDetails> details = new List<UrlDetails>(); WebClient web = new WebClient(); string url = string.Format("https://api.facebook.com/method/fql.query?query=SELECT url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='" + urltxt + "'"); string response = web.DownloadString(url); DataSet ds = new DataSet(); using (StringReader stringReader = new StringReader(response)) { ds = new DataSet(); ds.ReadXml(stringReader); } DataTable dt = ds.Tables["link_stat"]; foreach (DataRow dtrow in dt.Rows) { UrlDetails website = new UrlDetails(); website.Url = dtrow["url"].ToString(); website.LikeCount = dtrow["like_count"].ToString(); website.SharedCount = dtrow["share_count"].ToString(); website.CommentCount = dtrow["comment_count"].ToString(); website.ClickCount = dtrow["click_count"].ToString(); website.TotalCount = dtrow["total_count"].ToString(); details.Add(website); } return details.ToArray(); } public class UrlDetails { public string Url { get; set; } public string SharedCount { get; set; } public string LikeCount { get; set; } public string CommentCount { get; set; } public string ClickCount { get; set; } public string TotalCount { get; set; } } } |
VB
Imports System.Collections.Generic Imports System.Data Imports System.IO Imports System.Net Imports System.Web.Services ''' <summary> ''' Summary description for AutoCompleteService ''' </summary> ' To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. <WebService([Namespace]:="http://tempuri.org/")> _ <WebServiceBinding(ConformsTo:=WsiProfiles.BasicProfile1_1)> _ <System.Web.Script.Services.ScriptService()> _ Public Class WebService2 Inherits System.Web.Services.WebService <WebMethod()> _ Public Function BindDatatable(ByVal urltxt As String) As UrlDetails() Dim details As New List(Of UrlDetails)() Dim web As New WebClient() Dim url As String = String.Format("https://api.facebook.com/method/fql.query?query=SELECT url, share_count, like_count, comment_count, total_count, click_count FROM link_stat where url='" & urltxt & "'") Dim response As String = web.DownloadString(url) Dim ds As New DataSet() Using stringReader As New StringReader(response) ds = New DataSet() ds.ReadXml(stringReader) End Using Dim dt As DataTable = ds.Tables("link_stat") For Each dtrow As DataRow In dt.Rows Dim website As New UrlDetails() website.Url = dtrow("url").ToString() website.LikeCount = dtrow("like_count").ToString() website.SharedCount = dtrow("share_count").ToString() website.CommentCount = dtrow("comment_count").ToString() website.ClickCount = dtrow("click_count").ToString() website.TotalCount = dtrow("total_count").ToString() details.Add(website) Next Return details.ToArray() End Function Public Class UrlDetails Public Property Url() As String Get Return m_Url End Get Set(ByVal value As String) m_Url = value End Set End Property Private m_Url As String Public Property SharedCount() As String Get Return m_SharedCount End Get Set(ByVal value As String) m_SharedCount = value End Set End Property Private m_SharedCount As String Public Property LikeCount() As String Get Return m_LikeCount End Get Set(ByVal value As String) m_LikeCount = value End Set End Property Private m_LikeCount As String Public Property CommentCount() As String Get Return m_CommentCount End Get Set(ByVal value As String) m_CommentCount = value End Set End Property Private m_CommentCount As String Public Property ClickCount() As String Get Return m_ClickCount End Get Set(ByVal value As String) m_ClickCount = value End Set End Property Private m_ClickCount As String Public Property TotalCount() As String Get Return m_TotalCount End Get Set(ByVal value As String) m_TotalCount = value End Set End Property Private m_TotalCount As String End Class End Class |
Demo
|
No comments:
Post a Comment