2

我有一个小应用程序,用户可以在其上在搜索框中输入特定关键字,当他们单击“搜索”时,会显示一个包含基于关键字的推文的表格。我想通过这个练习进一步实现的是,每次有人发推文时,我都能够收到基于此关键字的通知,而无需我再次搜索。请有人帮忙。

这是我的搜索代码到目前为止的样子:

 session_save_path("/tmp");
 session_start();
 ini_set('display_errors', 1);
 require_once('TwitterAPIExchange.php');

//setting the api connection

$settings = array(
'oauth_access_token' => "****",
'oauth_access_token_secret' => "****",
'consumer_key' => "****",
'consumer_secret' => "****"
);

$url ="https://api.twitter.com/1.1/search/tweets.json";
 $requestMethod = 'GET';


//displaying the search results

$twitter = new TwitterAPIExchange($settings);

$_SESSION['key'] ='';
if(isset($_GET['results']))
{
    $r_count = $_GET['results'];
}
if(isset($_GET['keyword']))
{
    print_r("<p style='padding-left:1em;color:#FFC20E;font-size:20px'>Search Results for Keyword: <span style='font-weight: bold'>".$_GET['keyword']."</span></p><br>");

$result = '?q=%23'.$_GET['keyword'].'&result_type=recent&count='.$r_count.'';
$key = $_GET['keyword'];
$result = preg_replace("/\b([a-z]*${key}[a-z]*)\b/i","<b>$1</b>",$result);
$string = $twitter ->setGetfield($result)
            ->buildOauth($url, $requestMethod);
           ->performRequest();  
$response = json_decode($string);

echo "<table border='1'>";
echo "<th>Display Name</th>";
echo "<th>handle</th>";
echo "<th>Tweet</th>";
echo "<th>Location</th>";
foreach($response->statuses as $tweet)
{
    echo "<tr>";
    //echo "<td> {$tweet->user->name} </td><td><img src={$tweet->user->profile_image_url}/>{$tweet->user->screen_name} </td><td> {$tweet->text}</td><td> {$tweet->user->location}</td>";
    echo '<td>'.$tweet->user->name.'</td><td><img  height="35" style="width:35px;display:inline-block" src="'.$tweet->user->profile_image_url.'"/><a style ="color: #0075c9" target="_blank" href="https://twitter.com/'.$tweet->user->screen_name.'">@'.$tweet->user->screen_name.'</a></td><td>'.$tweet->text.'</td><td>'.$tweet->user->location.'</td>';
    echo "</tr>";
}
echo "</table>";
}
4

0 回答 0