-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.php
179 lines (142 loc) · 3.8 KB
/
index.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
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
<html>
<head>
<title>Learn PHP & MongoDB</title>
<link rel="stylesheet" href="http://css.happyherbivore.com/960test/960.css" />
<style>
p.tweets {
overflow: hidden;
border-bottom: 3px solid orange;
background: #666;
padding: 5px;
border-radius: 3px;
color: white;
}
p.tweets span {float:right;}
p.db-docs {
overflow: hidden;
border-bottom: 3px solid orange;
background: #000;
padding: 5px;
border-radius: 3px;
color: white;
}
p.db-docs span {float:right;}
</style>
</head>
<body>
<div class="container_12">
<div class="grid_6">
<h2>MongoDB records</h2>
<?php
ini_set ('display_errors', 1);
error_reporting (E_ALL | E_STRICT);
?>
<?php
if ($_POST) {
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// Get twitter objects from static file
$lines = file_get_contents('stream.json');
$grp = json_decode($lines);
// get a single tweet from a json doc
$tweetdoc = $grp[$_POST['tweetitem']];
$collection->insert($tweetdoc);
$conn->close();
} catch (MongoConectionException$e) {
die('Error connecting to MongoDB server');
} catch (MongoException$e) {
die('Error: ' .$e->getMessage());
}
}
?>
<?php
try {
// open connection to MongoDB server
$conn = new Mongo('localhost');
// access database
$db = $conn->test;
// access collection
$collection = $db->items;
// execute query to retrieve all documents
$cursor = $collection->find();
foreach ($cursor as $obj) {
echo "<p class='db-docs'><span>" .$obj['user']['screen_name'];
echo "<img src='" .$obj['user']['profile_image_url'] ."'></span>";
echo $obj['text'] ."</p>";
}
echo $cursor -> count() .' document(s) found. <br>';
$conn->close();
} catch (MongoConectionException$e) {
die('Error connecting to MongoDB server');
} catch (MongoException$e) {
die('Error: ' .$e->getMessage());
}
?>
</div>
<div class="grid_6">
<?php
// Great starting reference for this example.
// http://webhole.net/2009/08/31/how-to-read-json-data-with-php/
// Make a string object
$lines = file_get_contents('stream.json');
// Make an object from a json string
$grp = json_decode($lines);
/* type debug */
//echo "Type of \$lines: " .gettype($lines) ."<br>";
//echo "Type of \$grp: " .gettype($grp) ."<br>";
//echo "type of \$string: " .gettype($string) ."<br><br>";
?>
<h2>Twitter Stream for <?php echo $grp[0]->user->screen_name; ?></h2>
<!--
<p>
<h4>Get twitter stream by username</h4>
<form action="." method="POST">
<input name="input" type="text">
<input type="submit">
</form>
</p>
-->
<?php
// Display Twitter Stream for a based on USERNAME
foreach ($grp as $key => $json) {
echo "<p class='tweets'><span>" .$json->user->screen_name;
echo "<img src='" .$json->user->profile_image_url ."'></span>";
echo $json->text;
echo "<form action='.' method=POST>";
echo "<input name='tweetitem' value='$key' type='hidden'>";
echo "<input type='submit' value='<-- Add to Database'></form></p>";
}
// source: http://de.php.net/json_last_error
switch (json_last_error()) {
case JSON_ERROR_NONE:
//echo ' - No errors';
break;
case JSON_ERROR_DEPTH:
echo ' - Maximum stack depth exceeded';
break;
case JSON_ERROR_STATE_MISMATCH:
echo ' - Underflow or the modes mismatch';
break;
case JSON_ERROR_CTRL_CHAR:
echo ' - Unexpected control character found';
break;
case JSON_ERROR_SYNTAX:
echo ' - Syntax error, malformed JSON';
break;
case JSON_ERROR_UTF8:
echo ' - Malformed UTF-8 characters, possibly incorrectly encoded';
break;
default:
echo ' - Unknown error';
break;
}
?>
</div>
</div> <!-- end Container_12 -->
</body>
</html>