Sajith
Sajith

Reputation: 856

Facebook, Twitter, LinkedIn share link count

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?

Upvotes: 21

Views: 19168

Answers (5)

Adam Azad
Adam Azad

Reputation: 11297

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)

Upvotes: 29

srv
srv

Reputation: 41

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'];

Upvotes: 0

Per Kristian
Per Kristian

Reputation: 795

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

Upvotes: 1

Sajith
Sajith

Reputation: 856

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;}

Upvotes: 5

geekslot
geekslot

Reputation: 213

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/

Upvotes: 0

Related Questions