Random user PHP

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tommiwalsh
Forum Newbie
Posts: 4
Joined: Mon Nov 09, 2009 7:59 am

Random user PHP

Post by tommiwalsh »

Hi,

I'm working on a Flash twitter feed which uses PHP to generate an xml file, which in turn the Flash file reads. If I specify a twitter account then this all works fine, but I need it to randomly display one of multiple account feeds. Here's the PHP I have so far which reads the one account, is there a way to include all the different accounts and have it pick one at random?

Code: Select all

 
<?php
 
$feedContent = file_get_contents("http://twitter.com/statuses/user_timeline/username1.xml" );
 
if( $feedContent == "" ){
                exit;
}
else
{
    $newfile="twitterFeed.xml";
    $file = fopen ($newfile, "w");
    fwrite($file, $feedContent);
    fclose ($file); 
}
?>
 
Any help with this would really be appreciated as I seem to be going round in circles at the moment..

Thanks,

Tom
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Random user PHP

Post by AbraCadaver »

Code: Select all

$users = array('user1','user2','user3');
$random_user = $users[array_rand($users)]; 
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tommiwalsh
Forum Newbie
Posts: 4
Joined: Mon Nov 09, 2009 7:59 am

Re: Random user PHP

Post by tommiwalsh »

Hi,

Thanks for replying so quickly, I'm not at home so can't test it but is this how it should look now?

Code: Select all

 
<?php
 
$users = array ('username1', 'username2', 'username3' );
 
$random_user = $users [ array_rand ($users) ];
 
$url = "http://twitter.com/statuses/user_timeline/" . $random_user;
 
$feedContent = file_get_contents($url);
 
if( $feedContent == "" )
{
    exit;
}
else
{
    $newfile="twitterFeed.xml";
    $file = fopen ($newfile, "w");
    fwrite($file, $feedContent);
    fclose ($file);
}
 
?>
 
Sorry, I'm a complete novice when it comes to PHP..

Thanks again!
User avatar
AbraCadaver
DevNet Master
Posts: 2572
Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:

Re: Random user PHP

Post by AbraCadaver »

Looks good, but I would assume you need to add .xml.

Code: Select all

$url = "http://twitter.com/statuses/user_timeline/" . $random_user . ".xml";
-Shawn
mysql_function(): WARNING: This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
tommiwalsh
Forum Newbie
Posts: 4
Joined: Mon Nov 09, 2009 7:59 am

Re: Random user PHP

Post by tommiwalsh »

Great! Thanks again, I'll give it a go when I get back and post to let you know it worked.
tommiwalsh
Forum Newbie
Posts: 4
Joined: Mon Nov 09, 2009 7:59 am

Re: Random user PHP

Post by tommiwalsh »

Works perfectly, thanks again, that's been giving me a headache for a bit now!

In case anyone needs the finished code here's the working PHP-

Code: Select all

 
<?php
 
$users = array ('username1', 'username2', 'username3' );
 
$random_user = $users [ array_rand ($users) ];
 
$url = "http://twitter.com/statuses/user_timeline/" . $random_user . ".xml";
 
$feedContent = file_get_contents($url);
 
if( $feedContent == "" )
{
    exit;
}
else
{
    $newfile="twitterRandom.xml";
    $file = fopen ($newfile, "w");
    fwrite($file, $feedContent);
    fclose ($file);
}
 
?>
 
Post Reply