Facebook, Twitter, LinkedIn share link count
Asked Answered
D

5

21

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 LinkedIn to calculate the popularity of some content?

How to get share count my particular API? Is there any other option? Is there some API?

Dumbhead answered 9/1, 2014 at 5:38 Comment(3)
I can answer your question but I'm a bit confused why did you tag jQuery in your question? Do you want to dynamically load those results? using $.getJSON or $.ajax?Tedder
i have removed that tagDumbhead
Try meddelare.com =)Lightman
D
5

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;}
Dumbhead answered 3/2, 2014 at 5:34 Comment(1)
The Twitter counts.json link is no longer valid for this purpose. See: twittercommunity.com/t/…Ceresin
T
29

Update

Unfortunately, Twitter share count is impossible with API 1.1

Ref: intgr/(number) is number of shares and all responses are in JSON

Facebook

http://graph.facebook.com/?id=http://{URL}

Returns:

{
   "id": "http://{URL}",
   "shares": intgr/(number)
}

Twitter

http://cdn.api.twitter.com/1/urls/count.json?url=http://{URL}

Returns:

{  
   "count": intgr/(number)
   "url":"http:\/\/{URL}\/"
}

v1.1 Update

Version 1.1 of Twitter API which does not support count.json. Thus, it will not be possible to retireve tweets counts. However, I figured out that Tweet Buttons use a custom endpoint to get these numbers.

Here's the new endpoint

https://cdn.syndication.twitter.com/widgets/tweetbutton/count.json?url={URL}

I am not sure whether Twitter will take this endpoint down, and replace it when they officially shutdown v 1.0, but it works.

LinkedIn

http://www.linkedin.com/countserv/count/share?url=http://{URL&format=json

Returns:

{
   "count": intgr/(number),
   "fCnt": "intgr/(number)",
   "fCntPlusOne":"intgr/(number) + 1", // increased by one
   "url":"http:\/\/{URL}"
}

Getting shares count with jQuery

Ref: for Twitter and linkdIn I had to add callback to get a response

HTML:

<p id="facebook-count"></p>
<p id="twitter-count"></p>
<p id="linkdin-count"></p>

JS:

$('#getJSON').click( function () {

    $('#data-tab').fadeOut();
    $URL = $('#urlInput').val();

    // Facebook Shares Count
    $.getJSON( 'http://graph.facebook.com/?id=' + $URL, function( fbdata ) {
        $('#facebook-count').text( 'The URL has ' + ReplaceNumberWithCommas(fbdata.shares) + ' shares count on Facebook')
    });

    // Twitter Shares Count
    $.getJSON( 'http://cdn.api.twitter.com/1/urls/count.json?url=' + $URL + '&callback=?', function( twitdata ) {
        $('#twitter-count').text( 'The URL has ' + ReplaceNumberWithCommas(twitdata.count) + ' shares count on Twitter')
    });

    // LinkIn Shares Count
    $.getJSON( 'http://www.linkedin.com/countserv/count/share?url=' + $URL + '&callback=?', function( linkdindata ) {
        $('#linkdin-count').text( 'The URL has ' + ReplaceNumberWithCommas(linkdindata.count) + ' shares count on linkdIn')
    });

    $('#data-tab').fadeIn();

});

Complete Fiddle

UPDATE:

Another Fiddle (returns the total shares across the 3 above)

Tedder answered 9/1, 2014 at 10:0 Comment(15)
@Adm Azad, Could you please put on full code i am confused how to take this my codeDumbhead
Updated my answer, the complete working fiddle jsfiddle.net/AdamAzad/5TxBdTedder
let me know one thing, we can get share count choose only AppId like facebook and LinkedIn etc.. because my another requirement i wont passing link i have only AppId i need to get all link total share count. is there any chance for thatDumbhead
"i need to get all link total share count" that's possible by using JS operators to get sum the of shares, can you explain more?Tedder
I have only app Id i don't have share link because i need to report in admin area for the total share for facebook,linkedin,twitter etc..Dumbhead
if there is no chance, if i stored all share link to the database then how to do get total count each Social media ie,fb,linkedin,twitter...Dumbhead
You don't need the app ID! the app ID used to authorize usersTedder
I have stored All shared link in to the database i need to the sum of the all shared link count how to do this. Is there any code. please help me if you dont mind put examples for Total share count for some shared link, i would really appreciate ..Dumbhead
Sorry for late reply, I updated the fiddle it counts Facebook, Twitter & LinkdIn shares and the total share across all three above, it's a basic exampleTedder
I have tried it but i cant adding for the total, getting NAN Share count errorDumbhead
The fiddle works just fine! If you're trying something on your own then post your code/create a fiddleTedder
@Dumbhead You're right about NaN error, the problem is that your connection might be slow so it takes longer time to get the json data so I added a function to check if json call are finished or not instead of getting the attr(); right away so here's the updated fiddle jsfiddle.net/AdamAzad/9Fhd6/7, I see that you found an answer for yourself :) but I didn't know that you work on .NET because it's not my in workflows list anyways ..Tedder
As of October 2015 Twitter will be deprecating that endpoint so it will no longer work, see: twittercommunity.com/t/…Silvasilvain
@Silvasilvain I am aware of this; thanks. They posted a similar question here #32747102Tedder
This feature has now been entirely deprecated by LinkedIn: developer.linkedin.com/blog/posts/2018/…Despotism
D
5

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;}
Dumbhead answered 3/2, 2014 at 5:34 Comment(1)
The Twitter counts.json link is no longer valid for this purpose. See: twittercommunity.com/t/…Ceresin
H
1

