dongzhong5573 2011-02-18 10:36
浏览 24
已采纳

PHP在没有OAuth的情况下获取Twitter API JSON文件内容(几乎拥有它)

Hey guys, I have this script working fine with OAuth, but I accidentally nuked my 350 API hits with a stupid while statement :( I'm trying to get data from the Twitter API without OAuth, I can't figure it out (still pretty new), heres what I have

<html>
<body>
<center>
<hr />
<br />
<table border="1">
<tr><td>ScreenName</td><td>Followed back?</td></tr>
<?php
//twitter oauth deets
$consumerKey    = 'x';
$consumerSecret = 'x';
$oAuthToken     = 'x';
$oAuthSecret    = 'x';
// Create Twitter API objsect 
require_once("twitteroauth.php");
$oauth = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
//get home timeline tweets and it is stored as an array
$youfollow = $oauth->get('http://api.twitter.com/1/friends/ids.json?screen_name=lccountdown');
$i = 0;
//start loop to print our results cutely in a table
while ($i <= 20){
$youfollowid = $youfollow[$i];
$resolve = "http://api.twitter.com/1/friendships/exists.json?user_a=".$youfollow[$i]."&user_b=jwhelton";
$followbacktest = $oauth->get($resolve);
//$homedate= $hometimeline[$i]->created_at;
//$homescreenname = $hometimeline[$i]->user->screen_name;
echo "<tr><td>".$youfollowid."</td><td>".$followbacktest."</td></tr>";
$i++;
}
?>
</table>
</center>
</body>
</html>

Neither of the two Twitter functions require authentication, so how can I get the same results? Thanks guys, Dex

  • 写回答

1条回答 默认 最新

  • douyazi1129 2011-02-19 02:16
    关注

    You aren't calling making the TwitterOAuth calls correctly. Don't use the full URL just the method path and pass parameters in a hash array.

    You probably also don't want to use a while loop when a foreach will work just as well.

    <?php
    //twitter oauth deets
    $consumerKey    = 'x';
    $consumerSecret = 'x';
    $oAuthToken     = 'x';
    $oAuthSecret    = 'x';
    // Create Twitter API objsect 
    require_once("twitteroauth.php");
    
    $oauth = new TwitterOAuth($consumerKey, $consumerSecret, $oAuthToken, $oAuthSecret);
    
    //get home timeline tweets and it is stored as an array
    $youfollow = $oauth->get('friends/ids', array('screen_name' => 'lccountdown'));
    $i = 0;
    
    //start loop to print our results cutely in a table
    foreach($youfollow as $id){
      $followbacktest = $oauth->get('friendships/exists', array('user_a' => $id, 'user_b' => 'jwhelton'));
      echo "<tr><td>".$id."</td><td>".$followbacktest."</td></tr>";
    }
    

    I have not directly tested this but it should work. You should also consider moving to fuller featured GET friendships/show method.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。
  • ¥60 许可证msc licensing软件报错显示已有相同版本软件,但是下一步显示无法读取日志目录。
  • ¥15 Attention is all you need 的代码运行
  • ¥15 一个服务器已经有一个系统了如果用usb再装一个系统,原来的系统会被覆盖掉吗
  • ¥15 使用esm_msa1_t12_100M_UR50S蛋白质语言模型进行零样本预测时,终端显示出了sequence handled的进度条,但是并不出结果就自动终止回到命令提示行了是怎么回事:
  • ¥15 前置放大电路与功率放大电路相连放大倍数出现问题
  • ¥30 关于<main>标签页面跳转的问题
  • ¥80 部署运行web自动化项目
  • ¥15 腾讯云如何建立同一个项目中物模型之间的联系