有没有办法让用户在Facebook,Twitter,LinkedIn上分享一些链接
示例:有多少次链接被分享给Facebook,Twitter,LinkedIn以计算某些内容的受欢迎程度。
如何分享我的特定api?还有其他选择吗? 有没有api?
答案 0 :(得分:29)
<强>更新强>
不幸的是,使用API 1.1无法实现Twitter共享计数
参考:intgr/(number)
是分享数,所有回复都在JSON
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}\/"
}
Twitter API 1.1版,不支持count.json
。因此,不可能退出推文计数。但是,我发现Tweet Buttons使用自定义端点来获取这些数字。
这是新的终点
https://cdn.syndication.twitter.com/widgets/tweetbutton/count.json?url={URL}
我不确定Twitter是否会将此端点关闭,并在正式关闭v 1.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}"
}
参考:对于Twitter和linkdIn我必须添加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();
});
Another Fiddle (returns the total shares across the 3 above)
答案 1 :(得分:9)
我找到了答案...... !!!!!!!
数据网址 您可以在此处找到数据
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;}
答案 2 :(得分: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>
下载强>
https://404it.no/js/blog/SocialMediaStats.js
像这样添加到HTML标题:
<script type="text/javascript" src="SocialMediaStats.js"></script>
更多示例和文档
Javascript Class For Getting URL Shares On Facebook, Twitter And LinkedIn
答案 3 :(得分:0)
检查一下:
//
http://www.jsfiddle.net/eXJBs
//
//要更改您的网址,您会发现http://www.google.com只需将其取下并添加http://www.yourdomain.com
你也可以查看这个Facebook开发者页面,这非常有用 https://developers.facebook.com/docs/plugins/share-button/
答案 4 :(得分: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'];