-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgetData.php
87 lines (69 loc) · 3.22 KB
/
getData.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
<?php
require_once('TwitterAPIExchance.php');
// ensure all variables are set
if(isset( $_POST['lat'] ) &&
isset( $_POST['long'] ) &&
isset( $_POST['radius'] )
){
// authentication settings for Twitter API
$settings = array(
'oauth_access_token' => "YOUR-KEY-HERE",
'oauth_access_token_secret' => "YOUR-KEY-HERE",
'consumer_key' => "YOUR-KEY-HERE",
'consumer_secret' => "YOUR-KEY-HERE"
);
// set variables from POST data
$lat = filter_input(INPUT_POST, 'lat', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$long = filter_input(INPUT_POST, 'long', FILTER_SANITIZE_NUMBER_FLOAT, FILTER_FLAG_ALLOW_FRACTION);
$radius = $_POST['radius'];
$radius = filter_var($radius, FILTER_SANITIZE_NUMBER_INT);
$unit = $_POST['unit'];
$radius = $radius . $unit;
// request url
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$requestMethod = 'GET';
// our specific query, set to return 100 tweets (max)
$getfield = "?geocode=$lat,$long,$radius&count=100";
$request = ($url . $getfield);
// authenticate with twitter api using library
$twitter = new TwitterAPIExchange($settings);
// make request, save response
$response = $twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest();
}else{
echo "\n\n\n<br><br>No user input provided.";
}
?>
<html>
<head>
<!-- JQuery from Google CDN -->
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.2.0/jquery.min.js"></script>
<!-- Script from Twitter to display embedded tweets -->
<script src="//platform.twitter.com/widgets.js" charset="utf-8"></script>
<!-- Mad stylish stuff in here -->
<link rel="stylesheet" href="./styles.css"/>
<title>Geo Tweets</title>
</head>
<body>
<header>
<h2>Top 10 Tweets:</h2>
</header>
<div id="tweets" class="tweets"></div>
<!-- I'm /still/ a sellout -->
<footer>
<p>By <a href="http://patrickoboyle.com">Patrick O'Boyle</a></p>
</footer>
<!-- Embedded JS with embedded PHP, oh dear...re-design coming soon(TM) -->
<script type="text/javascript">
var data = (<?php echo $response ?>).statuses;
var top10 = data.sort(function(a, b) { return a.favorite_count < b.favorite_count ? 1 : -1; }).slice(0, 10);
console.log(top10);
$.each(top10, function(data, tweet){
var profile = "https://twitter.com/" + tweet.user.screen_name + "/status/" + tweet.id_str;
var dataStuff = "<blockquote class='twitter-tweet'><p>" + tweet.text + "—" + tweet.user.name + " (" + tweet.user.screen_name + ")<a href='" + profile + "' data-datetime='" + tweet.created_at + "'></a></blockquote>";
$('#tweets').append("<blockquote class='twitter-tweet'><p>" + tweet.text + "—" + tweet.user.name + " (" + tweet.user.screen_name + ")<a href='" + profile + "' data-datetime='" + tweet.created_at + "'></a></blockquote>");
});
</script>
</body>
</html>