首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >计算一下facebook,twitter和g+的“份额”,并将它们存储在数据库中?

计算一下facebook,twitter和g+的“份额”,并将它们存储在数据库中?
EN

Stack Overflow用户
提问于 2013-02-28 20:54:59
回答 1查看 2.7K关注 0票数 5

我在我的Word Press方面工作,并得到了这个想法。而不是实施“喜欢/Fav”功能来确定热门文章,我想改为计算在一起有多少facebook的分享,推文和+1收到的文章,一旦他们都被统计在一起存储在数据库中(根据文章),所以我可以选择最多的分享,推特和+1的文章,然后我可以选择最热门的文章。我也需要更新数据库每次用户点击的facebook,推特或g+的按钮。

在Word Press中和使用他们的api可以做到这一点吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2013-03-02 21:40:46

这并不像看起来那么简单。

你想实现的所有API在GitHub上都有一个很好的要点:.

您可以连接到这些服务器并使用jQuery和AJAX获取数据:

代码语言:javascript
复制
function how_many_tweets(url, post_id) {
    var api_url = "http://cdn.api.twitter.com/1/urls/count.json";
    var just_url = url || document.location.href;
    $.ajax({
        url: api_url + "?callback=?&url=" + just_url,
        dataType: 'json',
        success: function(data) {
            var tweets_count = data.count;
            // do something with it
        }
    });
}

function how_many_fb_shares(url, post_id) {
    var api_url = "http://api.facebook.com/restserver.php";
    var just_url = url || document.location.href;
    $.ajax({
        url: api_url + "?method=links.getStats&format=json&urls=" + just_url,
        dataType: 'json',
        success: function(data) {
            var shares_count = data[0].total_count;
            // do something with it
        }
    });
};

function how_many_google_pluses(url, api_key, post_id) {
    var api_url = "https://clients6.google.com/rpc?key=" + api_key;
    var just_url = url || document.location.href;
    $.ajax({
        url: api_url,
        dataType: 'json',
        contentType: 'application/json',
        type: 'POST',
        processData: false,
        data: '[{"method":"pos.plusones.get","id":"p","params":{"nolog":true,"id":"' + just_url + '","source":"widget","userId":"@viewer","groupId":"@self"},"jsonrpc":"2.0","key":"p","apiVersion":"v1"}]',
        success: function(data) {
            var google_pluses = data.result.metadata.globalCounts.count;
            // do something with it
        }
    })
}

然后,可以将// do something with it行替换为对您的博客的另一个AJAX请求。你需要编写一个插件来处理这个请求,并将数据保存在$wpdb中。这个插件相对简单:

代码语言:javascript
复制
<?php

/*
    Plugin Name: Save Share Count Request Plugin
    Plugin URI: http://yourdomain.com/
    Description: This plugin catches 'save share count' requests and updates the database.
    Version: 1.0

*/

// check if request is a 'save share count' request
// i'm using sscrp_ prefix just not to redefine another function
// sscrp_ means SaveShareCountRequestPlugin_
function sscrp_is_save_share_count_request() {
    if(isset($_GET['_save_share_count_request'])) return true;
    else return false;
}

// save the count in database
function sscrp_save_share_count_in_wpdb($type, $count, $post_id) {

    // $count is your new count for the post with $post_id
    // $type is your social media type (can be e.g.: 'twitter', 'facebook', 'googleplus')
    // $post_id is post's id

    global $wpdb;

    // create a $wpdb query and save $post_id's $count for $type.
    // i will not show how to do it here, you have to work a little bit

    // return true or false, depending on query success.

    return false;
}

// catches the request, saves count and responds
function sscrp_catch_save_share_count_request() {
    if(sscrp_is_save_share_count_request()) {
        if(isset($_GET['type'])) {
            $social_media_type = $_GET['type'];
            $new_count = $_GET['value'];
            $post_id = $_GET['post_id'];
            if(sscrp_save_share_count_in_wpdb($social_media_type, $new_count, $post_id)) {
                header(sprintf('Content-type: %s', 'application/json'));
                die(json_encode(array("sscrp_saved"=>true)));
            } else {
                header(sprintf('Content-type: %s', 'application/json'));
                die(json_encode(array("sscrp_saved"=>false)));
            }
        } else {
            header(sprintf('Content-type: %s', 'application/json'));
            die(json_encode(array("sscrp_saved"=>false)));
        }
    }
}

// catch the save request just after wp is loaded
add_action('wp_loaded', 'sscrp_catch_save_share_count_request');

?>

当你有了你的插件,我们可以在你的JavaScript文件中编辑// do something with it行:

适用于how_many_tweets()

  1. 将是:

$.ajax({ url:"http://yourdomain.com/path_to_your_wp_installation/?_save_share_count_request=1&type=twitter&value=“+ tweets_count + "&post_id=”+ post_id,dataType:'json',成功:函数(数据){ var saved = data.sscrp_saved;if(saved) { //完成!}否则{ //哦,发生错误});

how_many_fb_shares()how_many_tweets()复制/粘贴代码,只需更改:

..。网址:"... &type=facebook ... ...

  • For how_many_google_pluses()和facebook做同样的事情:

..。网址:"... &type=googleplus ... ...

然后,你必须以某种方式使用$type$count过滤你的帖子,这是你已经写到你的$wpdb中的。

我这就得走了。我已经为您提供了更多关于连接Facebook、Twitter和Google API的简单信息。我希望你能利用它,实现你想要的目标。

票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/15136106

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档