将HTML表单中的GET值传递给Twitter API 1.1 PHP [英] Passing a GET value from HTML form to Twitter API 1.1 PHP
问题描述
我只是新手,学习如此感谢您提供的任何建议:)我正在制作一个非常简单的工具,以便从Twitter API 1.1 GET请求中检索特定数据,以获得学校项目。我正在使用J7mbo的 TwitterAPIExchange.php 和格式。我创建了一个HTML表单来将数据传递给数组,并可以向API发送请求而不会出现问题。当我将变量输入到PHP中的$ getfield时,我也可以成功地将GET请求中的特定数据打印到API,但是当我尝试使用表单发送GET请求数据时,出现以下错误:
注意:未定义偏移量:1位于/var/www/exp/TwitterAPIExchange.php,第158行
警告:无效参数为foreach()在第31行的/var/www/exp/screen1.php中提供。
以下是代码:
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,
);
/ ** REST请求的URL,请参阅:https://dev.twitter.com/docs/api/1.1/ ** /
$ url ='https:// api .twitter.com / 1.1 /朋友/ list.json';
$ getfield = $ _GET ['screen_name'];
$ requestMethod ='GET';
/ **注意:在调用buildOauth()之前设置GET字段。 ** /
$ twitter = new TwitterAPIExchange($ settings);
$ response = $ twitter-> setGetfield($ getfield)
- > buildOauth($ url,$ requestMethod)
- > performRequest();
##打印出我感兴趣的字段##
$ 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>在此输入名称< / h3>
< form action =method =GET>
< input type =textname =screen_name/>< br />
< input type =submit/>
< / form>
所以,我认为请求在OAuth之前被格式化了,因为我得到了相同的错误即使我在OAuth阵列中没有访问密钥。
在API的POST请求中,表单数据直接传递到API调用数组中,但GET请求的构建方式不同,并且我没有看到如何传递形成数据。这将工作正常:
$ getfield ='?screen_name = TWITTER_SCREEN_NAME';
当我尝试$ getfield = $ _GET ['screen_name'];它出错了。感谢您的时间,对不起,如果这个noob的东西是讨厌的:3
如果 $ getfield =' ?screen_name = TWITTER_SCREEN_NAME';
没有表单数据,那么你应该只需要改变行
$ getfield = $ _GET ['screen_name'];
至
$ getfield =?screen_name =。$ _ GET ['screen_name'];
假设您只传递 @handle
通过表格
另外,(这与您的问题无关) if(!空($ _ GET ['screen_na ...
在 $ _ 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:
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
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 [
.
这篇关于将HTML表单中的GET值传递给Twitter API 1.1 PHP的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!