我正在尝试显示来自某个特定用户的推文,但只显示包含特定主题标签的推文。我已经能够以某种方式工作,但无法弄清楚如何从两者中获取它(如果它甚至可能)。
到目前为止,这是我的代码,它从特定用户那里获取所有推文:
<?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);
?>
有没有办法修改此代码,以便只从具有特定主题标签的单个用户中提取推文?
答案 0 :(得分: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);
?>