-
Notifications
You must be signed in to change notification settings - Fork 12
/
TwitterSearch.php
381 lines (324 loc) · 8.69 KB
/
TwitterSearch.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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
<?
/**
* Wrapper class around the Twitter Search API for PHP
* Based on the class originally developed by David Billingham
* and accessible at http://twitter.slawcup.com/twitter.class.phps
* @author Ryan Faerman <[email protected]>
* @version 0.2
* @package PHPTwitterSearch
*/
class TwitterSearch {
/**
* Whether to return tweet metadata. Default is false.
* @var boolean
*/
var $include_entities = false;
/**
* Type of results to recieve; mixed, popular or recent.
* @var string
*/
var $result_type;
/**
* Can be set to JSON (requires PHP 5.2 or the json pecl module) or XML - json|xml
* @var string
*/
var $type = 'json';
/**
* It is unclear if Twitter header preferences are standardized, but I would suggest using them.
* More discussion at http://tinyurl.com/3xtx66
* @var array
*/
var $headers=array('X-Twitter-Client: PHPTwitterSearch','X-Twitter-Client-Version: 0.1','X-Twitter-Client-URL: http://ryanfaerman.com/twittersearch');
/**
* Recommend setting a user-agent so Twitter knows how to contact you inc case of abuse. Include your email
* @var string
*/
var $user_agent='';
/**
* @var string
*/
var $query='';
/**
* @var array
*/
var $responseInfo=array();
/**
* Use an ISO language code. en, de...
* @var string
*/
var $lang;
/**
* Language of query (only ja is currently effective).
* @var string
*/
var $locale;
/**
* The number of tweets to return per page, max 100
* @var int
*/
var $rpp;
/**
* The page number to return, up to a max of roughly 1500 results
* @var int
*/
var $page;
/**
* Return tweets with a status id greater than the since value
* @var int
*/
var $since;
/**
* Return tweets with a status id smaller than the max value
* @var int
*/
var $max;
/**
* Returns tweets by users located within a given radius of the given latitude/longitude, where the user's location is taken from their Twitter profile. The parameter value is specified by "latitide,longitude,radius", where radius units must be specified as either "mi" (miles) or "km" (kilometers)
* @var string
*/
var $geocode;
/**
* When "true", adds "<user>:" to the beginning of the tweet. This is useful for readers that do not display Atom's author field. The default is "false"
* @var boolean
*/
var $show_user = false;
/**
* @param string $query optional
*/
function TwitterSearch($query=false) {
$this->query = $query;
}
/**
* Find tweets from a user
* @param string $user required
* @return object
*/
function from($user) {
$this->query .= ' from:'.str_replace('@', '', $user);
return $this;
}
/**
* Find tweets to a user
* @param string $user required
* @return object
*/
function to($user) {
$this->query .= ' to:'.str_replace('@', '', $user);
return $this;
}
/**
* Find tweets referencing a user
* @param string $user required
* @return object
*/
function about($user) {
$this->query .= ' @'.str_replace('@', '', $user);
return $this;
}
/**
* Find tweets containing a hashtag
* @param string $user required
* @return object
*/
function with($hashtag) {
$this->query .= ' #'.str_replace('#', '', $hashtag);
return $this;
}
/**
* Find tweets containing a word
* @param string $user required
* @return object
*/
function contains($word) {
$this->query .= ' '.$word;
return $this;
}
/**
* Set include_entities to true
* @return object
*/
function include_entities() {
$this->include_entities = true;
return $this;
}
/**
* @param string $type required
* @return object
*/
function result_type($type) {
$this->result_type = $type;
return $this;
}
/**
* Set show_user to true
* @return object
*/
function show_user() {
$this->show_user = true;
return $this;
}
/**
* @param int $since_id required
* @return object
*/
function since($since_id) {
$this->since = $since_id;
return $this;
}
/**
* @param int $max_id required
* @return object
*/
function max($max_id) {
$this->max = $max_id;
return $this;
}
/**
* @param int $language required
* @return object
*/
function lang($language) {
$this->lang = $language;
return $this;
}
/**
* @param string $loc required
* @return object
*/
function locale($loc) {
$this->locale = $loc;
return $this;
}
/**
* @param int $n required
* @return object
*/
function rpp($n) {
$this->rpp = $n;
return $this;
}
/**
* @param int $n required
* @return object
*/
function page($n) {
$this->page = $n;
return $this;
}
/**
* @param float $lat required. lattitude
* @param float $long required. longitude
* @param int $radius required.
* @param string optional. mi|km
* @return object
*/
function geocode($lat, $long, $radius, $units='mi') {
$this->geocode = $lat.','.$long.','.$radius.$units;
return $this;
}
/**
* Build and perform the query, return the results.
* @param $reset_query boolean optional.
* @return object
*/
function results($reset_query=true) {
$request = 'http://search.twitter.com/search.'.$this->type;
$request .= '?q='.urlencode($this->query);
if(isset($this->rpp)) {
$request .= '&rpp='.$this->rpp;
}
if(isset($this->page)) {
$request .= '&page='.$this->page;
}
if(isset($this->lang)) {
$request .= '&lang='.$this->lang;
}
if(isset($this->locale)) {
$request .= '&locale='.$this->locale;
}
if(isset($this->since)) {
$request .= '&since_id='.$this->since;
}
if(isset($this->max)) {
$request .= '&max_id='.$this->max;
}
if($this->show_user) {
$request .= '&show_user=true';
}
if(isset($this->geocode)) {
$request .= '&geocode='.$this->geocode;
}
if(isset($this->result_type)) {
$request .= '&result_type='.$this->result_type;
}
if($this->include_entities) {
$request .= '&include_entities=true';
}
if($reset_query) {
$this->query = '';
}
return $this->objectify($this->process($request))->results;
}
/**
* Returns the top ten queries that are currently trending on Twitter.
* @return object
*/
function trends() {
$request = 'http://search.twitter.com/trends.json';
return $this->objectify($this->process($request));
}
/**
* Internal function where all the juicy curl fun takes place
* this should not be called by anything external unless you are
* doing something else completely then knock youself out.
* @access private
* @param string $url Required. API URL to request
* @param string $postargs Optional. Urlencoded query string to append to the $url
*/
function process($url, $postargs=false) {
$ch = curl_init($url);
if($postargs !== false) {
curl_setopt ($ch, CURLOPT_POST, true);
curl_setopt ($ch, CURLOPT_POSTFIELDS, $postargs);
}
curl_setopt($ch, CURLOPT_VERBOSE, 1);
curl_setopt($ch, CURLOPT_NOBODY, 0);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_USERAGENT, $this->user_agent);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION,1);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $this->headers);
$response = curl_exec($ch);
$this->responseInfo=curl_getinfo($ch);
curl_close($ch);
if( intval( $this->responseInfo['http_code'] ) == 200 )
return $response;
else
return false;
}
/**
* Function to prepare data for return to client
* @access private
* @param string $data
*/
function objectify($data) {
if( $this->type == 'json' )
return (object) json_decode($data);
else if( $this->type == 'xml' ) {
if( function_exists('simplexml_load_string') ) {
$obj = simplexml_load_string( $data );
$statuses = array();
foreach( $obj->status as $status ) {
$statuses[] = $status;
}
return (object) $statuses;
}
else {
return $out;
}
}
else
return false;
}
}
?>