2014-12-03 81 views
0

我試圖顯示來自一個特定用戶的推文,但只有包含特定哈希標籤的推文。我已經能夠以某種方式獲得它,但無法弄清楚如何從兩者中獲得它(如果甚至可能的話)。可能利用Twitter 1.1 API從某個特定用戶獲取特定主題標籤的推文?

這裏是我的代碼,到目前爲止其拉都來自特定用戶的tweet:

<?php 
require_once("twitteroauth.php"); 

$twitteruser = "xxxxxxxx"; 
$notweets = 5; 
$consumerkey = "xxxxxxxxxxx"; 
$consumersecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$accesstoken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$accesstokensecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { 
    $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); 
    return $connection; 
} 

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); 

$tweets = $connection->get("https://api.twitter.com/1.1/statuses/user_timeline.json?screen_name=".$twitteruser."&count=".$notweets); 

echo json_encode($tweets); 
?> 

有什麼辦法來修改這個代碼,以只從具有特定主題標籤這個單個用戶拉鳴叫?

回答

1

除了使用user_timeline API網址之外,您還可以使用「搜索推文」網址,並將Twitter用戶的句柄以及標籤一起放入搜索查詢中。這樣的事情:

<?php 
require_once("twitteroauth.php"); 

$twitteruser = "xxxxxxxx"; 
$hashtag = "xxxx"; //put the hashtag you want to search for here 
$notweets = 5; 
$consumerkey = "xxxxxxxxxxx"; 
$consumersecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$accesstoken = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 
$accesstokensecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxx"; 

function getConnectionWithAccessToken($cons_key, $cons_secret, $oauth_token, $oauth_token_secret) { 
    $connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_token_secret); 
    return $connection; 
} 

$connection = getConnectionWithAccessToken($consumerkey, $consumersecret, $accesstoken, $accesstokensecret); 

$tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=%40".$twitteruser."%20%23".$hashtag."&count=".$notweets); 

echo json_encode($tweets); 
?> 
+0

謝謝你,這很好。 – user13286 2014-12-03 22:42:09

相關問題