simplexml + foreach +twitter with unexpected results
Im using the following code to output all the nodes from the xml shown.
$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"); 开发者_如何学Go
All i get back is Array ( [0] => [1] => [2] => )
Heres a sample of the xml
http://twitter.com/statuses/followers/barakobama.xml?cursor=-1What am i doing wrong? Everything everyone has suggested has not worked! Im going insane.
You need either:
$id = $user['id'];
or
$id = $user->attributes()->id;
See basic usage of SimpleXML. What you're doing isn't a valid way of querying an attribute.
$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;
There is no next_link
or prev_link
in that XML, I assume you're talking about previous_cursor
and next_cursor
精彩评论