Passing a GET value from HTML form to Twitter API 1.1 PHP

旧街凉风 提交于 2019-12-02 12:57:36

问题


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:


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 [.



来源:https://stackoverflow.com/questions/17860524/passing-a-get-value-from-html-form-to-twitter-api-1-1-php

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!