Questo esempio spiega come poter gestire i click sul pulsante Like e i commenti nel plugin fb:comments di Facebook inseriti nel proprio sito web
Ascoltare il click sul pulsante Like button e sul pulsante Send button o l’inserimento di un commento nel plugin Comments box si può fare usando questo codice:
<script charset="utf-8" type="text/javascript" src="http://connect.facebook.net/it_IT/all.js"></script>
<script charset="utf-8" type="text/javascript">
function setFacebook(){
FB.init({ appId:'APP_ID', cookie:true, status:true, xfbml:true });
FB.Event.subscribe('edge.create',function(href){
voteFromFB( href1 );
});
FB.Event.subscribe('message.send',function(href){
voteFromFB( href2 );
});
FB.Event.subscribe('comment.create',function(comment){
commentFromFB( comment );
});
}
</script>
I primi 2 argomenti “href1″ e “href2″ dei log saranno gli url del like che verrà pubblicato su FB, il terzo argomento “comment” è invece un oggetto:
{
"commentID" : "7124678124012745120874",
"href" : "href indicato nel tag <fb:comments>"
}
La funzione commentFromFB può essere scritta così con Mootools e con jQuery:
// Mootools
function commentFromFB( comment ){
var Req = new Request.JSON({
url: 'script.php',
onSuccess: function( json ){
if( json.success ){
alert( json.last_comment );
} else {
alert( 'Errore' );
}
}
}).post({
'handle_comment': true,
'href_comment': comment.href
});
}
// jQuery
function commentFromFB( comment ){
$.post( "script.php",
{ 'handle_comment': true, 'href_comment': comment.href },
function(data){
if( data.success ){
alert( data.last_comment );
} else {
alert( 'Errore' );
}
},
"json"
);
}
il file script.php gestisce la chiamata così:
<?php
$out = array( 'comments' => '' );
if( isset( $_POST['handle_comment'] ) ){
/*
invio all'url https://graph.facebook.com/comments/ il parametro GET ?id='.$_POST['href_comment']...
ad esempio https://graph.facebook.com/comments/?id=www.esempio.com
*/
$comments = json_decode( file_get_contents( 'https://graph.facebook.com/comments/?id='.$_POST['href_comment'] ), true );
if(count($comments['data'])){
$last_comment = $comments['data'][ count($comments['data']) - 1 ];
/*
$last_comment sarà un array così composto:
{
"id": "524727245724",
"from": {
"name": "Nome Cognome",
"id": "id utente FB"
},
"message": "Testo del messaggio",
"created_time": "2011-11-07T21:41:06+0000"
}
potete quindi utilizzarlo come volete nel vostro backend
*/
$out = array( 'success' => true, 'last_comment' => $last_comment['comment'] );
} else {
$out = array( 'success' => true, 'last_comment' => 'Nessun messaggio' );
}
}
?>
A questo punto nel browser visualizzerete l’alert con il testo del messaggio per testare il funzionamento dello script.
Ascoltare Click su Like button e commenti nella Comments box
Questo esempio spiega come poter gestire i click sul pulsante Like e i commenti nel plugin fb:comments di Facebook inseriti nel proprio sito web
Ascoltare il click sul pulsante Like button e sul pulsante Send button o l’inserimento di un commento nel plugin Comments box si può fare usando questo codice:
<script charset="utf-8" type="text/javascript" src="http://connect.facebook.net/it_IT/all.js"></script> <script charset="utf-8" type="text/javascript"> function setFacebook(){ FB.init({ appId:'APP_ID', cookie:true, status:true, xfbml:true }); FB.Event.subscribe('edge.create',function(href){ voteFromFB( href1 ); }); FB.Event.subscribe('message.send',function(href){ voteFromFB( href2 ); }); FB.Event.subscribe('comment.create',function(comment){ commentFromFB( comment ); }); } </script>I primi 2 argomenti “href1″ e “href2″ dei log saranno gli url del like che verrà pubblicato su FB, il terzo argomento “comment” è invece un oggetto:
{ "commentID" : "7124678124012745120874", "href" : "href indicato nel tag <fb:comments>" }La funzione commentFromFB può essere scritta così con Mootools e con jQuery:
// Mootools function commentFromFB( comment ){ var Req = new Request.JSON({ url: 'script.php', onSuccess: function( json ){ if( json.success ){ alert( json.last_comment ); } else { alert( 'Errore' ); } } }).post({ 'handle_comment': true, 'href_comment': comment.href }); } // jQuery function commentFromFB( comment ){ $.post( "script.php", { 'handle_comment': true, 'href_comment': comment.href }, function(data){ if( data.success ){ alert( data.last_comment ); } else { alert( 'Errore' ); } }, "json" ); }il file script.php gestisce la chiamata così:
<?php $out = array( 'comments' => '' ); if( isset( $_POST['handle_comment'] ) ){ /* invio all'url https://graph.facebook.com/comments/ il parametro GET ?id='.$_POST['href_comment']... ad esempio https://graph.facebook.com/comments/?id=www.esempio.com */ $comments = json_decode( file_get_contents( 'https://graph.facebook.com/comments/?id='.$_POST['href_comment'] ), true ); if(count($comments['data'])){ $last_comment = $comments['data'][ count($comments['data']) - 1 ]; /* $last_comment sarà un array così composto: { "id": "524727245724", "from": { "name": "Nome Cognome", "id": "id utente FB" }, "message": "Testo del messaggio", "created_time": "2011-11-07T21:41:06+0000" } potete quindi utilizzarlo come volete nel vostro backend */ $out = array( 'success' => true, 'last_comment' => $last_comment['comment'] ); } else { $out = array( 'success' => true, 'last_comment' => 'Nessun messaggio' ); } } ?>A questo punto nel browser visualizzerete l’alert con il testo del messaggio per testare il funzionamento dello script.