从Twitter API(THM oauth)解码JSON数据的问题

时间:2011-08-03 16:45:34

标签: twitter json oauth-provider

我正在使用tmhOAuth.php / class登录twitter。我成功登录并发送推文。

当我使用friends.php脚本时,我在插入数据库时​​遇到了一些问题。我确实认为我的问题在于代码中的$ paging变量。因为它只循环了七次。我关注的是626人,所以我的$ ids是626,然后$ paging是7。

当我在网络浏览器中运行php时,我只能提取7个关注者(即跟随用户#626,跟随用户526,跟随用户426 ......)它似乎回应了最后一个用户每个页面请求。这部分是由于通过PAGESIZE常量一次请求100个用户ID。当我使用不同的数字调整$ paging时,例如数字626,我得到{“errors”:[{“code”:17,“message”:“没有用户匹配指定的术语”}}}

不幸的是,我怀疑这是一个相当简单的php循环问题,但经过一段时间我试图破解这个问题后,我再也无法直接思考了。

提前致谢。

define('PAGESIZE', 100);
require 'tmhOAuth.php';
require 'tmhUtilities.php';

if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$ids += $data['ids'];
$cursor = $data['next_cursor_str'];
} else {
echo $tmhOAuth->response['response'];
break;
}
endwhile;

// lookup users
$paging = ceil(count($ids) / PAGESIZE);
$users = array();
for ($i=0; $i < $paging ; $i++) {
$set = array_slice($ids, $i*PAGESIZE, PAGESIZE);
$tmhOAuth->request('GET', $tmhOAuth->url('1/users/lookup'), array(
'user_id' => implode(',', $set)
));

// check the rate limit
check_rate_limit($tmhOAuth->response);

if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
if ($tmhOAuth->response['code'] == 200) {
$data = json_decode($tmhOAuth->response['response'], true);
$name = array();
foreach ($data as $val)
    {
        $name = $data[0]['screen_name'];
    }
    echo "this is the screen name  " .$name. "\n";
    $users += $data;
} else {
echo $tmhOAuth->response['response'];
break;
}

}
var_dump($users);
?>

我试图回应的数据,然后解析并插入数据库是标准的Twitter JSON数据,所以我不会在消息中包含它。任何帮助都是

1 个答案:

答案 0 :(得分:0)

问题解决了:

  foreach ($data as $val)
{
    $name = $val['screen_name'];
    echo "this is the screen name  " .$name. "\n";
    $users[] = $name;
}