string from php script to flash - php

I'm using facebook php sdk to get friend list of an user. But I'm strugling to get that list to flash.
The idea is that the list is always new, depending from the user loading the application. So what I need is one .html file which is loaded with php script to proccess friend list and embedded flash .swf to which php would pass information.
How can I achieve this? thanks

There are two ways to do it:
Set the flashvars to the data you want to pass to the Flash file. Since the friend list rarely changes, that should work.
Or use a URLLoader:
var loader:URLLoader = new URLLoader();
var request:URLRequest = new URLRequest("http://example.com/yourscript.php");
loader.addEventListener("complete", loader_complete);
loader.loader(request);
private function loader_complete(event:Event):void {
var scriptData:String = event.currentTarget.data;
// process data
}

Related

Load remote php to send form submission variables, is it possible?

I have created a form using Flash & actionscript 3. The problem I am having is that I can send the variables to php when stored at the same location as that of the swf file, but if i want to make an android AIR application for the form, I cant address the php file which send me the email. Is it possible to address a remote php file? Please help me, I am stuck at this stage.
Probably you stuck with crossdomain.xml.
The idea that first flash load the policy file and check's permissions: can flash access to this server or not. It's a simple security: to prevent flash doing request's where it shouldn't.
For more information just google by keyword crossdomain.xml.
I found the solution. I just had to use the following code.
var url:String = "php url";
var my_url:URLRequest = new URLRequest(url);
my_url.method = URLRequestMethod.POST;
my_url.data = my_vars;
var my_loader:URLLoader = new URLLoader();
my_loader.dataFormat = URLLoaderDataFormat.VARIABLES;
my_loader.load(my_url);

Action Script 3. How to get data from swf (as) to php and write to database

I'm creating Flash "memory" game, Idea to discover 2 equal cards. Here is timer which count time in how many seconds all cards are discovered. After complete game timer stops. How can I get this time from swf file and write to database? As I understand I need take this data with php file, but don't have idea how to do It.
As I know I need to do something like that:
var urlLoader:URLLoader = new URLLoader();
urlLoader.addEventListener(Event.COMPLETE, scoreSaveResponse,false,0,true);
var request:URLRequest = new URLRequest("http://mysite.com/score.php");
var urlVars:URLVariables = new URLVariables();
urlVars.time = timer.currentCount;
urlVars.userName = "yourUserName";
request.method = URLRequestMethod.POST;
urlLoader.data = urlVars;
urlLoader.load(request);
function scoreSaveResponse(e:Event):void {
//whatever you return from the php page is found in urlLoader.data
}
//LondonDrugs_MediaServices's code
But could someone explain me how to do It, because If I use this part of code nothing happens, I don't get any errors, but I don't know how to use this in php or how to write it to database.
My app will be used for Facebook, that's mean username will be used the same as in Facebook. So I think first i need to get time to PHP than get username from Facebook and write both to database.
Thank you for answers.
Try to use AMFPHP.
AMFPHP makes it possible to write a service for your application where you can store your SWF values into a PHP database.
http://www.silexlabs.org/amfphp/

Save html-page to MySQL DB after being modified with javascript

I want to save an entire HTML-page after it's being modified with Javascript. It's a week schedule, written by my predecessor, that has to be saved per user in a MySQL Database. I've been searching on this topic for hours but haven't found anything useful.
Does anyone have a clue if this is possible? And if so, how this can be done easily?
You can get actual page HTML with Javascript:
var html = document.getElementsByTagName('html')[0].innerHTML
And then make a POST request to serverside script which will save that page to database.
Obtain the source from the DOM with javascript:
document.getElementsByTagName('html')[0].innerHTML
Then send an Ajax request to your PHP script that stores the value into the database:
var formData = new FormData();
formData.append("html", document.getElementsByTagName('html')[0].innerHTML);
var oXHR = new XMLHttpRequest();
oXHR.open("POST", "http://foo.com/saveHTML.php");
oXHR.send(formData);
In your PHP script:
$html = $_POST['html'];
... # store it

Save An Array From SWF and Store Using Actionscript 3 and PHP

