ian007 ian007 - 2 months ago 6
JSON Question

how can i add a textbox to twitter api script?

I have this twitter api script which pulls tweets from a specified user.

id like to know how i can change it, so that i can have a text box field instead of a fixed username.

here is the code

could i link the

else {$user = "twitterusername";}
to a text box with submit button?

Thank you



<?$url = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$requestMethod = "GET";
if (isset($_GET['user'])) {$user = $_GET['user'];} else {$user = "twitterusername";}
if (isset($_GET['count'])) {$user = $_GET['count'];} else {$count = 20;}
$getfield = "?screen_name=$user&count=$count";
$twitter = new TwitterAPIExchange($settings);
$string = json_decode($twitter->setGetfield($getfield)
->buildOauth($url, $requestMethod)
->performRequest(),$assoc = TRUE);
if($string["errors"][0]["message"] != "") {echo "<h3>Sorry, there was a problem.</h3><p>Twitter returned the following error message:</p><p><em>".$string[errors][0]["message"]."</em></p>";exit();}
foreach($string as $items)
{
echo "Tweet: ". $items['text']."<br />";
echo "retweets: ". $items['retweet_count']."<br />";
}
?>




Answer

What you have here:

if (isset($_GET['user']))  {$user = $_GET['user'];}  else {$user  = "twitterusername";}

The username is only fixed if you are not passing a GET (aka. querystring) value for user.

If you visited the page with ?user=someOtherUsername it would pull data back for that user.

So to have a form pass that value in from a text box, you could have something as simple as this in the same PHP file:

<form method="GET" action="">
    <input type="text" name="user" /><input type="submit" value="Submit" />
</form>

This is the same with your count value as well. You can control is by using another GET value with the name count.

Comments