【问题标题】:PHP getting Twitter API JSON file contents without OAuth (Almost have it)PHP 在没有 OAuth 的情况下获取 Twitter API JSON 文件内容(几乎有)
【发布时间】:2011-02-18 10:36:11
【问题描述】:

大家好, 我的这个脚本在 OAuth 上运行良好,但我不小心用一个愚蠢的 while 语句对我的 350 个 API 命中进行了核对:( 我试图在没有 OAuth 的情况下从 Twitter API 获取数据,我想不通(仍然很新) ,这就是我所拥有的

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

这两个 Twitter 功能都不需要身份验证,那么我怎样才能获得相同的结果? 多谢你们, 敏捷

【问题讨论】:

    标签: php json api twitter get


    【解决方案1】:

    您没有正确调用 TwitterOAuth 调用。不要只使用完整的 URL 方法路径并在哈希数组中传递参数。

    当 foreach 也能正常工作时,您可能也不想使用 while 循环。

    <?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>";
    }
    

    我没有直接测试过,但它应该可以工作。您还应该考虑改用功能更全面的 GET friendships/show 方法。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-09-24
      • 2014-03-08
      • 1970-01-01
      • 2023-04-06
      • 2021-09-15
      • 2012-05-28
      • 2012-06-15
      • 2015-07-05
      相关资源
      最近更新 更多