Facebook, Twitter, LinkedIn share link count

后端 未结 5 1451
小蘑菇
小蘑菇 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:18

    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:

    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)

提交回复
热议问题