国外社交网站获取分享数量APIs
2015-05-14 08:32
501 查看
之前有一篇文章详细介绍了如何获取网页在Facebook,Twitter和LinkedIn社交平台上分享的数量,点击这里查看。这里再扩充一下!
Twitter
GET URL:
返回结果:
Facebook
[b]GET URL:[/b]
返回结果:
Pinterest
[b]GET URL:[/b]
返回结果:
LinkedIn
[b]GET URL:[/b]
返回结果:
POST body:
返回结果:
返回结果:
这里有一个网站封装了一个小插件,专门用来在页面上显示社交网站分享工具条,可以直接拿过来用,比较方便!http://sharrre.com/
Facebook,Twitter,LinkedIn比较常用,给出调用API的例子:
IE浏览器可能会无法正确获取到Facebook返回的数据,可以尝试在URL后面加上&callback=?,这样JQuery会将它当成JSONP来调用。
Facebook还有另一个API返回XML数据,调用方法是这样的:
如果在IE浏览器下出现No Transport错误而无法获取到Facebook返回的数据,尝试在JavaScript代码的最前面加上$.support.cors = true;允许跨域访问数据。
然后在页面上这样调用:
http://cdn.api.twitter.com/1/urls/count.json?url=http://stylehatch.co
返回结果:
{ "count":528, "url":"http://stylehatch.co/" }
http://graph.facebook.com/?id=http://stylehatch.co
返回结果:
{ "id": "http://stylehatch.co", "shares": 61 }
http://api.pinterest.com/v1/urls/count.json?callback=&url=http://stylehatch.co
返回结果:
({"count": 0, "url": "http://stylehatch.co"})
http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&format=json
返回结果:
{ "count":17, "fCnt":"17", "fCntPlusOne":"18", "url":"http:\/\/stylehatch.co" }
Google Plus
POST URL:https://clients6.google.com/rpc?key=YOUR_API_KEY
POST body:
[{ "method":"pos.plusones.get", "id":"p", "params":{ "nolog":true, "id":"http://stylehatch.co/", "source":"widget", "userId":"@viewer", "groupId":"@self" }, "jsonrpc":"2.0", "key":"p", "apiVersion":"v1" }]
返回结果:
[{ "result": { "kind": "pos#plusones", "id": "http://stylehatch.co/", "isSetByViewer": false, "metadata": { "type": "URL", "globalCounts": { "count": 3097.0 } } } , "id": "p" }]
StumbledUpon
GET URL:http://www.stumbleupon.com/services/1.01/badge.getinfo?url=http://stylehatch.co
返回结果:
{ "result":{ "url":"http:\/\/stylehatch.co\/", "in_index":true, "publicid":"1iOLcK", "views":39, "title":"Style Hatch - Hand Crafted Digital Goods", "thumbnail":"http:\/\/cdn.stumble-upon.com\/mthumb\/941\/72725941.jpg", "thumbnail_b":"http:\/\/cdn.stumble-upon.com\/bthumb\/941\/72725941.jpg", "submit_link":"http:\/\/www.stumbleupon.com\/submit\/?url=http:\/\/stylehatch.co\/", "badge_link":"http:\/\/www.stumbleupon.com\/badge\/?url=http:\/\/stylehatch.co\/", "info_link":"http:\/\/www.stumbleupon.com\/url\/stylehatch.co\/" }, "timestamp":1336520555, "success":true }
这里有一个网站封装了一个小插件,专门用来在页面上显示社交网站分享工具条,可以直接拿过来用,比较方便!http://sharrre.com/
Facebook,Twitter,LinkedIn比较常用,给出调用API的例子:
// Facebook $.getJSON("http://graph.facebook.com/?id=http://stylehatch.co", function (d) { $("#fackbook_count").text("The Facebook Share count is: " + d.shares); }); // Twitter $.getJSON("http://cdn.api.twitter.com/1/urls/count.json?url=http://stylehatch.co&callback=?", function (d) { $("#twitter_count").text("The Twitter Share count is: " + d.count); }); // LinkedIn $.getJSON("http://www.linkedin.com/countserv/count/share?url=http://stylehatch.co&callback=?", function (d) { $("#linkedin_count").text("The LinkdeIn Share count is: " + d.count); });
IE浏览器可能会无法正确获取到Facebook返回的数据,可以尝试在URL后面加上&callback=?,这样JQuery会将它当成JSONP来调用。
Facebook还有另一个API返回XML数据,调用方法是这样的:
$.get("http://api.facebook.com/method/links.getStats?urls=http://stylehatch.co", function (d) { $("#fackbook_count").text("The Facebook Share count is: " + $(d).find("total_count").text()); });
如果在IE浏览器下出现No Transport错误而无法获取到Facebook返回的数据,尝试在JavaScript代码的最前面加上$.support.cors = true;允许跨域访问数据。
将代码进行封装
我们将上面Facebook,Twitter,LinkedIn三个社交网站的API进行封装,以方便页面调用。$.fn.getShareCount = function (url) { var self = this; var displayShareCount = function (val, obj) { if (!isNaN(val) && val > 0) { obj.show(); if (val > 999) { obj.attr("title", val); obj.text("500+"); } else obj.text(val); } }; return { facebook: function () { $.get("http://api.facebook.com/method/links.getStats?urls=" + url, function (d) { var c = $(d).find("total_count").text(); self.each(function () { displayShareCount(c, $(this)); }); }); }, twitter: function () { $.getJSON("http://cdn.api.twitter.com/1/urls/count.json?url=" + url + "&callback=?", function (d) { self.each(function () { displayShareCount(d.count, $(this)); }); }); }, linkedin: function () { $.getJSON("http://www.linkedin.com/countserv/count/share?url=" + url + "&callback=?", function (d) { self.each(function () { displayShareCount(d.count, $(this)); }); }); } }; };
然后在页面上这样调用:
$(function () { var shareUrl = window.location.href.toLowerCase(); $('#fackbook_count').getShareCount(shareUrl).facebook(); $('#twitter_count').getShareCount(shareUrl).twitter(); $('#linkedin_count').getShareCount(shareUrl).linkedin(); });
相关文章推荐
- 分享国外知名的源代码网站
- 分享32个优秀的国外电子商务网站设计案例
- 国内国外社交网站会聚用户分析
- 分享37个获取LOGO设计灵感的网站
- 如何在自己的网站或博客或空间加上社交分享按钮,猜你喜欢工具,社会化评论框
- 分享7个我常去的国外Ajax&Jquery网站
- 分享34个国外优秀的音乐主题网站作品
- 分享27个最新国外超酷单页面网站设计
- 分享国外几个主流的设计网站/社区
- 分享32个优秀的国外电子商务网站设计案例
- 【菜鸟分享日记】个人收藏的一些国外教程网站
- 2011年国外最受欢迎的15个社交网站
- 分享几个社交类网站常用并且也是最好用的jquery类库
- 分享JAVA开发者最常去的20个国外英文网站
- 国外社交网站个人首页的眼动研究情况
- 分享一些国外嵌入式开发的网站
- sharepoint 2007 获取所有网站集前40w个文件中启用历史版本文件的大小/数量/最后修改时间
- 国外热门网站的分享功能实现:facebook,twitter,google+1,tumblr等
- 国外一些新兴WEB2.0网站分享
- 分享7个我常去的国外Ajax&Jquery网站