I have a simple search bar, user types in a search terms and tweets that match the search terms should show up. However, it only works for single word searches.
<div id="search">
<form action="" method="get">
<label>
Search:
<input type="text" name="search_box" id="search_box" />
<input type="submit" name="submit" id="submit" value="Search" />
</label>
</form>
</div>
<?php
$q=$_GET['search_box'];
$search = "http://search.twitter.com/search.atom?q=".$q."";
$tw = curl_init();
curl_setopt($tw, CURLOPT_URL, $search);
curl_setopt($tw, CURLOPT_RETURNTRANSFER, TRUE);
$twi = curl_exec($tw);
$search_res = new SimpleXMLElement($twi);
echo "<h3>Twitter search results for '".$q."'</h3>";
foreach ($search_res->entry as $twit1) {
$description = $twit1->content;
$message = $row['content'];
echo "<div class='text'>".$description."</div><br><br>";
}
curl_close($tw);
?>
If you’re searching with quotation marks for exact phrase matches then you need to escape these quotes before constructing your query string. With the current code:
the input “test phrase” will result in this expression (which will probably cause a parse error):
"http://search.twitter.com/search.atom?q="test phrase"";Instead, try urlencoding the user input before including it in the search string:
Which should result in the value:
http://search.twitter.com/search.atom?q=%22test%20phrase%22You can check that this works by entering the URL in your browser.