Facebook, Twitter, LinkedIn share link count

后端 未结 5 1448
小蘑菇
小蘑菇 2020-12-04 12:48

Is there any way to get the number of users to share a link on Facebook, Twitter, and LinkedIn?

Example: How many times some link was shared to Facebook, Twitter, and

5条回答
  •  甜味超标
    2020-12-04 13:23

    I found Answer ...!!!!!!!

    Data URLs Here’s where you can find the data

    Facebook    http://graph.facebook.com/?ids=YOURURL
    Twitter http://urls.api.twitter.com/1/urls/count.json?url=YOURURL
    Google  https://clients6.google.com/rpc [see below for JSON-RPC]
    

    Note: Since I’m using “dynamic,” this requires .NET 4.0. Also, I’m using the JavaScriptSerializer class which is officially depreciated, but will probably not actually be removed. You could also easily use Regex to get these simple values.*

    int GetTweets(string url) {
    
        string jsonString = new System.Net.WebClient().DownloadString("http://urls.api.twitter.com/1/urls/count.json?url=" + url);
    
        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
        int count = (int)json["count"]; 
    
        return count;
    }
    
    int GetLikes(string url) {
    
        string jsonString = new System.Net.WebClient().DownloadString("http://graph.facebook.com/?ids=" + url);
    
        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
        int count = json[url]["shares"];
    
        return count;
    }
    
    int GetPlusOnes(string url) {
    
        string googleApiUrl = "https://clients6.google.com/rpc"; //?key=AIzaSyCKSbrvQasunBoV16zDH9R33D88CeLr9gQ";
    
        string postData = @"[{""method"":""pos.plusones.get"",""id"":""p"",""params"":{""nolog"":true,""id"":""" + url + @""",""source"":""widget"",""userId"":""@viewer"",""groupId"":""@self""},""jsonrpc"":""2.0"",""key"":""p"",""apiVersion"":""v1""}]";
    
        System.Net.HttpWebRequest request = (System.Net.HttpWebRequest)System.Net.WebRequest.Create(googleApiUrl);
        request.Method = "POST";
        request.ContentType = "application/json-rpc";
        request.ContentLength = postData.Length;
    
        System.IO.Stream writeStream = request.GetRequestStream();
        UTF8Encoding encoding = new UTF8Encoding();
        byte[] bytes = encoding.GetBytes(postData);
        writeStream.Write(bytes, 0, bytes.Length);
        writeStream.Close();
    
        System.Net.HttpWebResponse response = (System.Net.HttpWebResponse)request.GetResponse();
        System.IO.Stream responseStream = response.GetResponseStream();
        System.IO.StreamReader readStream = new System.IO.StreamReader(responseStream, Encoding.UTF8);
        string jsonString = readStream.ReadToEnd();
    
        readStream.Close();
        responseStream.Close();
        response.Close();
    
        var json = new System.Web.Script.Serialization.JavaScriptSerializer().Deserialize(jsonString);
        int count = Int32.Parse(json[0]["result"]["metadata"]["globalCounts"]["count"].ToString().Replace(".0", ""));
    
        return count;}
    

提交回复
热议问题