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?

Adam Azad
  • 11,171
  • 5
  • 29
  • 70
Sajith
  • 856
  • 4
  • 19
  • 48

5 Answers5

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)

Adam Azad
  • 11,171
  • 5
  • 29
  • 70
  • 1
    @Adm Azad, Could you please put on full code i am confused how to take this my code – Sajith Jan 09 '14 at 10:45
  • 3
    Updated my answer, the complete working fiddle http://jsfiddle.net/AdamAzad/5TxBd/ – Adam Azad Jan 09 '14 at 12:58
  • 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 that – Sajith Jan 24 '14 at 08:39
  • _"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? – Adam Azad Jan 24 '14 at 08:46
  • 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.. – Sajith Jan 24 '14 at 08:52
  • 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... – Sajith Jan 24 '14 at 08:55
  • You don't need the app ID! the app ID used to authorize users – Adam Azad Jan 24 '14 at 08:56
  • 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 .. – Sajith Jan 29 '14 at 11:18
  • Sorry for late reply, I updated the [fiddle](http://jsfiddle.net/AdamAzad/9Fhd6/) it counts Facebook, Twitter & LinkdIn shares and the total share across all three above, it's a basic example – Adam Azad Jan 30 '14 at 08:16
  • I have tried it but i cant adding for the total, getting NAN Share count error – Sajith Jan 30 '14 at 10:18
  • The fiddle works just fine! If you're trying something on your own then post your code/create a fiddle – Adam Azad Jan 30 '14 at 12:01
  • @Sajith 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 http://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 .. – Adam Azad Feb 08 '14 at 07:45
  • 2
    As of October 2015 Twitter will be deprecating that endpoint so it will no longer work, see: https://twittercommunity.com/t/a-new-design-for-tweet-and-follow-buttons/52791 – jimbo2087 Sep 29 '15 at 08:20
  • @jimbo2087 I am aware of this; thanks. They posted a similar question here http://stackoverflow.com/questions/32746602/how-do-i-fetch-twitter-share-count-via-php-after-the-upcoming-update – Adam Azad Sep 29 '15 at 08:22
  • This feature has now been entirely deprecated by LinkedIn: https://developer.linkedin.com/blog/posts/2018/deprecating-the-inshare-counter – Adam Garner Feb 19 '18 at 11:06
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;}
Sajith
  • 856
  • 4
  • 19
  • 48
  • The Twitter counts.json link is no longer valid for this purpose. See: https://twittercommunity.com/t/is-http-urls-api-twitter-com-1-urls-count-json-a-valid-way-to-get-the-tweet-count-for-a-url/13369 – mikeyq6 Mar 27 '15 at 16:04
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

Per Kristian
  • 785
  • 8
  • 10
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/

geekslot
  • 213
  • 1
  • 4
  • 11
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'];
srv
  • 41
  • 2