ThemeForest

PHP Wizards Needed! :)

3860 posts
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 100 and 499 items
  • United Kingdom
  • Referred between 200 and 499 users
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

3 years ago
faevilangel_designs
faevilangel_designs Recent Posts
Threads Started
83 posts
  • Has been a member for 2-3 years
  • Exclusive Author
  • 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.

3 years ago
2425 posts Customer Service Manager aka SuperDrew
  • Has been a member for 3-4 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Attended a Community Meetup
  • Interviewed on the Envato Notes blog
  • Envato Staff
  • Support Staff
  • Author had a Free File of the Month
  • Exclusive Author
  • Sold between 100 and 1 000 dollars
  • Bought between 50 and 99 items
  • United States
  • Referred between 10 and 49 users
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.

3 years ago
CreatingDrew is an Envato staff member
3860 posts
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 100 and 499 items
  • United Kingdom
  • Referred between 200 and 499 users
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

3 years ago
420 posts
  • Has been a member for 3-4 years
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 10 and 49 users
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

3 years ago
3860 posts
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 100 and 499 items
  • United Kingdom
  • Referred between 200 and 499 users
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

3 years ago
3860 posts
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 100 and 499 items
  • United Kingdom
  • Referred between 200 and 499 users
flashjunkie says

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

3 years ago
420 posts
  • Has been a member for 3-4 years
  • Beta Tester
  • Exclusive Author
  • Sold between 1 000 and 5 000 dollars
  • Bought between 10 and 49 items
  • United States
  • Referred between 10 and 49 users
vasilios says

Glad it worked! :D

3 years ago
3860 posts
  • Has been a member for 5-6 years
  • Repeatedly Helped protect Envato Marketplaces against copyright violations
  • Contributed a Tutorial to a Tuts+ Site
  • Contributed a Blog Post
  • Author had a Free File of the Month
  • Beta Tester
  • Exclusive Author
  • Sold between 10 000 and 50 000 dollars
  • Bought between 100 and 499 items
  • United Kingdom
  • Referred between 200 and 499 users
flashjunkie says
Glad it worked! :D

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

Thanks again man! :D

3 years ago
by
by
by
by
by