多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

中國最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2

json教程

  • 關于 JSON
  • JSON 教程

    json格式

    JSON解析

    JSON遍歷

    JSON調用

    JSON轉換

    JSON獲取

    JSON字符串

    JSON數組

    JSONP獲取Twitter和Facebook文章數的具體步驟

    閱讀 (2390)

    Twitter和Facebook要求身份驗證來獲取文章的數量,但事實證明你可以通過JSONP來獲取這些信息。請參考下面的步驟。


    The JavaScript

    我將使用基本的JavaScript來告訴你如何做到這一點:

    代碼如下:
    // 獲取文章數量的封裝對象 
    var socialGetter = (function() { 
    /* JSONP: 獲取腳本的工具函數 */ 
    function injectScript(url) { 
    var script = document.createElement('script'); 
    script.async = true; 
    script.src = url; 
    document.body.appendChild(script); 
    } 
    
    return { 
    getFacebookCount: function(url, callbackName) { 
    injectScript('https://graph.facebook.com/?id=' + url + '&callback=' + callbackName); 
    }, 
    getTwitterCount: function(url, callbackName) { 
    injectScript('http://urls.api.twitter.com/1/urls/count.json?url=' + url + '&callback=' + callbackName); 
    } 
    }; 
    })(); 
    
    // 回調方法 
    function twitterCallback(result) { 
    result.count && console.log('The count is: ', result.count); 
    } 
    function facebookCallback(result) { 
    result.shares && console.log('The count is: ', result.shares); 
    } 
    
    // 調用 
    socialGetter.getFacebookCount('http://davidwalsh.name/twitter-facebook-jsonp', 'facebookCallback'); 
    socialGetter.getTwitterCount('http://davidwalsh.name/twitter-facebook-jsonp', 'twitterCallback'); 
    
    因為有眾多輕量級的 micro-frameworks來處理JSONP,所以本文最重要的部分可能是獲取信息的url了。根據需要和習慣選擇你的JSONP工具! 

    Twitter和Facebook對于這些請求肯定有數量和頻率上的限制,所以如果你的網站訪問量很大,則JSONP很可能會被攔截或屏蔽. 一種快速的解決方案是將文章數量信息存儲在sessionStorage中,但這只是針對單個用戶的方式。如果你運行的網站流量較大,你最好選擇在服務器端抓取數據并緩存下來,并在一定的時間內自動刷新。


    國外社交網站獲取分享數量APIs

    Twitter


    GET URL:

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

    返回結果:

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

    Facebook


    GET URL:

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

    返回結果:

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

    Pinterest


    GET URL:

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

    返回結果:

    ({"count": 0, "url": "http://stylehatch.co"})

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

    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/

      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返回的數據,可以嘗試在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;允許跨域訪問數據。

    將代碼進行封裝

      我們將上面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();
    });

    關閉
    程序員人生
    主站蜘蛛池模板: 伊人久久国产 | 一级毛片在线完整免费观看 | 欧美另类videosbestsex日本 | 日韩精品一区二区三区免费视频 | 毛片一级在线观看 | 亚洲精品久久久久久久久久ty | www.日本高清.com| 中文字幕最新在线 | 中文字幕亚洲综合久久2 | 在线观看免费精品国自产 | 欧洲美女人牲交一级毛片 | 日韩欧美一区二区三区不卡 | 永久免费网站 | 日本日本 | 欧美手机手机在线视频一区 | 亚洲精品一区二区三区在线观看 | 日韩欧美视频在线 | 国产免费久久精品99久久 | 三级毛片视频 | 噜噜影视| 欧美午夜理伦三级理论三级 | 亚洲国产成人久久一区久久 | wwwxx欧美 | 欧美爱爱网址 | 欧美成人精品不卡视频在线观看 | 亚洲爆乳无码一区二区三区 | 亚洲一区色图 | 俄罗斯videosex性欧美黑吊 | 网站在线观看 | 久久久久国产精品美女毛片 | 国产小情侣激情小视频免费看 | 亚洲人成在线观看男人自拍 | 亚洲邪恶天堂影院在线观看 | 色精品一区二区三区 | 国产高清精品入口91 | 最近的中文字幕大全免费版 | 日本欧美韩国一区二区三区 | 国产成人91| 717影院理论午夜伦不卡久久 | www在线播放 | 久草视频福利在线观看 |