I have an AS3 project that takes user inputs (basically like a multiple choice test) and saves the these inputs in an array. I need to save the array to a text file so it can be reloaded when the app is reloaded. When the user returns to the application they can pick up where they let off.
I really just need to know what options I have for saving an array inside an swf to an xml file or text file in the same directory. Is this even possible.
Any ideas or concepts would be greatly appreciated.
thanks,
Laurence
First, you need to serialize your object to a string. Then, post that string to a PHP script via a URLLoader instance. Store it as desired (database, text file, etc). When you need to reconstruct the object, load the string from your PHP script. Unserialize it and it's ready to go.
For serialization and deserialization, here are helper functions:
private function serializeObject(o:Object):String
{
var ba:ByteArray = new ByteArray();
ba.writeObject(o);
return ba.toString();
}
private function unserializeObject(s:String):Object
{
var ba:ByteArray = new ByteArray();
ba.writeUTFBytes(s);
ba.position = 0;
return ba.readObject();
}
You could store your variables as "Flash cookies" using the SharedObject class. It's similar to the way PHP or Javascript store cookies locally on the user's compupter. I wrote this Session class a while back:
http://code.google.com/p/daleyjem/source/browse/trunk/com/daleyjem/as3/Session.as
It basically allows you to get, set and check if a set cookie exists.

Passing variable FROM flash to HTML/php

I was hoping maybe someone could provide some insight on a problem I'm having a tough time deciding how to solve.
I have a rather simple flash application users can make a quick username when connected, and the username is created inside the flash swf.
Now, I have a cron job deleting inactive usernames every ten minutes (on my mysql database where these usernames are all stored and accessed by the other people online) which is fine. But it can still get cluttered up if a bunch of people sign off at once, there is still that 10 minute window before the cron job clears them.
The users have an option to click log out in the flash application which is fine and works great. But of course many choose not to click log off they just click the browser x.
I've looked into onbeforeunload and jquery's .unload but I still need a way to get the username variable that's IN flash INTO the HTML, then use a php script to run the delete username mysql query.
Is there an easier solution?
If not, any insight on how I might pass the username variable to the html to hold onto it after the user makes their username so it can be involved with the .unload function running the php script?
EDIT::::: Maybe is there a way to create a UNIQUE string of numbers with php then pass that var to flash to include with the mysql row then since i already have that var since it was created on the html side, just along with the unload, have it delete the row that has that unique id?
If anyone things this idea would be the best approach, and if i used something like md5(uniqid(microtime()) . $_SERVER['REMOTE_ADDR'] . $_SERVER['HTTP_USER_AGENT']) to make a random iD how could i go about storing the result in a var i could place in the flash vars param then again in the jquery unload or javascript onbeforeunload if that would be better . im just more familiar with jquery
You could actually invoke an ExternalInterface command to populate a hiddenfield in your HTML coming from your SWF component.
if (ExternalInterface.available)
{
var js:String = "yourJavaScriptFunction";
var valToPass:String;
ExternalInterface.call(js(valToPass));
}
And in your HTML page, you write a javascript function:
<script language="javascript" type="text/javascript">
function yourJavaScriptFunction(valToPass)
{
document.getElementById('yourHiddenField').value = valToPass;
}
And from the unload event fired up by your page, you can access the value which was passed from your SWF.
Take note that you can call the javascript function from your SWF as soon as you get the login credentials of your user.
Hope this helps.
You want to use a combination of URLRequest, URLVariables and URLLoader to do this. See the below.
var myData:URLRequest = new URLRequest("some.php");
myData.method = URLRequestMethod.POST;
var variables:URLVariables = new URLVariables();
variables.username = 'CREATED USERNAME';
myData.data = variables;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.VARIABLES;
loader.addEventListener(Event.COMPLETE, dataOnLoad);
loader.load(myData);
function dataOnLoad(evt:Event){
trace('Completed');
}
This would then call the 'some.php' file with your username stored in '$_POST['username']'. Your php script can then make an impression on the db linking that username to that session (however you want to do that).

Categories