将 GET 值从 HTML 表单传递到 Twitter API 1.1 PHP [英] Passing a GET value from HTML form to Twitter API 1.1 PHP

查看:19
本文介绍了将 GET 值从 HTML 表单传递到 Twitter API 1.1 PHP的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我只是新手,所以感谢您提供任何建议:) 我正在制作一个非常简单的工具,用于从 Twitter API 1.1 GET 请求中检索学校项目的特定数据.我正在使用 J7mbo 的 TwitterAPIExchange.php 和格式.我创建了一个 HTML 表单来将数据传递给数组,并且可以毫无问题地向 API 发送请求.当我将变量输入 PHP 中的 $getfield 时,我还可以成功地将特定数据从 GET 请求打印到 API,但是当我尝试使用表单为 GET 请求发送数据时,我收到以下错误:

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:

注意:未定义偏移量:158 行/var/www/exp/TwitterAPIExchange.php 中的 1

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

警告:为第 31 行/var/www/exp/screen1.php 中的 foreach() 提供的参数无效

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

代码如下:

    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>

所以,我认为请求在 OAuth 之前格式错误,因为即使我在 OAuth 数组中没有访问密钥,我也会遇到相同的错误.

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.

在对 API 的 POST 请求中,表单数据直接传递到 API 调用数组中,但 GET 请求的构建方式不同,我没有看到如何将表单数据传递给它.这会正常工作:

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';

当我尝试 $getfield = $_GET['screen_name'];它出错了.感谢您的时间,对不起,如果这个菜鸟的东西很烦人:3

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

推荐答案

如果 $getfield = '?screen_name=TWITTER_SCREEN_NAME'; 在没有表单数据的情况下工作,那么您只需要更改行

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

$getfield = $_GET['screen_name'];

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

假设你只是通过表单中的@handle

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

此外,(这与您的问题无关)if(!empty($_GET ['screen_na...] 行之间不应有空格$_GET[.

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

这篇关于将 GET 值从 HTML 表单传递到 Twitter API 1.1 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