A Flash Developer Resource Site

Results 1 to 2 of 2

Thread: [RESOLVED] Twitter Updates via php/xml

  1. #1
    Senior Member
    Join Date
    Oct 2004
    Location
    London
    Posts
    186

    resolved [RESOLVED] Twitter Updates via php/xml

    Hi all,

    I'm trying (desperately) to re-work the code here untoldentertainment.com (AS3) to AS2...

    First time I'm using sharedObject and also, bit confused about calling the .xml file via the php.

    Here's the actionscript:

    Actionscript Code:
    var twitterXML:XML; // This holds the xml data
    var cachedTwitterData:SharedObject; // This stores the data loaded from the Flash cookie
    var twitterPHPScriptPath:String;
    var myTwitterID:String;
    var wereAllowedToWriteToTheFlashCookie:Boolean; // You can use this value to decide whether or not to make repeated save attempts
     
    //Put your Twitter username here.  For example, ours is "untoldEnt" :
    myTwitterID = "aneemal";
    // Put the path to your php script here:
    twitterPHPScriptPath = "tweet.php";
    wereAllowedToWriteToTheFlashCookie = true; // (let's be optimistic until we discover otherwise)
    // Check to see if the user has already retrieved the Twitter updates and stored them in a Flash cookie:
    loadTwitterDataFromFlashCookie();
     
    function loadTwitterDataFromFlashCookie() {
        // Call up the Twitter data from the Flash cookie on the user's machine:
        cachedTwitterData = SharedObject.getLocal("twitter");
        var timeStamp:Date = cachedTwitterData.data.timeStamp;
        if (timeStamp != null) {
            // The timeStamp variable is in there, which means we've retrieved Twitter info for this user at some point.
            // Now let's check the timeStamp. If the last time we grabbed Twitter data was too long ago, let's grab fresh info:
            if (hasExpired(timeStamp)) {
                // The most recent copy of the Twitter data in the Flash cookie is old!  Let's get some fresh data:
                trace("cookie has expired!  let's get some fresh data");
                loadTwitterXML();      
            } else {
                // It hasn't been very long since we grabbed a fresh copy of the Twitter data. Let's just use what we've got in the Flash cookie:
                trace("cookie is fresh. display the data.");
                twitterXML = cachedTwitterData.data.twitterXML;
                showTwitterStatus();
            }
        } else {
            // If there's no timeStamp variable in there, then the cookie's empty.  We need to hit Twitter to grab our data for the first time.
            loadTwitterXML();              
        }          
    }
     
    function saveTwitterDataToFlashCookie() {
        flushStatus = cachedTwitterData.flush(10000);
        if (flushStatus == true) {
            trace("********* save complete. ***********\n");
            finishedHandlingTwitterData();
        } else if (flushSatus == false) {
            trace("User denied permission -- value not saved.\n");
            wereAllowedToWriteToTheFlashCookie = false;
        }
    }
     
    function finishedHandlingTwitterData() {
        trace("Twitter data saved.");
    }
     
    function hasExpired(timeStamp:Date) {
        // Store the date RIGHT NOW, to compare against the Flash cookie timeStamp:
        var now:Date = new Date();
        // Let's say that the cookie expires after 1 hour, 0 minutes and zero seconds:
        var expiryHours:Number = 1;
        var expiryMinutes:Number = 0;
        var expirySeconds:Number = 0;
     
        // Store some handy conversion values:
        var msPerHour:Number = 3600000;
        var msPerMinute:Number = 60000;
        var msPerSecond:Number = 1000;
     
        // Multiply those values to get the expiry time in milliseconds:
        var expiryTime:Number = (expiryHours * msPerHour) + (expiryMinutes * msPerMinute) + (expirySeconds * msPerSecond);
     
        // Return whether or not the timeStamp is past the expiry date:
        return (now.getTime() - timeStamp.getTime() > expiryTime)
    }

    function loadTwitterXML() {
        var getTwitter:XML = new XML();
        getTwitter.ignoreWhite = true;
        getTwitter.onLoad = function(success:Boolean) {
            if (success) {
                finishLoadingXML();
            } else {
                trace ("Error loading php");
            }
        }
        getTwitter.load(twitterPHPScriptPath + "?twitterId=" + myTwitterID);           
    }
     
    function finishLoadingXML() {
        // Populate the xml object with the xml data:
        cachedTwitterData = SharedObject.getLocal("twitter");
        // Set the date/time RIGHT NOW:
        cachedTwitterData.data.timeStamp = new Date();
        twitterXML = new XML;
        // Store the twitterXML data:
        cachedTwitterData.data.twitterXML = twitterXML;
        // (Try to) save the whole shebang the user's Flash cookie:
        saveTwitterDataToFlashCookie();
        showTwitterStatus();
    }
     
    function showTwitterStatus() {
        trace("showTwitterStatus");
        // Uncomment these lines if you want to see all the fun stuff Twitter sends you:
        //trace(twitterXML);
        //trace(twitterXML.head);
        // Prep the text field to hold our latest Twitter update:
        twitter_txt.wordWrap = true;
        twitter_txt.autoSize = true
        // Populate the text field with the first element in the status.text nodes:
        twitter_txt.text = twitterXML;
    }

    And here's the php:
    PHP Code:
    <?php
     
    $twitterId 
    = isset($_REQUEST["twitterId"]) ? $_REQUEST["twitterId"] : '';
    if( 
    $twitterId == "" ){
                    exit;
    }
    $file file_get_contents("http://twitter.com/statuses/user_timeline/" $twitterId  ".xml" );
    echo 
    $file;
     
    ?>
    Please, please, please, help me get this project off the ground!

    Many thanks for any input.

  2. #2
    Senior Member
    Join Date
    Oct 2004
    Location
    London
    Posts
    186
    Ok, I've managed to get this working... if interested, please see my next thread for script.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center