Page 1 of 1

Random user PHP

Posted: Mon Nov 09, 2009 8:13 am
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

Re: Random user PHP

Posted: Mon Nov 09, 2009 8:33 am
by AbraCadaver

Code: Select all

$users = array('user1','user2','user3');
$random_user = $users[array_rand($users)]; 
-Shawn

Re: Random user PHP

Posted: Mon Nov 09, 2009 8:51 am
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!

Re: Random user PHP

Posted: Mon Nov 09, 2009 8:57 am
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

Re: Random user PHP

Posted: Mon Nov 09, 2009 9:16 am
by tommiwalsh
Great! Thanks again, I'll give it a go when I get back and post to let you know it worked.

Re: Random user PHP

Posted: Mon Nov 09, 2009 10:15 am
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);
}
 
?>