Skip to content

Instantly share code, notes, and snippets.

@mrosata
Created February 25, 2016 00:31
Show Gist options
  • Select an option

  • Save mrosata/075cae972f8685efde1c to your computer and use it in GitHub Desktop.

Select an option

Save mrosata/075cae972f8685efde1c to your computer and use it in GitHub Desktop.
A Twitter API Call - Allows clientside to make 3 requests which each return the next query results
<?php
ini_set('display_errors', 1);
error_reporting(E_ALL);
require '../vendor/autoload.php';
define("OAUTH_ACCESS_TOKEN", "the oauth here ya know");
define("OAUTH_ACCESS_TOKEN_SECRET", "put the secret here");
define("CONSUMER_KEY", "key key key key key key");
define("CONSUMER_SECRET", "shhhh its a secret");
/** Set access tokens here - see: https://dev.twitter.com/apps/ **/
$settings = array(
'oauth_access_token' => OAUTH_ACCESS_TOKEN,
'oauth_access_token_secret' => OAUTH_ACCESS_TOKEN_SECRET,
'consumer_key' => CONSUMER_KEY,
'consumer_secret' => CONSUMER_SECRET
);
$twitter = new TwitterAPIExchange($settings);
$today = date('Y-m-d', strtotime('-1 month'));
$lat = isset($_POST['lat']) ? number_format(floatval($_POST['lat']), 4, '.', '') : '';
$long = isset($_POST['long']) ? number_format(floatval($_POST['long']), 4, '.', '') : '';
$lastID = isset($_POST['lastID']) ? intval($_POST['lastID']) : 0;
$count = isset($_POST['count']) ? intval($_POST['count']) : 100;
$miles = 350;
if (!($session_id = session_id())) {
session_start();
}
// this is relevant to my javascript
if ($lastID === 0) {
$_SESSION['next'] = 3;
}
$remaining = (int)$_SESSION['next'];
if ($remaining > 0) {
// this is the twitter query
$url = 'https://api.twitter.com/1.1/search/tweets.json';
$params = "?q=FEMA OR hurricane OR flood OR weather OR storm OR wind OR disaster OR rain since:{$today}&geocode={$lat},{$long},{$miles}mi" . ($lastID > 0 ? "&max_id={$lastID}&count={$count}" : "&count={$count}");
$requestMethod = 'GET';
$results = $twitter->setGetfield($params)
->buildOauth($url, $requestMethod)
->performRequest();
$json_results = array(
'results' => json_decode($results),
'next' => --$remaining
);
$_SESSION['next'] = $remaining;
}
else {
$json_results = array('finished' => true);
}
echo json_encode($json_results);
exit;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment