3868 posts
  • Has been part of the Envato Community for over 7 years
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
flashjunkie says

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

faevilangel_designs
faevilangel_designs Recent Posts Threads Started
83 posts
  • Has been part of the Envato Community for over 5 years
  • Sells items exclusively on Envato Market
  • Located in United Kingdom
faevilangel_designs says

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.

2541 posts
  • Has referred 10+ members
  • Has sold $100+ on Envato Market
  • Has collected 50+ items on Envato Market
  • Located in United States
+9 more
CreatingDrew says
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.

3868 posts
  • Has been part of the Envato Community for over 7 years
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
flashjunkie says

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

426 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has sold $5,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
vasilios says

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

3868 posts
  • Has been part of the Envato Community for over 7 years
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
flashjunkie says
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! :D

3868 posts
  • Has been part of the Envato Community for over 7 years
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
flashjunkie says

made some changes but it works like a charm, you are awesome! Thanks man!!!

426 posts
  • Has been part of the Envato Community for over 5 years
  • Has referred 10+ members
  • Has sold $5,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+3 more
vasilios says

Glad it worked! :D

3868 posts
  • Has been part of the Envato Community for over 7 years
  • Has referred 200+ members
  • Has sold $10,000+ on Envato Market
  • Has been a beta tester for an Envato feature
+9 more
flashjunkie says
Glad it worked! :D

Flawlessly! http://www.flash-junkie.com/iTweet/

Thanks again man! :D

by
by
by
by
by
by