This Javascript class will let you fetch share information from Facebook, Twitter and LinkedIn.

Example of usage

<p>Facebook count: <span id="facebook_count"></span>.</p>
<p>Twitter count: <span id="twitter_count"></span>.</p>
<p>LinkedIn count: <span id="linkedin_count"></span>.</p>
<script type="text/javascript">
    var smStats=new SocialMediaStats('https://google.com/'); // Replace with your desired URL
    smStats.facebookCount('facebook_count'); // 'facebook_count' refers to the ID of the HTML tag where the result will be placed.
    smStats.twitterCount('twitter_count');
    smStats.linkedinCount('linkedin_count');    
</script>

Download

https://404it.no/js/blog/SocialMediaStats.js

Add to HTML header like this:

<script type="text/javascript" src="SocialMediaStats.js"></script>

More examples and documentation

Javascript Class For Getting URL Shares On Facebook, Twitter And LinkedIn

Halidom answered 27/1, 2015 at 8:17 Comment(0)
O
0

check this:

//
http://www.jsfiddle.net/eXJBs
//

//to change the URL to yours, you will find a http://www.google.com just take it off and add your http://www.yourdomain.com

and you can check up this facebook developer pages too which is very useful https://developers.facebook.com/docs/plugins/share-button/

Oceania answered 9/1, 2014 at 8:1 Comment(0)
C
0

This function will fetch share counts from facebook, g-plus, linkedin, pinterest

function social_share_counts($pageurl)
{
    //facebook

    $fb_data = file_get_contents('http://graph.facebook.com/'.$pageurl);
    $fb_json = json_decode($fb_data,TRUE);
    if(count($fb_json) > 1){
        $facebook_shares = $fb_json['shares'];
    }
    else{
        $facebook_shares = 0;
    }
    //facebook

    //google
    $curl = curl_init();
    curl_setopt( $curl, CURLOPT_URL, "https://clients6.google.com/rpc" );
    curl_setopt( $curl, CURLOPT_POST, 1 );
    curl_setopt( $curl, CURLOPT_POSTFIELDS, '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' . $pageurl . '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]' );
    curl_setopt( $curl, CURLOPT_RETURNTRANSFER, true );
    curl_setopt( $curl, CURLOPT_HTTPHEADER, array( 'Content-type: application/json' ) );
    $curl_results = curl_exec( $curl );
    curl_close( $curl );
    $google_json = json_decode( $curl_results, true );

    $google_shares = intval( $google_json[0]['result']['metadata']['globalCounts']['count'] );
    //google

    //linkedin
    $linkedin_data = file_get_contents('https://www.linkedin.com/countserv/count/share?format=json&url='.$pageurl);
    $linkedin_json = json_decode($linkedin_data);
    $linkedin_shares = $linkedin_json->count;
    //linkedin

    //pinterest
    $pinterest_data = file_get_contents('http://api.pinterest.com/v1/urls/count.json?callback=sharecount&url='.$pageurl);
    preg_match('/"count":(\d*).+$/',$pinterest_data,$match);
    $pinterest_shares = $match[1];
    //pinterest

    return array("facebook"=>$facebook_shares,"google"=>$google_shares,"linkedin"=>$linkedin_shares,"pinterest"=>$pinterest_shares);
}

//now check->

$share_cnts = social_share_counts('https://www.facebook.com');
echo $share_cnts['facebook'];
Chastitychasuble answered 5/5, 2016 at 17:29 Comment(1)
test sdbhj sd shadjk hkdsh sakjd husadh kasjdh kDumbhead

© 2022 - 2024 — McMap. All rights reserved.