-
Notifications
You must be signed in to change notification settings - Fork 0
/
tweets.php
68 lines (50 loc) · 1.81 KB
/
tweets.php
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
<?php
/*
* @
*/
$searchTerm = "lebron james"; // original search term.
$keywords = explode (' ',$searchTerm);
$query='';
for($i = 0; $i < count ( $keywords ); $i ++) {
if ($keywords [$i] != '') {
$query.= $keywords[$i] . '%20'; // build a query
}
}
$finalQuery = rtrim ( $query, '%20' ); // remove the last %20 from the above query
$request = 'http://search.twitter.com/search.json?q=' . $finalQuery . '&rpp=100&include_entities=true'; // the
// final
// request
$st = curl_init ( $request ); // initialize a crul request
curl_setopt ( $st, CURLOPT_RETURNTRANSFER, 1 );
$response = curl_exec ( $st );
$results = json_decode ( $response, 1 );
$list = array ();
/*
* @ store the decoded data into a new array
*/
foreach ( $results ['results'] as $result )
{
$list[] = array (
'user' => $result ['from_user'],
'text' => $result ['text'],
'date' => date("l M j \- g:ia",strtotime($result ['created_at']))
);
}
$m = new Mongo (); // instantiate a new mongo object
$db = $m->selectDB ('tweets');
$db->createCollection('tweets_collection'); // select a database, the new database is created
// on the fly
$cursor = $db->tweets_collection->find();
$cursor->sort ( array (
'date' => 1
) );
$cursor->limit(20)->batchSize(20);
$result = $cursor->getNext();
echo '<h2>The 20 tweets for search term:' . $searchTerm . 'is as following</h2><br>';
echo '<hr><table>';
echo '<tr><th>User</th><th>Text</th><th>Date</th></tr>';
for($i=0;$i<20;$i++)
{
echo '<tr><td>' . $result[$i]['user'] . '</td><td>' . $result[$i]['text'] . '</td><td>' . $result[$i]['date'] . '</td></tr>';
}
echo '</table>';