- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
Hey guys.
OK… Im trying to build a small flash widget that loads in data from twitters ‘user_timeline.xml’ file. Now due to twitters strict crossdomain policy i have had to sort some PHP to gather the xml data and save it as a new XML file on my server from which the flash reads.
The problem is that the script can only be called 150 times per hour due to twitters API limits, so i have three options,
1) Make the flash call the PHP script every time its launched and effectively break the file due to the API limits.
2) Manually update the XML by browsing to the PHP file every time i tweet on twitter
3) Ask one of you extremely kind people to help me create a PHP script that updates the XML file every 5-10 minutes or so.
So as i am on the forums its not hard to guess that i want to go with option 3. Is this something that is easy to do? i basically want the script to run every 5-10 minutes so that the twitter widget is more or less up to date but i dont run into any bother with the API limits.
This is my PHP script…
<?php //load in the xml ---
$theRAW = file_get_contents("http://twitter.com/statuses/user_timeline/flashjunkie.xml?count=1");
$theXML = simplexml_load_string($theRAW);
//load in xml file
$xmlFile = "twitter.xml";
$xmlHandle = fopen($xmlFile, "w");
//set xml as string
$xmlString = $theXML?>asXML();
echo($xmlString);
//write xml to file
fwrite($xmlHandle, $xmlString);
//close out xml file
fclose($xmlHandle);
?>
Any help, code or advice is MUCH appreciated 
Thanks
Jay
I’m not a php wiz, but i’m sure a handy function called CURL will do exactly what you want
it’s pretty much standard on php from version 5, and most hosts will have the module uploaded, but always double check.
- Community Moderator
- United States
- Was featured in a podcast
- Attended a Community Meetup
- Author had a Free File of the Month
- Bought between 50 and 99 items
- Repeatedly Helped protect Envato Marketplaces against copyright violations
- Has been a member for 4-5 years
- Contributed a Tutorial to a Tuts+ Site
I’m not a php wiz, but i’m sure a handy function called CURL will do exactly what you want it’s pretty much standard on php from version 5, and most hosts will have the module uploaded, but always double check.Since he is using file_get_content() cURL isn’t needed but will speed things up big time, it’s much faster than file_get_contents();
I’m thinking a simple cron job that calls the php file every x minutes would do the trick.
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
Thanks for the advice guys, i think i am majorly over complicating things here,
im going to write some code that checks the last modified time of the twitter.xml file, if it is older than x minutes then reload the file using the function i showed you, otherwise do nothing 
now… where to start…
lol
You can have the best of both worlds: #1 and #3. Add a simple if statement to check if the twitter.xml file was edited within the last X minutes. If so, then get the info from the Twitter API and write it to the file, and finally display the XML to the user (according to your first bit of code).
This code hasn’t been tested, but I don’t see any bugs, but there most likely is.
<?php $file = 'twitter.xml';
$url = 'http://twitter.com/statuses/user_timeline/flashjunkie.xml?count=1';
$diff = 5; // Number of minutes to keep the old file.
/* Don't edit below this line unless you know what you are doing */
/* ...or I screwed something up. */
$content = '';
if ((time() - filemtime($file))?>= 60 * (int)$diff) {
$xml = simplexml_load_file($url);
$fh = fopen($file, 'w') or die('Couldn\'t open or create the file.');
$content = $xml->asXML();
fputs($fh, $content); // fputs is faster than fwrite
echo $content;
fclose($fh);
unset($content, $xml, $fh);
} else {
$fh = fopen($file, 'a');
echo fread($fh, filesize($file));
fclose($fh);
unset($fh);
}
?>
file_get_contents() isn’t needed. simplexml_load_file does all the work for you. 
EDIT : That link in the code shouldn’t be there. hits forum system
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
You can have the best of both worlds: #1 and #3. Add a simple if statement to check if the twitter.xml file was edited within the last X minutes. If so, then get the info from the Twitter API and write it to the file, and finally display the XML to the user (according to your first bit of code).This code hasn’t been tested, but I don’t see any bugs, but there most likely is.
<?php $file = 'twitter.xml'; $url = 'http://twitter.com/statuses/user_timeline/flashjunkie.xml?count=1'; $diff = 5; // Number of minutes to keep the old file. /* Don't edit below this line unless you know what you are doing */ /* ...or I screwed something up. */ $content = ''; if ((time() - filemtime($file))?>= 60 * (int)$diff) { $xml = simplexml_load_file($url); $fh = fopen($file, 'w') or die('Couldn\'t open or create the file.'); $content = $xml->asXML(); fputs($fh, $content); // fputs is faster than fwrite echo $content; fclose($fh); unset($content, $xml, $fh); } else { $fh = fopen($file, 'a'); echo fread($fh, filesize($file)); fclose($fh); unset($fh); } ?>file_get_contents() isn’t needed. simplexml_load_file does all the work for you.
EDIT : That link in the code shouldn’t be there. hits forum system
testing now, if this works i will love you forever! 
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
made some changes but it works like a charm, you are awesome! Thanks man!!!
Glad it worked! 
- Author had a Free File of the Month
- Author was Featured
- Beta Tester
- Bought between 100 and 499 items
- Contributed a Blog Post
- Contributed a Tutorial to a Tuts+ Site
- Exclusive Author
- Has been a member for 6-7 years
- Item was Featured
