Passing a GET value from HTML form to Twitter API

2019-03-02 09:06发布

I'm only new and learning so thanks for any advice you can give :) I'm making a really simple tool to retrieve specific data from a Twitter API 1.1 GET request for a school project. I'm using J7mbo's TwitterAPIExchange.php and format. I created an HTML form to pass data to the array and can POST requests to the API without a problem. I can also successfully print specific data from a GET request to the API when I enter the variables into the $getfield in the PHP, but when I try and use a form to send data for GET requests I get the following errors:

Notice: Undefined offset: 1 in /var/www/exp/TwitterAPIExchange.php on line 158

Warning: Invalid argument supplied for foreach() in /var/www/exp/screen1.php on line 31

Here is the code:

    ini_set('display_errors', 1); 
    if(!empty($_GET ['screen_name'])) {
    require_once('TwitterAPIExchange.php');


    $settings = array(
    'oauth_access_token' => "MY_KEY_HERE",
    'oauth_access_token_secret' => "MY_KEY_HERE",
    'consumer_key' => "MY_KEY_HERE",
    'consumer_secret' => "MY_KEY_HERE",
    );

    /** URL for REST request, see: https://dev.twitter.com/docs/api/1.1/ **/
    $url = 'https://api.twitter.com/1.1/friends/list.json';
    $getfield = $_GET['screen_name'];
    $requestMethod = 'GET';

    /** Note: Set the GET field BEFORE calling buildOauth(); **/
    $twitter = new TwitterAPIExchange($settings);
    $response = $twitter->setGetfield($getfield)
                ->buildOauth($url, $requestMethod)
                ->performRequest();

    ##This prints out the fields I am interested in##

    $arrResults = json_decode($response,true);

    foreach ($arrResults['users'] as $arrSearchResult) {
$strTweet = $arrSearchResult['created_at'] ;
$strTweet1 = $arrSearchResult['screen_name'] ;
    print_r("<div class='tweet'>$strTweet $strTweet1</div>");
}       
    }

    ?>
    <h3>Enter the name here</h3>
    <form action="" method="GET">
    <input type="text" name="screen_name" /><br />
    <input type="submit" />
    </form>

So, I'm thinking that the request is being malformed before OAuth, because I get the same errors even when I don't have access keys in the OAuth array.

In POST requests to the API, form data is passed directly into the API call array, but the GET requests are built differently, and I'm not seeing how to pass form data to it. This will work fine:

    $getfield = '?screen_name=TWITTER_SCREEN_NAME';

It's when I try $getfield = $_GET['screen_name']; That it goes wrong. Thanks for your time, sorry if this noob stuff is annoying :3

1条回答
来,给爷笑一个
2楼-- · 2019-03-02 09:32

If $getfield = '?screen_name=TWITTER_SCREEN_NAME'; works without form data, then you should just need to change the line

$getfield = $_GET['screen_name'];

to

$getfield = "?screen_name=".$_GET['screen_name'];

Assuming you're only passing through the @handle through in the form

Also, (this is not pertinent to your issue) the line if(!empty($_GET ['screen_na... shouldn't have a space inbetween the $_GET and the [.

查看更多
登录 后发表回答