如何使api查询动态化

New to php and currently stuck How do I get the user input from my html to pass to my api query? Instead of having the query hardcoded, I want the user input to be set as the query for the api

<?php
//keys

$CONSUMER_KEY = 'w';
$CONSUMER_SECRET = 'w';
$ACCESS_KEY = 'w';
$ACCESS_SECRET = 'w';

//include lib
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//connect to the api
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_KEY, $ACCESS_SECRET);
$content = $connection->get("account/verify_credentials");


// get tweets
// $tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=trump");
$tweets = $connection->get("search/tweets", ["q" => "trump"]);


?>

<!DOCTYPE html>
    <head>
        <meta charset="utf-8">
        <meta http-equiv="X-UA-Compatible" content="IE=edge">
        <title>Twitter Api Search</title>
        <meta name="description" content="">
        <meta name="viewport" content="width=device-width, initial-scale=1">
        <link rel="stylesheet" href="">
    </head>
    <body>
            <form action="" method="get">
                            <input name="keyword" type="text" placeholder="Search Tweets">

            </form>
            <?php foreach ($tweets->statuses as $key => $tweet) { ?>
            <img src="<?=$tweet->user->profile_image_url;?>" /><?=$tweet->text; ?><br>
        <?php } ?>
    </body>
</html>

Things we need to do:

  1. Put all your API code into one file, call it api/twitter.php.
  2. Put all of your HTML code into one file, call it pages/twitter-search.php.
  3. Change your HTML form in the page to reflect the API file path.

api/twitter.php

//keys

$CONSUMER_KEY = 'w';
$CONSUMER_SECRET = 'w';
$ACCESS_KEY = 'w';
$ACCESS_SECRET = 'w';

//include lib
require "twitteroauth/autoload.php";
use Abraham\TwitterOAuth\TwitterOAuth;

//connect to the api
$connection = new TwitterOAuth($CONSUMER_KEY, $CONSUMER_SECRET, $ACCESS_KEY, $ACCESS_SECRET);
$content = $connection->get("account/verify_credentials");


// get tweets
// $tweets = $connection->get("https://api.twitter.com/1.1/search/tweets.json?q=trump");


// because in our form on our page we use a text input with the name 'keyword'
$tweets = $connection->get("search/tweets", ["q" => $_GET['keyword']]);


?>

pages/twitter.php

<?php
echo '<!DOCTYPE html>
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <title>Twitter Api Search</title>
    <meta name="description" content="">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <link rel="stylesheet" href="">
</head>
<body>
<form action="api/twitter.php" method="get">
    <input name="keyword" type="text" placeholder="Search Tweets">

</form>';

foreach ($tweets->statuses as $key => $tweet) {
    echo '<img src="'.$tweet->user->profile_image_url.'" />'.$tweet->text.'<br>';
}

echo '
</body>
</html>';
?>

Notice inside the form, our action attribute changed to 'api/twitter.php' which reflects the new destination of your API file. The text box input has a name of 'keyword' which is what the API file looks for when its ran.