momo
momo

Reputation: 1

Using geocode API in twitter search

My code works perfectly fine. It has a form where users have to enter a keyword and it will give them a Twitter search results for that keyword. How can I add the user's IP address or location in the results using geocode?

This is my code:

<?php
include "twitteroauth.php";?>
<?php
$consumer = "...";
$consumersecret = "...";
$accesstoken = "...";
$accesstokensecret = "...";

$twitter = new TwitterOAuth($consumer, $consumersecret, $accesstoken, $accesstokensecret);



?>
<html> 

<head>
<meta charset="UTF-8" />
<title>twitter search</title>
</head> 

<body>
<form action="" method="post">
    <label> Search: <input type="text" name ="keyword"/></label>
</form>

<?php 
  if (isset($_POST['keyword'])){
      $tweets = $twitter->get('https://api.twitter.com/1.1/search/tweets.json?q='.$_POST['keyword'].'&result_type=recent&count=50');
      foreach($tweets as $tweet){
           foreach($tweet as $t){
              echo '<p> <img src="'.$t->user->profile_image_url.'"/><p>Tweet:&nbsp'.$t->text.'<br>';
          }
      }
  }


?>
</body>

</html>

Upvotes: 0

Views: 618

Answers (1)

Michael Nelles
Michael Nelles

Reputation: 5992

Here is what has worked for me (the assumption here is that your included file is the same)

require_once 'lib/twitteroauth.php';

define('CONSUMER_KEY', 'your_consumer_key');
define('CONSUMER_SECRET', 'your_consumer_secret');
define('ACCESS_TOKEN', 'your_access_token');
define('ACCESS_TOKEN_SECRET', 'your_access_token_secret');

function search(array $query)
{
  $toa = new TwitterOAuth(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_TOKEN_SECRET);
  return $toa->get('search/tweets', $query);
}

$query = array(
  "q" => "hipster",
  "count" => 20,
  "geocode" => "37.781157,-122.398720,1mi" // San Fran, Cali, USA
);
$results = search($query);

foreach ($results->statuses as $result) {
  echo $result->user->screen_name . ": " . $result->text . "\n";
}

Upvotes: 1

Related Questions