Advertisement
tpaper

Untitled

May 28th, 2015
396
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 2.87 KB | None | 0 0
  1. <?php
  2. //Questo script in php "dumpa" le informazioni sui post di uno specifico gruppo di facebook in un database MYSQL
  3. //E' stato scritto "al volo" in poche ore ed è quindi scritto male. Non escludo che in futuro ne pubblicherò una versione aggiornata.
  4. //
  5. //Enrico 'tpaper' Ronconi - 28 may 2015 - V 10^(-9) ALPHA
  6. //
  7. //PS: Per poterla usare è necessario un token valido di un utente che ha accesso al feed del gruppo.
  8. //PPS: Se usate lo script per analizzare gruppi "famosi" o di cui sono membro gradirei essere citato come autore dello script. Voi potete prendervi i meriti per l'elaborazione successiva dei dati.
  9.  
  10. $count = 0;
  11.  
  12.   $MYSQL_host = 'localhost';
  13.   $MYSQL_username = 'SALANI';
  14.   $MYSQL_password = 'SALANI';
  15.   $MYSQL_database = 'fbstat';
  16.  
  17.   $MYSQL = @new mysqli($MYSQL_host, $MYSQL_username, $MYSQL_password, $MYSQL_database); //Start connection
  18.  
  19.   if($MYSQL->connect_error) die('Error while connection (Errno: '.$MYSQL->connect_errno.' - '.$MYSQL->connect_error.")!\nAborted\n"); echo("Succesfully connected!\n");
  20.  
  21.   $MYSQL->set_charset("utf8");
  22. $mysqli = $MYSQL;
  23.  
  24. $request_url ="https://graph.facebook.com/
  25. SALANI/
  26. feed?
  27. fields=from,message,likes.limit(0).summary(true),comments.limit(1).summary(true)&
  28. limit=800&
  29. access_token=SALANI";
  30.  
  31. $prossimo = req($request_url);
  32.  
  33. while($prossimo != NULL) {
  34.     $prossimo = req($prossimo);
  35. }
  36.  
  37. echo("\n\nTot: $count");
  38.  
  39. function req($url){
  40.     global $mysqli;
  41.     global $count;
  42.     $requests = file_get_contents(str_replace(array("\r", "\n"), "", $url));
  43.     //$requests = file_get_contents($url);
  44.  
  45.     $fb_response = json_decode($requests);
  46.     $messaggi = $fb_response->{"data"};
  47.     if(count($messaggi) == 0) return NULL;
  48.     foreach ($messaggi as $messaggio) {
  49.         $count = $count +1;    
  50.         $c1 = $messaggio->{"id"};
  51.         $c2 = $messaggio->{"from"}->{"id"};
  52.         $c3 = $mysqli->real_escape_string($messaggio->{"from"}->{"name"});
  53.         $c4 = $messaggio->{"likes"}->{"summary"}->{"total_count"};
  54.         $c5 = $messaggio->{"comments"}->{"summary"}->{"total_count"};
  55.         $c6 = explode("+",str_replace("T", " ", $messaggio->{"updated_time"}));
  56.         $c6 = $c6[0];
  57.         if(array_key_exists(0,$messaggio->{"comments"}->{"data"})){
  58.             if($messaggio->{"comments"}->{"summary"}->{"order"} != "chronological") die("err chrono\n");
  59.             $c7 = '"'.$messaggio->{"comments"}->{"data"}[0]->{"from"}->{"id"}.'"';
  60.             $c8 = '"'.$mysqli->real_escape_string($messaggio->{"comments"}->{"data"}[0]->{"from"}->{"name"}).'"';
  61.         } else {
  62.             $c7 = "NULL";
  63.             $c8 = "NULL";
  64.         }      
  65.         $out = $mysqli->query("INSERT INTO degradoland
  66.         (post_id,user_id,user_name,like_n,comment_n,date,first_com_user_id,first_com_user_name)
  67.         VALUES
  68.         (\"$c1\", \"$c2\", \"$c3\", $c4, $c5, \"$c6\", $c7, $c8);
  69.         ");
  70.         if(!$out) {die("query err ".$mysqli->error." \n");}
  71.         $lastdate = $messaggio->{"updated_time"};
  72.     }
  73.     echo("n: $count - $lastdate\n");
  74.     return $fb_response->{"paging"}->{"next"};
  75. }
  76. $mysqli->close()
  77. ?>
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement