dsu5188 2019-08-18 18:02
浏览 306

如何使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>

  • 写回答

1条回答 默认 最新

  • drsb77336 2019-08-18 20:32
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥15 树莓派5怎么用camera module 3啊
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥15 Attention is all you need 的代码运行