我只是新手,並且非常感謝您提供的任何建議:)我正在製作一個非常簡單的工具來從Twitter API 1.1 GET中檢索特定數據申請學校項目。我正在使用J7mbo的TwitterAPIExchange.php和格式。我創建了一個HTML表單來將數據傳遞給數組,並可以向API發送請求而不會出現問題。當我將變量輸入到PHP中的$ getfield時,我也可以成功地從GET請求打印特定數據到API,但是當我嘗試使用表單發送GET請求數據時,出現以下錯誤:將GET值從HTML表單傳遞到Twitter API 1.1 PHP
注意:未定義偏移:1 /var/www/exp/TwitterAPIExchange.php上管線158
警告:在/var/www/exp/screen1.php爲的foreach()供給線的參數無效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陣列中沒有訪問密鑰,也會得到相同的錯誤。
在對API的POST請求中,表單數據直接傳遞到API調用數組中,但GET請求的構建方式不同,並且我沒有看到如何將表單數據傳遞給它。這將工作正常:
$getfield = '?screen_name=TWITTER_SCREEN_NAME';
這是當我嘗試$ getfield = $ _GET ['screen_name'];它出錯了。感謝您的時間,對不起,如果這小白東西是煩人:3
啊,我在想,$ _GET已經通過了,但事實並非如此。謝謝,它修復了Undefined Offest錯誤。我仍然得到這個: 警告:在第31行的/var/www/exp/screen1.php中爲foreach()提供的無效參數 –
如果在foreach循環之前有'print_r($ arrResults);'是否打印數組,還是空白? – Novocaine
是的它的工作原理! :)))我回到以前的保存並添加$ getfield =「?screen_name =」。$ _ GET ['screen_name']; 現在它打印正確,沒有錯誤!正如你所提到的,我也刪除了$ _GET之間的空間,那是個錯誤。 我不能非常感謝你,我很喜歡學習PHP,有時我覺得我試着去太快了哈哈,但是非常感謝你:) –