Php 把facebook、twitter和g+算在一起&引用;股份;并将它们存储在数据库中?
我当时正在做我的文字出版工作,我有了这个想法。我不想使用“Like/Fav”功能来确定热门文章,而是想计算出该文章收到的facebook共享、推文和+1的数量,一旦全部计算完毕,将它们存储在数据库中(根据文章),这样我就可以通过选择共享最多的文章来选择热门文章,推特和+1。我还需要在用户每次点击facebook、twitter或g+按钮时更新数据库Php 把facebook、twitter和g+算在一起&引用;股份;并将它们存储在数据库中?,php,facebook,wordpress,twitter,google-plus,Php,Facebook,Wordpress,Twitter,Google Plus,我当时正在做我的文字出版工作,我有了这个想法。我不想使用“Like/Fav”功能来确定热门文章,而是想计算出该文章收到的facebook共享、推文和+1的数量,一旦全部计算完毕,将它们存储在数据库中(根据文章),这样我就可以通过选择共享最多的文章来选择热门文章,推特和+1。我还需要在用户每次点击facebook、twitter或g+按钮时更新数据库 这在Word Press内部和使用其api是否可以实现?这并不像看上去那么简单。 关于GitHub以及您想要实现的所有API,有一个很好的要点: 您
这在Word Press内部和使用其api是否可以实现?这并不像看上去那么简单。 关于GitHub以及您想要实现的所有API,有一个很好的要点: 您可以连接到这些服务器并使用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请求来替换//用它做点什么
行您需要编写一个插件来处理此请求并将数据保存在$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');
?>
how\u fb\u shares()
从how\u tweets()
复制/粘贴代码,只需更改:
...
url: "... &type=facebook ...
...
有多少个google plus()
与facebook相同:
...
url: "... &type=googleplus ...
...
$wpdb
的$type
和$count
来过滤您的帖子
我得走了。我为您提供的不仅仅是关于连接Facebook、Twitter和谷歌API的简单信息。我希望你能利用它,实现你想要实现的目标。这是一篇有趣的文章,感谢你的补充回答,我会接受它,并在我完成后给你+50奖励!很高兴我能帮忙。:)@Akashivsky我有一个问题想问你,我想从社交媒体获取用户提要和帖子,并使用API存储到数据库。请帮我解决这个困难。
...
url: "... &type=googleplus ...
...