国外社交网站获取分享数量APIs

2019-10-08 15:34 来源:未知

Facebook


**GET URL:**

http://graph.facebook.com/?id=http://stylehatch.co

重临结果:

{
   "id": "http://stylehatch.co",
   "shares": 61
}

LinkedIn


**GET URL:**

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

回来结果:

{
    "count":17,
    "fCnt":"17",
    "fCntPlusOne":"18",
    "url":"http://stylehatch.co"
}

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
}

 

  这里有三个网址封装了三个小插件,特意用来在页面上出示社交网站共享工具条,能够一向拿过来用,相比便于!

  FacebookTwitterLinkedIn正如常用,给出调用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回来的多寡,能够品尝在U传祺L前面加上&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;同意跨域访谈数据。

 

Twitter


GET URL:

http://cdn.api.twitter.com/1/urls/count.json?url=http://stylehatch.co

回到结果:

{
    "count":528,
    "url":"http://stylehatch.co/"
}

Pinterest


**GET URL:**

http://api.pinterest.com/v1/urls/count.json?callback=&url=http://stylehatch.co

重返结果:

({"count": 0, "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"
}]

将代码实行李包裹装

  我们将方面FacebookTwitterLinkedIn多少个社交网址的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();
});

 

  从前有一篇著作详细介绍了何等赢得网页在FacebookTwitterLinkedIn龃龉平台上分享的多寡,点击这里翻看。这里再推而广之一下!

TAG标签:
版权声明:本文由澳门国际银河备用网址发布于www.308877.com,转载请注明出处:国外社交网站获取分享数量APIs