$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>javascript - Facebook, Twitter, LinkedIn share link count - Stack Overflow|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

javascript - Facebook, Twitter, LinkedIn share link count - Stack Overflow

matteradmin8PV0评论

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?

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?

Share Improve this question edited Oct 14, 2020 at 12:19 Adam Azad 11.3k5 gold badges30 silver badges72 bronze badges asked Jan 9, 2014 at 5:38 SajithSajith 8564 gold badges20 silver badges48 bronze badges 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? – Adam Azad Commented Jan 9, 2014 at 9:45
  • i have removed that tag – Sajith Commented Jan 9, 2014 at 10:42
  • Try meddelare.com =) – Joel Purra Commented Nov 22, 2015 at 10:28
Add a comment  | 

5 Answers 5

Reset to default 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)

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

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

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/

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'];
Post a comment

comment list (0)

  1. No comments so far