Facebook、Twitter、LinkedIn的分享链接数量

21

有没有办法获取在Facebook、Twitter和LinkedIn上分享链接的用户数量?

例如:某个链接被分享到Facebook、Twitter和LinkedIn上的次数,以计算某些内容的受欢迎程度。

如何获取我特定 API 的分享计数?是否还有其他选择?是否有某些 API 可供使用?


我可以回答你的问题,但我有点困惑,为什么你在问题中标记了 jQuery?你想要动态加载这些结果吗?使用 $.getJSON 还是 $.ajax - Adam Azad
我已经移除了那个标签。 - Sajith
Try https://meddelare.com/ =) - Joel Purra
5个回答

29

更新

很遗憾,使用API 1.1无法获取Twitter分享计数。

参考:intgr/(数字)表示分享数量,所有响应均为JSON格式。

Facebook

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

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

推特

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

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

v1.1 更新

Twitter API 的第 1.1 版本不支持 count.json,因此无法检索推文计数。但是,我发现 Tweet Buttons 使用自定义终端点来获取这些数字。

以下是新的终端点:

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

我不确定Twitter是否会在正式关闭v1.0时删除此端点并替换它,但它是有效的。

领英

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

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

使用jQuery获取分享计数

参考:对于Twitter和LinkedIn,我需要添加callback 以获取响应

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();

});

完整的Fiddle

更新:

另一个Fiddle(返回上述3个的总共分享数)


1
@Adm Azad,您能否提供完整的代码?我不太清楚如何将其应用到我的代码中。 - Sajith
3
我已更新我的答案,完整的可运行代码在 http://jsfiddle.net/AdamAzad/5TxBd/ 上。 - Adam Azad
我只有应用程序ID,没有分享链接,因为我需要在管理区域报告Facebook、LinkedIn、Twitter等社交媒体的总共分享次数。 - Sajith
如果没有机会,如果我将所有分享链接存储到数据库中,那么如何获取每个社交媒体的总计数,例如fb、linkedin、twitter等... - Sajith
2
从2015年10月开始,Twitter将废弃该端点,因此它将不再起作用,请参见:https://twittercommunity.com/t/a-new-design-for-tweet-and-follow-buttons/52791 - jimbo2087
显示剩余10条评论

5

我发现了答案!!!!!

数据URL 这里是可以找到数据的地方

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]

注意: 由于我使用“动态”方式,这需要 .NET 4.0。此外,我使用的JavaScriptSerializer类已被官方弃用,但实际上可能不会被删除。您也可以轻松地使用正则表达式获取这些简单值。*

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

Twitter counts.json链接已不再适用于此用途。请参阅: 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

1
这个JavaScript类可以让你从Facebook、Twitter和LinkedIn获取分享信息。
使用示例:
<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

添加到HTML头部,像这样:

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

更多例子和文档

获取Facebook、Twitter和LinkedIn分享URL的Javascript类


0

此函数将从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'];

测试 sdbhj sd shadjk hkdsh sakjd husadh kasjdh k - Sajith

0

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接