Điều này không đơn giản như vậy.
Có một ý chính tuyệt vời về GitHub với tất cả các API bạn muốn triển khai: Get the share counts from various APIs.
Bạn có thể kết nối với các máy chủ và lấy dữ liệu sử dụng jQuery và 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
}
})
}
Sau đó, bạn có thể thay thế // do something with it
dòng với một yêu cầu AJAX vào blog của bạn. Bạn sẽ cần phải viết một plugin để xử lý yêu cầu này và lưu dữ liệu trong $ wpdb. Plugin tương đối đơn giản:
<?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');
?>
Khi bạn có plugin của bạn, chúng ta có thể chỉnh sửa // do something with it
dòng trong tập tin JavaScript của bạn:
Đối how_many_tweets()
nó sẽ là:
$.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()
sao chép/dán mã từ how_many_tweets()
và chỉ thay đổi:
...
url: "... &type=facebook ...
...
Đối how_many_google_pluses()
làm tương tự như với facebook:
...
url: "... &type=googleplus ...
...
Sau đó, bạn phải bằng cách nào đó lọc bài viết của mình sử dụng $type
và $count
mà bạn đã viết thư cho $wpdb
của bạn.
Tôi phải đi ngay bây giờ. Tôi đã cung cấp cho bạn nhiều thông tin đơn giản hơn về việc kết nối với các API của Facebook, Twitter và Google. Tôi hy vọng bạn sẽ tận dụng nó và đạt được những gì bạn muốn đạt được.
Đây là một bài đọc thú vị, cảm ơn bạn đã trả lời mở rộng, tôi sẽ chấp nhận nó và tặng cho bạn 50 giải thưởng khi tôi có thể – Ilja
Cảm ơn! Mừng vì tôi có thể giúp. :) – akashivskyy
@akashivskyy Tôi có một câu hỏi với bạn rằng tôi muốn tìm nạp nguồn cấp dữ liệu người dùng và đăng từ phương tiện truyền thông xã hội và lưu trữ đến cơ sở dữ liệu bằng cách sử dụng API đó. Xin hãy giúp tôi giải quyết khó khăn này. – Apache