...

View Full Version : Solved: X-Fire Status with PHP



fbelzile
12-31-2005, 08:38 PM
I'm making a clan for the America's Army game and members we will be using xfire for voice chat. Heres the question:

Is there any way to use PHP to see the status of xfire members to put on the "Who's Online" page?

I'm am still very new to PHP and I'm wondering if this is possible.
Thanks :)

fbelzile
01-03-2006, 02:41 PM
I just found out that this will be impossible.

I need to parse every profile page. I know it will take a while, but how would I do this?

Heres my profile page:

http://profile.xfire.com/fbelzile

schleppel
01-03-2006, 03:47 PM
To get a single users status i would do this:

<?php

// set the username of the person
$user = 'fbelzile' /*'manikowski'*/;

// get the profile page from the xfire server
// you have to have allow-url-fopen enabled
// info: http://uk2.php.net/ref.filesystem#ini.allow-url-fopen
$page = file_get_contents('http://www.xfire.com/xf/modules.php?name=XFire&file=profile&uname=' . $user);

// match the "User is: online/offline"
preg_match('#\<strong\>User is\:\</strong\>.+\<span class\="[^"]+"\>\<b\>([^\<]+)\</b\>\</span\>#isU'
,$page,$match);

// if everything worked,
// $match[1] is now online or offline
echo $user . ' is ' . $match[1];

?>

If you want multiple users, and they will all be in your (or someone else's) friends list, you could grab them all at the same time and sort using pre_match_all. It would be faster than getting them individually because you'd only have to request one page instead of many pages.

fbelzile
01-03-2006, 05:46 PM
Awesome!
Thats what I'm talkin about :thumbsup:

OK, sorry but I'm very new to PHP still and I'm wondering how to get multiple users from that one page (my friend(s) on the same page)...

Sorry if Im asking for a lot, but code will help.


Thank-you so much!! :)

schleppel
01-03-2006, 06:49 PM
This outputs a HTML list of users and status from the page, the person's profile first, and all the people from the friends list:

<?php

// set the username of the person
$user = 'fbelzile';

// get the profile page from the xfire server
// you have to have allow-url-fopen enabled
// info: http://uk2.php.net/ref.filesystem#ini.allow-url-fopen
if(($page = file_get_contents('http://www.xfire.com/xf/modules.php?name=XFire&file=profile&uname=' . $user)) === false)
{
exit('Could not load file.');
}

// match the "User is: online/offline"
preg_match('#\<strong\>User is\:\</strong\>.+\<span class\="[^"]+"\>\<b\>([^\<]+)\</b\>\</span\>#isU',$page,$match);

// match you friends status's
preg_match_all('#\<li\>\s+\<span class\="status"\>.+\<span class\="[^"]+"\>\<b\>([^\<]+)\</b\>\</span\>.+\</span\>.+\<a href\="[^"]+"\>([^\<]+)\</a\>#isU',$page,$matches);

// echo profile owners status
echo "<ul>\n\t<li>" . $user . ' is ' . $match[1] . '</li>';
// loop through and echo friends status's
foreach($matches[2] as $key => $value)
{
// $value is the username and $matches[1][$key] is the status
echo "\n\t<li>" . $value . ' is ' . $matches[1][$key] . '</li>';
}
echo "\n</ul>";

?>

fbelzile
01-03-2006, 09:34 PM
Nice!!
Now, if I wanted to only display the users online, how would I do that?
(Last question, I swear! lol)

I got this, but its only displaying the name Tyler...



<?php

$user = 'fbelzile';

if(($page = file_get_contents('http://www.xfire.com/xf/modules.php?name=XFire&file=profile&uname=' . $user)) === false)
{
exit('Could not load file.');
}

preg_match('#\<strong\>User is\:\</strong\>.+\<span class\="[^"]+"\>\<b\>([^\<]+)\</b\>\</span\>#isU',$page,$match);

preg_match_all('#\<li\>\s+\<span class\="status"\>.+\<span class\="[^"]+"\>\<b\>([^\<]+)\</b\>\</span\>.+\</span\>.+\<a href\="[^"]+"\>([^\<]+)\</a\>#isU',$page,$matches);

foreach($matches[2] as $key => $value)
{
if($matches[2][$key] = 'online')

{
echo $value;
}

if($matches[2][$key] = 'offline')

{
echo "";
}
}

?>


Laugh at me if you want! Im a real PHP newbie :confused:

Thanks again. :)

Prikid
01-03-2006, 09:54 PM
you'd need to setup a loop before calling this script. Not such a flexible example (would be more flexible if you were pulling information from database)


<?
$users = array("user1","user2","user3","user4");

foreach ($users as $key => $user)
{

... the script above
}
?>

schleppel
01-03-2006, 10:54 PM
This should work, hopefully:

<?php

// set the username of the person
$user = 'fbelzile';

// get the profile page from the xfire server
// you have to have allow-url-fopen enabled
// info: http://uk2.php.net/ref.filesystem#ini.allow-url-fopen
if(($page = file_get_contents('http://www.xfire.com/xf/modules.php?name=XFire&file=friends&uname=' . $user)) === false)
{
exit('Could not load file.');
}

// match the "User is: online/offline"
preg_match('#\<strong\>User is\:\</strong\>.+\<span class\="[^"]+"\>\<b\>([^\<]+)\</b\>\</span\>#isU',$page,$match);

// match you friends status's
preg_match_all('#\<td width\="22%"\>\s+\<a href\="modules\.php\?[^"]+"\>([^\<]+)\</a\>\s+\</td\>\s+\<td width\="13%"\>\s+\<span class\="[^"]+"\>(online|offline)\</span\>#isU',$page,$matches);

// start the HTML list
echo "<ul>\n\t";

// check if the profile's owner is online
if($match[1] == 'online')
{
echo '<li>' . $user . '</li>';
}

// loop through the matched friends
foreach($matches[1] as $key => $value)
{
// check if they're online.
if($matches[2][$key] == 'online')

{
// echo name
echo "\n\t<li>" . $value . '</li>';
}
}

// end list
echo "\n</ul>";

?>


Edit: I updated the URL to the friends page.
Edit2: copied in the script again, i think i messed it up the first time.

fbelzile
01-03-2006, 11:13 PM
THANK-YOU!!

You helped me a lot in making my site!
Man, I love this forum :)

I have always got an answer for every question I had.

Thanks again schleppel (and Prikid, for the answer I never got to try) :)
Have a good day ;)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum