2017-02-24 56 views
0

我的代碼工作得很好。它有一種形式,用戶必須輸入一個關鍵字,併爲他們提供該關鍵字的Twitter搜索結果。 如何使用地理編碼在結果中添加用戶的IP地址或位置?在twitter搜索中使用地理編碼API

這是我的代碼:

<?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> 

回答

1

這裏是爲我工作(這裏的假設是,你的包含文件是一樣的)

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"; 
}