我正在写我的Word新闻界,并得到了这个想法。与其实施“Like/Fav”功能来确定顶级文章,我想反而将计算在一起的有多少facebook共享,推文和+1数据存储在数据库中(根据相关文章) ,所以我可以通过选择大多数股票,推文和+1的选择顶级文章。每次用户点击facebook,twitter或g +按钮时,我还需要更新数据库。将facebook,twitter和g +“股票”放在一起并将它们存储在数据库中?
这是可以在Word Press和使用他们的api的?
我正在写我的Word新闻界,并得到了这个想法。与其实施“Like/Fav”功能来确定顶级文章,我想反而将计算在一起的有多少facebook共享,推文和+1数据存储在数据库中(根据相关文章) ,所以我可以通过选择大多数股票,推文和+1的选择顶级文章。每次用户点击facebook,twitter或g +按钮时,我还需要更新数据库。将facebook,twitter和g +“股票”放在一起并将它们存储在数据库中?
这是可以在Word Press和使用他们的api的?
这并不像看起来那么简单。
GitHub上有一个很棒的要点,你要实现所有的API:Get the share counts from various APIs。
您可以连接到这些服务器并获取使用jQuery和AJAX数据:
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
}
})
}
然后,您可以用另一个AJAX请求到自己的博客更换// do something with it
线。 您需要编写一个插件来处理这个请求并将数据保存在$ wpdb中。插件比较简单:
<?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()
这将是:
$.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',
success: function(data) {
var saved = data.sscrp_saved;
if(saved) {
// done!
} else {
// oh crap, an error occured
}
}
});
对于how_many_fb_shares()
从how_many_tweets()
复制/粘贴代码,只需更改:
...
url: "... &type=facebook ...
...
对于how_many_google_pluses()
一样做与Facebook:
...
url: "... &type=googleplus ...
...
然后,你必须使用$type
和你写你的$wpdb
$count
到莫名其妙过滤自己的帖子。
我得走了。我向您提供的不仅仅是关于连接到Facebook,Twitter和Google API的简单信息。我希望你会利用它并实现你想达到的目标。
这是一个有趣的阅读,谢谢你的扩展答案,我会接受它,给你+50奖励,我可以 – Ilja 2013-03-03 00:06:10
谢谢!很高兴我能帮上忙。 :) – akashivskyy 2013-03-03 17:10:36
@akashivskyy我有一个问题给你,我想获取用户提要,并从社交媒体发布和存储到数据库使用那里的API。请帮我解决这个难题。 – Apache 2014-11-28 09:16:52