Link to home
Start Free TrialLog in
Avatar of iamcmag
iamcmag

asked on

Calling a PHP function from Flash AS2

I have a problem calling a PHP function from Flash. Using the technique of triggering the function using a variable (see: http://www.tutorialized.com/view/tutorial/Calling-PHP-Functions-from-Flash/2333 ) does not work on my server, it throws a fatal error: "Fatal error: Function name must be a string in phpTest3.php on line 14". The final work will not rest on this server either and I need a stable script.

The PHP script must be in a function. If you can get any PHP function to trigger and print a variable from Flash on screen I should be able to do the rest myself. I trigger the AS that calls the PHP function from a button in the swf-file.

My test file: http://squaregrain.com/clients/ee/phpTest3.php

This is pressed for time.

Regards,
Andreas.
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>PHP Test</title>
</head>
 
<body>
<?php
	//$triggerVar = "flashFunction";
	function flashFunction() {
		echo("This is PHP!!!");
	}
	$triggerVar();
?>
</body>
</html>

Open in new window

Avatar of Aneesh Chopra
Aneesh Chopra
Flag of India image

Flash can't call a PHP function as you are expecting to achieve.

Although you should call the PHP with querystring parameters or POST variables.

and PHP should react based on received GET/POST variables and return the response to flash

you should use LoadVars.sendAndLoad() method.


here is a link which has few sample code on how to communicate with server-side scripting using Flash actionscript

http://www.flashadvisor.com/tutorial/ActionScript/LoadVars_Class_in_Flash-30-1.html
You need to bridge the gap between Client-side scripting and Server-side scripting... and while Flash can do that in a Number of ways...  as Aneesh has mentioned above, it is not going to happen with just a call to the page...  

It seems like a backwards way to make something work, but just in case this is your only option... you can use ExternalInterface to call a JavaScript function... which then uses AJAX to communicated with your PHP scripts...

like I said though... this seems backwards, but there is a way to do it...

rp / ZA
Avatar of iamcmag
iamcmag

ASKER

Thx, guys. I have previously tried ExternalInterface.call to trigger a JS that loads the PHP page which then does its thing and auto closes, but this is not a pretty way to do it nor a good one since the parameters got fracked up while being passed back and forth.

I'll look into this and post back.

Do you now if it's possible to use LoadVars.sendAndLoad on another page (just resting on the server) than the Flash container? If it was possible to just send vars to another page that executes the script without displaying the page it wouldn't have to be in a function.

/A
ASKER CERTIFIED SOLUTION
Avatar of rascalpants
rascalpants
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I have written code that should do what you want using sendAndLoad.  You can view it working at http://www.craybe.com.au/testflash.html

You can download the source files from:  http://www.craybe.com.au/ee-sendvar.zip

There isn't a lot to it so it should hopefully explain itself but if you need further information feel free to ask and I will comment the code.

Code is as follows:
//*************************************************//
//********************FLASH CODE*******************//
//*************************************************//
 
buttontest.onRelease = function(){
	sendvar = new LoadVars();
        //The Variable we will be sending to php to trigger the function
	sendvar.my_func_name = "flashFunction";
        //Call the php sending the variables of sendvar
	sendvar.sendAndLoad("phpTest3.php", sendvar, "POST" );
        //Run after php has been loaded
	sendvar.onLoad = function(success){
                //if php file is loaded correctly
		if(success){
                        //display the returned text for &returnedfromphp
			returnedtext.text = this.returnedfromphp;
		}else{
                        //display fail message
			returnedtext.text = "PHP failed";
		}
	}
}
 
//*************************************************//
//**********************PHP CODE*******************//
//*************************************************//
(Note you do not need the html header, body etc in the php file just the below code)
 
<?php
     //retrieve function name from flash
     $triggerVar = $_POST["my_func_name"];
     function flashFunction(){
              //return result to flash, you can return multiple vars
              //just seperate them with an &
              echo("&returnedfromphp=This is PHP!!!");
     }
     $triggerVar();
?>

Open in new window

Since you are using AS 2.0, and you have the capabilities to use a class-based solution, I would recommend that.

It is poor programming practice to nest function inside of other functions...  so at the very least, you should seperate those out.

rp / ZA
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
that, or you, Craybe, could move the functions outside of the onRelease method for our Author... then he and you have a much cleaner solution.

;)

rp / Zone Advisor
Isn't that what I did in the code snippet of my last post? ;-)  that or I'm misunderstanding you, which is possible, I'm working on a VB project and I haven't worked in AS2 or PHP for months hehe
actually... no...  I was on EE mobile and didn't see the code snippet link... so I didn't even see that part of your post...  serves me right for posting a comment from my phone  ;)

rp / Zone Advisor
Avatar of iamcmag

ASKER

I've been tied up in another urgent project so I will dig into this again ASAP.

Thx for the posts so far.

/A
Avatar of iamcmag

ASKER

Sorry for the delayed response but I got caught up in another even more pressing project.

I have solved the problem and it was both my uncertainty of the LoadVars.sendAndLoad function as well as poorly written php code that was the problem (didn't know I had to use $_POST to get the vars). Rascalpants pointed me in the right direction by explaining that I could run the php code right off the server without having it as the Flash container, and Craybe provided a solution to triggering php inside a function which was how initially tackled the problem. So a split points solution seems fair.

Thx for the help guys :)

/A