Creating input form for Twitter API username selection

I’m working with a Twitter API script that fetches tweets from a specific user account. Right now the username is hardcoded in my script but I want to make it dynamic.

I need help creating an HTML form with an input field and submit button so users can enter any Twitter username they want to see tweets from.

Here’s my current code:

<?php
$apiUrl = "https://api.twitter.com/1.1/statuses/user_timeline.json";
$method = "GET";
if (isset($_POST['username'])) {$screenName = $_POST['username'];} else {$screenName = "defaultuser";}
if (isset($_POST['tweetLimit'])) {$limit = $_POST['tweetLimit'];} else {$limit = 15;}
$params = "?screen_name=$screenName&count=$limit";
$twitterApi = new TwitterAPIExchange($config);
$response = json_decode($twitterApi->setGetfield($params)
->buildOauth($apiUrl, $method)
->performRequest(), $assoc = TRUE);
if($response["errors"][0]["message"] != "") {
    echo "<h3>Error occurred</h3><p>API returned: ".$response[errors][0]["message"]."</p>";
    exit();
}
foreach($response as $tweet) {
    echo "Message: ". $tweet['text']."<br />";
    echo "Shares: ". $tweet['retweet_count']."<br />";
}
?>

How can I connect the else {$screenName = "defaultuser";} part to work with a text input form?

Spotted a bug in your error checking - you’re missing quotes around the array key:

echo "<p>API returned: ".$response[errors][0]["message"]."</p>";

Should be:

echo "<p>API returned: ".$response["errors"][0]["message"]."</p>";

For the form, you don’t need to change much. Your POST handling’s already there. Just add this HTML at the top:

<form method="POST">
    <input type="text" name="username" placeholder="Twitter username">
    <input type="number" name="tweetLimit" placeholder="Tweet count" value="15">
    <button type="submit">Load Tweets</button>
</form>
<hr>

One thing I’ve learned with Twitter API - always clean that username input. People love throwing in @ symbols and spaces:

if (isset($_POST['username'])) {
    $screenName = trim(str_replace('@', '', $_POST['username']));
} else {
    $screenName = "defaultuser";
}

Heads up - Twitter’s v1.1 API is deprecated. Migrate to v2 soon or you’ll hit issues.

just add a basic form before your php block. something like <form method="post"><input name="username" placeholder="twitter handle"><input type="submit" value="go"></form> works since you’ve already got the $_POST handling set up. don’t forget to sanitize the input though - people will definitely try weird stuff.

Your backend logic is already done - just add the HTML form above your PHP code:

<!DOCTYPE html>
<html>
<head>
    <title>Twitter Timeline Fetcher</title>
</head>
<body>
    <h2>Fetch Twitter Timeline</h2>
    <form method="POST">
        <label for="username">Twitter Username:</label>
        <input type="text" id="username" name="username" placeholder="@username" value="<?php echo isset($_POST['username']) ? htmlspecialchars($_POST['username']) : ''; ?>">
        
        <label for="tweetLimit">Number of tweets:</label>
        <input type="number" id="tweetLimit" name="tweetLimit" min="1" max="200" value="<?php echo isset($_POST['tweetLimit']) ? $_POST['tweetLimit'] : '15'; ?>">
        
        <button type="submit">Fetch Tweets</button>
    </form>
</body>
</html>

I’ve worked with Twitter API for years - always validate username input before making requests. Add basic validation to prevent empty submissions and strip @ symbols if users add them. Your error handling will catch invalid usernames anyway, but client-side validation saves you API calls.

Wrap your PHP code with an HTML form that POSTs to the same page - your existing PHP logic will handle everything else.

Add this before your PHP code:

<form method="POST" action="">
    <input type="text" name="username" placeholder="Enter Twitter username" required>
    <input type="number" name="tweetLimit" placeholder="Number of tweets (default: 15)" min="1" max="200">
    <input type="submit" value="Get Tweets">
</form>

Since your PHP already checks for $_POST['username'], this’ll work right away. The form creates the POST data your script needs.

That said, manual Twitter API calls get messy quick. You’ll deal with rate limits, error handling, auth refreshing, and parsing responses.

I use Latenode automation for this exact thing. Set up a webhook that takes the username, connects to Twitter automatically, handles errors, and returns clean JSON. Your HTML form just makes an AJAX call.

The automation manages auth, rate limiting, and caches results so you don’t hit API limits. Much cleaner than all that PHP complexity.

Check it out: https://latenode.com