simplexml + foreach + twitter带有意想不到的结果

时间:2009-11-08 18:04:31

标签: php simplexml

我使用以下代码输出显示的xml中的所有节点。

        $cursor = "?cursor=-1"
        $xml= new SimpleXmlElement($to->OAuthRequest('http://twitter.com/statuses/followers.xml?$cursor'));
        foreach ($xml->xpath('/users_list/users/user') as $user) {
            $id = $user->id;
            $names .= $user->screen_name;
            $profimg = $user->profile_image_url;
        }
        $next = $user->next_link;
        $prev = $user->prev_link;
        $pusharray = array("$names", "$next", "$prev");     

我回来的只有Array ( [0] => [1] => [2] => )

下载xml的样本
http://twitter.com/statuses/followers/barakobama.xml?cursor=-1

我做错了什么?每个人都建议的一切都没有奏效!我疯了。

2 个答案:

答案 0 :(得分:5)

您需要:

$id = $user['id'];

$id = $user->attributes()->id;

basic usage of SimpleXML。你正在做的不是查询属性的有效方法。

答案 1 :(得分:1)

$users_list = simplexml_load_file('http://twitter.com/statuses/followers/barakobama.xml?cursor=-1');

foreach ($users_list->users->user as $user)
{
    echo $user->id, ' ', $user->screen_name, ' ', $user->profile_image_url, "<br />\n";
}

echo 'prev: ', $users_list->previous_cursor, ' - next: ', $users_list->next_cursor;

该XML中没有next_linkprev_link,我假设您在谈论previous_cursornext_cursor