Showing posts with label php. Show all posts
Showing posts with label php. Show all posts

Saturday, March 10, 2007

Easy Skype Status in Flash

After my post Flash Skype Status Part III I've received emails from some of my readers asking me for help as they couldn't understand 100% the whole process.
When I did the experiment in September last year, I didn't take into account many things and just continued playing with Flash, php and the XML from SkypeWeb.
To avoid confusion, let's see how to show in Flash my Skype Status in a simple way:

First of all, be sure you have the the latest SKYPE installed

Then follow one of the two options below:

TEXT ONLY
- Open a new Flash document
- Select the Text tool and put a dynamic text field on the stage with instance name "myStatus" as shown in the picture



- on a new layer, write the following actionscript:

function loadXML(loaded:Boolean):Void {
if (loaded) {
my_status = this.firstChild.firstChild.childNodes[2].childNodes;
attachStatus();
} else {
trace("XML not loaded!");
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("http://mystatus.skype.com/myID.xml");
function attachStatus():Void {
myStatus.text = my_status
}


You would need to change myID for your actual Skype ID, ie echo123
- Test your movie pressing ctrl+enter, your Skype status should be shown in the text field.
*There is still a problem: "cross-domain" policy. I will explain how to avoid it later on.

If you want to know more about XML and Flash, please follow this link.

ADDING INTERACTIVITY
If you want to show different images depending on your status and let users interact with you, then follow this steps:
- Open a new Flash document
- As there are 6 different status, you should have already 6 different images. Import them all into your document and create a movieclip for each of them



- The most important thing here is to put an identifier name to each of them; to do that, right click on the MovieClips in the Library and select "linkage" and write as Identifier the exact name of each status, for instance "Do Not Disturb"



- You might have noticed already that in the library there is a seventh movieclip called "loader_mc". So we need to put an instance of this empty movieclip on the stage and the stage will have nothing else but our "loader_mc" that in fact is empty!



- on a new Layer write the following code:
function loadXML(loaded:Boolean):Void {
if (loaded) {
my_status = this.firstChild.firstChild.childNodes[2].childNodes;
attachStatus();
} else {
trace("XML not loaded!");
}
}
xmlData = new XML();
xmlData.ignoreWhite = true;
xmlData.onLoad = loadXML;
xmlData.load("http://mystatus.skype.com/myID.xml");
function attachStatus():Void {
loader_mc.attachMovie(my_status, "status", 1);
}

You would need to change myID for your actual Skype ID, ie echo123
- Test your movie pressing ctrl+enter, your Skype status should be shown on your movie.
- We can as well add a script to allow any visitor to contact us when pressing the movieclip. To do that just add the following actionscript:

loader_mc.onPress = function() {
getURL("skype:myID?chat");
};


People can have a chat with you through Skype if you're online or leave a message if you're not.
There are more options and all of them are explained on the Advanced Skype Links.

CROSS-DOMAIN POLICY
Cross domain in flash is when data from X website is loaded into a movie on another website with different domains; for security reasons, Flash Player is only allowed to do that under certain circumstances. To read more about Flash Player Security Sandbox, click here.
As the data we are trying to load is served by skype.com, we would need to by-pass the cross-domain policy; we can do that using a php script:


<?php
header("Content-type: text/xml; charset=utf-8");
echo file_get_contents($_GET["proxy_url"]);
?>
Save this script as proxy.php and upload it to your server.

- Now we need to change one line on our actionscript, so instead of

xmlData.load("http://mystatus.skype.com/myID.xml");


we will write
xmlData.load("http://domain.com/proxy.php?proxy_url=http://mystatus.skype.com/myID.xml");

- Remember to change myID for your actual Skype ID and domain.com for your own domain address.

The good thing about this php file is that we can use it on different projects, for example you can load the RSS feed from my blog just writing:
http://domain.com/proxy.php?proxy_url=http://overloadstudios.blogspot.com/feeds/posts/default

and it will appear as if the data is served from a different domain.

That's it!
This post is similar to Flash Skype Status Part III, however is more detailed and both the actionscript and php files are optimized.

salut!

Sunday, February 25, 2007

Flash-PHP file downloads counter

If you want to keep a record and show how many times has a file been downloaded, you can use Flash, php and a simple txt file to hold the number of downloads.

When I finished my Firefox Screensaver, I showed it first at the spreadfirefox community, of course, without any way to count the downloads.
When I finally had the time to show it here on my blog, I was looking for a way to count the number of downloads but had the limitation that on this blog, as far as I know, is only possible to write "front-end" scripts and, as you know, without a server is not possible to have any downloads counter as there is no way to store any data.

However, using flash as front-end I can pull any data from anywhere behind the scenes and show it back on the same flash.

Then, if in case you want to create a file downloads counter, just follow these steps:

First of all, we will need 4 files,
- the file we want to be downloaded, usually a compressed .zip file
- theCounter.txt file which will store the downloads number
- the flash file to show the number of downloads and to allow people to download the file
- a php file which behind the scenes will manage the downloads counter and point to the right link to the file to be downloaded.

Let's start with the flash file:

- Open a new flash document
- Put on the stage all the graphics you need and also a dinamic text field which will show the number of downloads with an instance name of "downloads" as shown in the picture:



- Select the background image and press F8 to make it a movieclip, and put it as instance name "downloads_btn"



- On a different layer called "actions", write the following actionscript:
// --- Function to load the stored number ---
function loadingVars():Void {
var my_lv:LoadVars = new LoadVars();
my_lv.onLoad = function(success:Boolean) {
if (success) {
downloads.text = "( "+my_lv.downloads+" downloads )";
} else {
trace("Error loading/parsing LoadVars.");
}
};
my_lv.load("theCounter.txt?num="+random(999));
}
// --- Calling the function ---
loadingVars();
// --- Button actions ---
downloads_btn.onPress = function() {
getURL("downloads.php");
};
downloads_btn.onRelease = function() {
loadingVars();
};
What is happening here is, first we are loading the variable stored in theCounter.txt file and to avoid the cache memory and load a fresh value everytime, we add a ?num=random(999) that gives us a unique value when calling the same file, that way the browser thinks you are loading a different file...
When pressing the button, we are calling a php which is the one in charge of the download and counter system.
On releasing the button, we load the variable again and as the file we are offering for download has been requested another time, the value of the variable loaded is bigger by one.
Save the file and publish it to have a swf file.

Now let's see the php:

- Open dreamweaver or any other php editor; even notepad can do the job.
- Write the following code:
I'm sorry this is an image but as I said before, blogger doesn't like back-end scripts and stripped it out when I wrote it here...

We are first opening theCounter.txt file, checking what it has inside and from it, the value of "donwloads" is stored in a variable called "count", then add one to this value, write the variable and the new value of it back to the txt file and closes it. Next we give the real location of the zip file and redirect the browser to it.

The .txt file has only one line inside:
downloads=X
where X is the current number of downloads, so the first time it should be 0 (zero).
That's it!
just upload everything to a server with php support and embed your swf where convenient.
For example, I embeded my swf on this post:



Try it, click on the button, download and install the screensaver, if you don't like you can always uninstall it :)

salut!

Friday, September 15, 2006

Flash Skype Status - Part III


My friend Sebastian Recabarren pointed me out a little problem about my Skype Status flash widget. He asked me who was lying or his Skype or my blog.
Browser's cache memory!
I though this problem was only when the user goes online through a proxy server, which keeps everything in its cache, but seems like flash always takes the image from the browsers cache even if there is no proxy server in between.
To avoid this problem, we can use an XML provided by SkypeWeb instead of loading the image.
The XML is available from:
http://mystatus.skype.com/userID.xml

That data is easy to pull from flash and then show the right status attaching the right movieclip.
What I did is, instead of

image01.loadMovie("http://mystatus.skype.com/smallclassic/yourID");

I'm loading the XML as follows:

function loadXML(loaded:Boolean):Void {
if (loaded) {
xmlNode =
this.firstChild;
my_status = xmlNode.childNodes[0].childNodes[2].childNodes;
attachStatus();
}
else {
trace("XML not loaded!");
}
}
xmlData =
new XML();
xmlData.ignoreWhite =
true;
xmlData.onLoad = loadXML;
xmlData.load("http://mystatus.skype.com/myID.xml");
function attachStatus():Void {
image01.loader_mc.attachMovie(my_status,"status",1)
}

I have added 6 movieclips showing the different possible status, each of them have a linkage identifier name depending on the status. To add an identifier to a movieclip just right click on it (in the library) and select "linkage properties", tick the "Export for actionscript" option and change the identifier to its equivalent skype status, ie: Not Available
Voila!
Testing locally everything works fine.
But, as usual when debugging, there is a new problem: cross domain policies.
Due to security reasons, any online flash application cannot load data from a different server and my experiment doesn't work online.
My mate Stephen Downs (Tink) pointed me out the solution:

It is possible to get round the sandbox by loading the XML into some PHP or something on your server and then loading it into Flash from there. Tricks the player into thinking your serving it


and my colleague Thomas Van Steenwinckel gave me a hand with the php and together we put the following script on my server:

<?
header("Content-type: text/xml; charset=utf-8");
$filename = "http://mystatus.skype.com/myID.xml" ;
$dataFile = fopen($filename, "r" );
if ( $dataFile )
{
while (!feof($dataFile))
{
$buffer = fgets($dataFile, 4096);
echo $buffer;
}
fclose($dataFile);
}
else
{
die( "fopen failed for $filename" ) ;
}
?>

Now on flash I'm loading the XML created by this php instead of the one provided by skypeWeb and my blog shows the right status of my Skype account.
Nice way to get rid of the cross domain issues.

PD: Just in case, remember to update to the latest Skype.

Salut!

-- IMPORTANT UPDATE --
Part I
Part II
Free Skype Status Flash Button

and the latest one of this series:
Easy Skype status in Flash
---------