日本搞逼视频_黄色一级片免费在线观看_色99久久_性明星video另类hd_欧美77_综合在线视频

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

json教程

  • 關于 JSON
  • JSON 教程

    json格式

    JSON解析

    JSON遍歷

    JSON調用

    JSON轉換

    JSON獲取

    JSON字符串

    JSON數組

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

    閱讀 (2395)

    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/

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

    關閉
    程序員人生
    主站蜘蛛池模板: 欧美激情精品久久久久久变态 | 精品九九九 | 久久久久久久成人 | 亚洲成人一区二区 | 国产成人免费视频 | 99久久久久久 | 国产成人av一区二区三区 | 色片免费看 | 国内免费精品视频 | 国产精品久久久久久 | 成人性生交大片免费网站 | 久久99视频这里只有精品 | 色天天综合久久久久综合片 | 亚洲天堂电影 | 中文字幕一区二区三区四区 | 亚洲一区h| 一区二区三区日韩欧美 | 日韩欧美在线观看视频网站 | 国产精品电影在线观看 | 日韩一区精品 | 黄色在线免费 | 国产精品久久毛片av大全日韩 | av网站免费看 | 日韩免费中文字幕 | 96看片| 毛片毛片毛片毛片毛片毛片 | 成人免费在线视频 | 天堂在线资源8 | 国产91一区二区三区 | 日本一区二区三区视频在线播放 | 国产黄在线 | 日本最新一区二区 | 在线视频一区二区三区 | 一本色道久久综合亚洲二区三区 | 日韩成人在线观看 | 国产成年人 | 91国内精品久久 | 麻豆二区 | 91国产精品| 在线免费看黄 | 日韩福利|