How to filter JSON and XML. PHP
I have a function that gets friends and followers of a twitter account. I'm making a "Who's Not Following back app" How do I filter the xml/json data (I can use both) ie to echo what is in following but not in followers.
Here's my code
function twitter_notf_page($query) {
$user = $query[1];
if (!$user) {
user_ensure_authenticated();
$user = user_current_username();
}
$request = API_URL."statuses/friends/{$user}.xml";
$tla = lists_paginatedd_process($request);
$request = API_URL."statuses/followers/{$user}.xml";
$tl = lists_paginatedd_process($request);
$content = "<h2>FRIENDS</h2>";
$content .= theme('notf', $tla);
$content.="<br><hr>";
$content .= "<h2>FOLLOWERS</h2>";
$content .= theme('notf', $tl);
theme('page', 'Followers', $content);
}
function lists_paginatedd_process($url) {
// Adds cursor/pagination parameters to a query
$cursor = $_GET['cursor'];
if (!is_numeric($cursor)) {
$cursor = -1;
}
$url .= '?cursor='.$cursor;
$xml = twitter_process($url);
$ret = simplexml_load_string($xml)
return $ret;
}
Results - Following
<?xml version="1.0" encoding="UTF-8"?>
<users_list>
<users type="array">
<user>
<id>66968592</id>
<name>DJ Swaggz ⬅click2fol</name>
<screen_name>DjSwaggz</screen_name>
</user>
<user>
<id>288859268</id>
<name>tegan shanks</name>
<screen_name>teganxroshanks</screen_name>
</user></users_list>
Results - Followers
<?xml version="1.0" encoding="UTF-8"?>
<users_list>
<users type="array">
<user>
<id>66968592</id>
<name>DJ Swaggz ⬅click2fol</name>
<screen_name>DjSwaggz</screen_name>
</user>
</user></users_list>
I want this guy to display (T开发者_如何学Che one that's not following back)
<user>
<id>288859268</id>
<name>tegan shanks</name>
<screen_name>teganxroshanks</screen_name>
</user>
Here you go :)
<?PHP
function get_nonfollowers($followingXml, $followersXml)
{
$following = simplexml_load_string($followingXml);
$followers = simplexml_load_string($followersXml);
$notFollowingMe = array();
foreach($following->users->user as $f)
{
$isFollowingMe = false;
foreach($followers->users->user as $follower)
{
if(strcmp($follower->id, $f->id) == 0)
{
$isFollowingMe = true;
break;
}
}
if(!$isFollowingMe)
$notFollowingMe[] = $f;
}
return $notFollowingMe;
}
$followingXml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<users_list>
<users type="array">
<user>
<id>66968592</id>
<name>DJ Swaggz ⬅click2fol</name>
<screen_name>DjSwaggz</screen_name>
</user>
<user>
<id>288859268</id>
<name>tegan shanks</name>
<screen_name>teganxroshanks</screen_name>
</user>
</users>
</users_list>
EOF;
$followersXml = <<<EOF
<?xml version="1.0" encoding="UTF-8"?>
<users_list>
<users type="array">
<user>
<id>66968592</id>
<name>DJ Swaggz ⬅click2fol</name>
<screen_name>DjSwaggz</screen_name>
</user>
</users>
</users_list>
EOF;
$notFollowingMe = get_nonfollowers($followingXml, $followersXml);
print_r($notFollowingMe);
?>
Output
Array
(
[0] => SimpleXMLElement Object
(
[id] => 288859268
[name] => tegan shanks
[screen_name] => teganxroshanks
)
)
Loop through the screen names
foreach($notFollowingMe as $user)
{
echo $user->screen_name . "\r\n";
}
精彩评论