PHP: ricerche su Twitter con JSON
Una semplice classe PHP per gestire le ricerche su Twitter con il formato JSON.
Possiamo effettuare ricerche su Twitter con PHP utilizzando il formato JSON come formato di risposta per i risultati creando una classe apposita per lo scopo. La classe da me realizzata, TwitterSearch, è di una semplicità disarmante in quanto si limita ad utilizzare la funzione json_decode() per creare un array associativo a partire dalla stringa JSON restituita da Twitter. Vediamo insieme i dettagli.
La classe è così strutturata:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
class TwitterSearch
{
const URL = 'http://search.twitter.com/search.json?q=';
public $term;
public function __construct($term)
{
$this->term = $term;
}
private function _niceTime($time)
{
$delta = time() - strtotime($time);
if ($delta < 60) {
return 'meno di 1 minuto fa.';
} else if ($delta < 120) {
return 'circa 1 minuto fa.';
} else if ($delta < (45 * 60)) {
return floor($delta / 60) . ' minuti fa.';
} else if ($delta < (90 * 60)) {
return 'circa 1 ora fa.';
} else if ($delta < (24 * 60 * 60)) {
return 'circa ' . floor($delta / 3600) . ' ore fa.';
} else if ($delta < (48 * 60 * 60)) {
return '1 giorno fa.';
} else {
return floor($delta / 86400) . ' giorni fa.';
}
}
private function _formatURLs($text)
{
return preg_replace('/http:\/\/(.*?)\/[^ ]*/', '<a href="\\0">\\0</a>', $text);
}
private function _fetch()
{
$jsonString = @file_get_contents(self::URL . urlencode($this->term));
if(!$jsonString) {
return null;
}
return $jsonString;
}
private function _parse()
{
$json = $this->_fetch();
if(is_null($json)) {
return null;
}
return json_decode($json, true);
}
public function render()
{
$output = $this->_parse();
$results = $output['results'];
$html = '';
if(is_null($output)) {
return '<p>Ricerca fallita.</p>';
}
foreach($results as $result) {
$profileImage = $result['profile_image_url'];
$from = $result['from_user'];
$id = $result['id_str'];
$text = $this->_formatURLs($result['text']);
$time = $this->_niceTime($result['created_at']);
$html .= '<div class="tweet">' . "\n";
$html .= '<div class="profile">' . "\n";
$html .= '<img src="' . $profileImage . '" alt="" />' . "\n";
$html .= '<a href="http://www.twitter.com/' . $from . '/status/' . $id . '">' .
$from . '</a>' . "\n";
$html .= '</div>' . "\n";
$html .= '<p>' . $text . '</p>' . "\n";
$html .= '<div class="time">' . $time . '</div>' . "\n";
$html .= '</div>';
}
return $html;
}
} |
Possiamo usarla in questo modo:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 |
<?php
require_once('TwitterSearch.php');
$twitterSearch = new TwitterSearch('gabriele romanato');
?>
<!DOCTYPE html>
<html>
<head>
<title>...</title>
</head>
<body>
<div id="twitter-search">
<h2>Su Gabriele Romanato</h2>
<?php echo $twitterSearch->render(); ?>
</div>
</body>
</html> |
Ecco un video che la mostra in azione:
Nel download è compreso un file per l’uso e la documentazione.

Nessun commento. Aggiungine uno!
I commenti sono chiusi.