1

我正在使用 tmhOAuth.php / class 登录到 twitter。我已经成功登录并发送了一条推文。

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

当我在网络浏览器中运行 php 时,我只能提取 7 个关注者(即关注用户 #626、关注用户 526、关注用户 426...)它似乎在呼应每个页面请求上的最后一个用户. 这部分是由于通过 PAGESIZE 常量一次请求 100 个用户 ID。当我使用不同的数字(例如数字 626)调整 $paging 时,我得到 {"errors":[{"code":17,"message":"No user matches for specified terms"}]}

不幸的是,我怀疑这是一个相当简单的 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 数据,所以我不会在消息中包含它。任何帮助都是

4

1 回答 1

0

问题解决了:

  foreach ($data as $val)
{
    $name = $val['screen_name'];
    echo "this is the screen name  " .$name. "\n";
    $users[] = $name;
}
于 2011-08-05T04:25:45.253 回答