2012-01-31 101 views
0

我可以很容易地使用PHP和JSON获取用户推文,但只要我用它来获取关注者列表,就会收到错误信息。两者都使用JSON来返回值。在PHP中使用JSON获取Twitter追随者?

的代码是:

$jsonurl = "https://api.twitter.com/1/followers/ids.json?cursor=-1&screen_name=mooinooicat"; 
$contents = file_get_contents($jsonurl); 
$results = json_decode($contents, true); 

echo "<pre>"; 
print_r($results); 
echo "</pre>"; 

这给了我下面的数组:

Array 
(
    [next_cursor] => 0 
    [ids] => Array 
     (
      [0] => 31085924 
      [1] => 53633023 
      [2] => 18263583 
     ) 

    [previous_cursor] => 0 
    [next_cursor_str] => 0 
    [previous_cursor_str] => 0 
) 

如何获得next_cursor和previous_cursor的价值观和我怎么循环刚刚经历的ID阵列?

我想解析读取到数据库中的结果。

+0

看看http://php.net/manual/en/language.types.array.php – 2012-01-31 11:36:46

+0

@andrebruton https://api.twitter.com/1/followers/ids.json它向我们提供了我提供的屏幕名称追随者的ID,我们可以替换屏幕名称吗? – 2012-07-30 23:57:22

+0

请参阅:[Twitter追随者API教程](https://dev.twitter.com/rest/reference/get/followers/ids) – kenorb 2015-05-30 22:45:41

回答

0

感谢没有例子的所有问题的答案...

我终于成功地从Getting values from a single array

这里弄明白使用示例代码如下:

foreach ($results as $result) { 
    if (is_array($result)) { 
    foreach ($result as $sub_result) { 
     // You can store this value in a variable, or output it in your desired format. 
     echo $sub_result . "<br />"; 
    } 
    } else { 
    echo $result . "<br />"; 
    } 
} 
+0

如果您想要数组键和值,请使用:foreach($ result as $ key => $ value){然后键名是$ key,值是$ value – andrebruton 2012-02-01 09:05:59

1

不使用正确的API尝试这样的事情

function fetch_twitter_count($user) { 
    if ($json = file_get_contents("http://api.twitter.com/1/users/show.json?screen_name=$user")) { 
     if(empty($json)) return 0; 

     $json = json_decode($json['body'], true); 

     return number_format(intval($json['followers_count'])); 
    } 
    return 'API Error'; 
} 

没有t测试,但应该做你想做的事情但是保持联系,你会想要使用某种缓存

相关问题