Thursday, April 17, 2014

[Facebook API] Bir Gorusmedeki Tum Mesajlari Almak

Eger bir arkadasinila uzun sureli Facebook'tan iletisim kuruyorsaniz, ve o kisi  ile suan iletisimde degilsiniz ancak iletisime gecmek icin hos bir seyler yapmak istiyorsaniz, Facebook'ta olan guzel konusmalarinizi ona iletmek isteyebilirsiniz.

Kodlama kisisel olduguu icin, ne sintaks, ne testler hic bir sey yoktur. Sadece isinizi gorsun yeterli. :)

Oncelikle mesajlari Facebook API'si ile 30'arli gruplar halinde cekiyoruz. Sonrasinda onlari PHP tarafinda birlestiriyoruz.


// Should run under Graph API Explorer
SELECT thread_id, recent_authors, snippet FROM thread WHERE folder_id = 0 AND snippet = 'LAST MSG'
var last_page = 217;
var current = 0;
function fetch(){
var xmlhttp = new XMLHttpRequest();
xmlhttp.onreadystatechange = function () {
if (xmlhttp.readyState == 4) {
var msg = (xmlhttp.responseText);
var data = new FormData();
data.append('data', msg);
var xhr = new XMLHttpRequest();
xhr.open('POST', 'SOMEWHERE/fetch.php', true);
xhr.onload = function () {
};
xhr.send(data);
if (current > last_page) {
console.log('Finished.');
return;
}
current++;
fetch();
}
};
xmlhttp.open("GET", "https://graph.facebook.com/fql?q=SELECT%20author_id%2C%20body%2C%20created_time%20FROM%20message%20%0AWHERE%20thread_id%20%3D%20%271565550332685%27%20%0AORDER%20BY%20created_time%20ASC%0ALIMIT%20" + current * 30 + "%2C%2030&format=json&suppress_http_code=1&access_token=TOKEN_COMES_HERE", true);
xmlhttp.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
xmlhttp.send();
}
<?php
$data = $_REQUEST['data'];
file_put_contents(__DIR__ . '/chats2/'. microtime(true) , $data);
view raw 3_fetch.php hosted with ❤ by GitHub
<?php
// requies PHP5.4
$messages = [];
foreach (glob('chats2/*') as $file) {
/*if (filesize($file) < 120) {
unlink($file);
}*/
$data = json_decode(file_get_contents($file), true);
$messages = array_merge($messages, $data['data']);
}
$time [];
foreach ($messages as $message) {
$author[] = $message['author_id'];
$time[] = $message['created_time'];
$msg[] = $message['body'];
}
array_multisort($time, SORT_ASC, $messages);
foreach ($messages as $msg) {
if ($msg['author_id'] == 'YOUR FB ID') {
$sender = 'YOURNAME';
}
else {
$sender = 'FRIENDNAME';
}
echo '<b>', $sender, ' </b>', '<i>', date('d.m.Y H:i', $msg['created_time']), '</i><br>', $msg['body'], '<br><br>';
}
view raw 4_list.php hosted with ❤ by GitHub

No comments:

Post a Comment