我可以很容易地使用PHP和JSON获得用户的推文,但是一旦我用它来获取关注者列表,我就会收到错误。两者都使用JSON来返回值。
代码是:
$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的值以及如何仅通过ids数组循环?
我想解析读入数据库的结果。
答案 0 :(得分: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';
}
尚未测试但应该做你想做的事情,但请记住你想要使用某种缓存
答案 1 :(得分: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 />";
}
}