/** * Returns recent tweets based on the location provided * * * @param $location name of the location to search * @param int $tweetCount default is 15.Number of tweets to return * @param null|float $lat * @param null|float $long * @param int $tweetSearchRadiusKM in KM, default is 50km, search tweets within radius * @return array */ public function getLocationBasedTweets($location, $tweetCount = 15, $lat = null, $long = null, $tweetSearchRadiusKM = 50) { $params = array( 'q' => $location . ' OR #' . $location, 'count' => $tweetCount, 'exclude_replies' => true, 'include_entities' => false, 'result_type' => 'recent' ); if ($lat !== null && $long !== null) { $params['geocode'] = "$lat,$long,{$tweetSearchRadiusKM}km"; } $response = $this->auth->get('search/tweets', $params); //print_r($this->auth->getHeaders()); //print_r($response); $tweetsArray = array(); foreach ($response['statuses'] as $key => $tweet) { /** * tweet may not contain coordinates whereas if a tweet is a retweet * retweeted_status may contain coordinates which makes tweet to appear in result */ if (isset($tweet['geo']['coordinates'][0]) && isset($tweet['geo']['coordinates'][1])) { $t = array(); $t['id'] = $tweet['id_str']; $t['text'] = $tweet['text']; $t['favorite_count'] = $tweet['favorite_count']; $t['retweet_count '] = $tweet['retweet_count']; $t['created_at'] = $tweet['created_at']; $t['source'] = $tweet['source']; $t['user_id'] = $tweet['user']['id']; $t['user_name'] = $tweet['user']['name']; $t['user_screen_name'] = $tweet['user']['screen_name']; $t['user_profile_image'] = $tweet['user']['profile_image_url']; $t['latitude'] = $tweet['coordinates']['coordinates'][1]; $t['longitude'] = $tweet['coordinates']['coordinates'][0]; $tweetsArray[] = $t; } } return $tweetsArray; }
<?php require_once __DIR__ . '/../vendor/autoload.php'; use TwitterOAuth\Auth\ApplicationOnlyAuth; use TwitterOAuth\Serializer\ArraySerializer; $credentials = ['consumer_key' => "q76Uz7tqlYNMRkjc2kBsAFsv5", 'consumer_secret' => "j1nDGT3uRRJQKtLy3dyjPjCuBUexXKFZc1vqPi2lY7B6Gx7Xwd"]; $app = new Silex\Application(); $app['debug'] = true; $app->register(new Silex\Provider\TwigServiceProvider(), array('twig.path' => __DIR__ . '/../')); $app->get('/', function () use($app) { return $app['twig']->render('index.twig'); }); $app->get('/get-tweet/{username}', function ($username) use($app, $credentials) { $auth = new ApplicationOnlyAuth($credentials, new ArraySerializer()); $params = array('screen_name' => $username, 'count' => 50, 'exclude_replies' => true); $tweets = $auth->get('statuses/user_timeline', $params); //echo '<pre>'; print_r($auth->getHeaders()); echo '</pre>'; //echo '<pre>'; print_r($response); echo '</pre><hr />'; return makeTweet($tweets); }); function makeTweet($tweets) { $commonWords = getCommonWords($tweets); $sentence = createSentence($commonWords); $sentence = $sentence . " " . getRandomHashtag(); return json_encode($sentence); } function getCommonWords($tweets) { $words = getWordCounts($tweets); $words = removeStupidWords($words);
use TwitterOAuth\Serializer\ArraySerializer; date_default_timezone_set('UTC'); /** * Array with the OAuth tokens provided by Twitter * - consumer_key Twitter API key * - consumer_secret Twitter API secret */ $credentials = array('consumer_key' => 'xvz1evFS4wEEPTGEFPHBog', 'consumer_secret' => 'L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg'); /** * Instantiate ApplicationOnly * * For different output formats you can set one of available serializers * (Array, Json, Object, Text or a custom one) */ $serializer = new ArraySerializer(); $auth = new ApplicationOnlyAuth($credentials, $serializer); /** * If you need to store Bearer Token you can get it like this: */ $bearerToken = $auth->getBearerToken(); echo 'Bearer Token: ' . $bearerToken . '<hr />'; /** * If you have a stored Bearer Token you can use it like this: * -- Uncomment To Test -- */ //$bearerToken = 'AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA/AAAAAAAAAAAAAAAAAAAA=AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA'; //$auth->setBearerToken($bearerToken); /** * Returns a collection of the most recent Tweets posted by the user * https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline */
$('pre.array') .addClass('closed') .click(function(){ $(this).toggleClass('closed'); }); } </script> <style> pre{overflow:hidden;} .strip{margin-left:5px;width:90%;vertical-align:top;display:inline-block;word-wrap:break-word;} .closed{height:84px;cursor:pointer;} </style> <?php $credentials = array('consumer_key' => 'xvz1evFS4wEEPTGEFPHBog', 'consumer_secret' => 'L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg'); $auth = new ApplicationOnlyAuth($credentials, new ArraySerializer()); // ==== ==== ==== // $params = array('screen_name' => 'ricard0per', 'count' => 3, 'exclude_replies' => true); $response = $auth->get('statuses/user_timeline', $params); echo '<strong>statuses/user_timeline</strong><br />'; echo '<pre class="array">'; print_r($auth->getHeaders()); echo '</pre>'; echo '<pre class="array">'; print_r($response); echo '</pre><hr />'; // ==== ==== ==== // $params = array('q' => '#php', 'count' => 3); $response = $auth->get('search/tweets', $params); echo '<strong>search/tweets</strong><br />'; echo '<pre class="array">';
*/ use TwitterOAuth\Serializer\ArraySerializer; date_default_timezone_set('UTC'); /** * Array with the OAuth tokens provided by Twitter * - consumer_key Twitter API key * - consumer_secret Twitter API secret */ $credentials = array('consumer_key' => 'xvz1evFS4wEEPTGEFPHBog', 'consumer_secret' => 'L8qq9PZyRg6ieKGEKhZolGC0vJWLw8iEJ88DRdyOg'); /** * Instantiate ApplicationOnly * * For different output formats you can set one of available serializers * (Array, Json, Object, Text or a custom one) */ $auth = new ApplicationOnlyAuth($credentials, new ArraySerializer()); /** * Returns a collection of the most recent Tweets posted by the user * https://dev.twitter.com/docs/api/1.1/get/statuses/user_timeline */ $params = array('screen_name' => 'ricard0per', 'count' => 3, 'exclude_replies' => true); /** * Send a GET call with set parameters */ $response = $auth->get('statuses/user_timeline', $params); echo '<pre>'; print_r($auth->getHeaders()); echo '</pre>'; echo '<pre>'; print_r($response); echo '</pre><hr />';