Posted by tfair in SWF Studio V3 on Feb 04 2011, 01:24 pm
Basic question: is it possible to connect with a remote script from a Swf Studio desktop application?
I've set up an app that calls a php script to insert some data in a database, using the usual code:
var senduserinfo:URLVariables = new URLVariables();
senduserinfo.first = f;
senduserinfo.last = l;
var request:URLRequest=new URLRequest();
request.url="url of php file";
request.method=URLRequestMethod.POST;
request.data=senduserinfo;
var loader:URLLoader=new URLLoader();
loader.dataFormat=URLLoaderDataFormat.VARIABLES;
etc.
This works fine locally, as I test it using my own local machine server. But when I call the same script as set up on a remote site, it doesn't do anything. Is this a security issue, or do I need to use the http plugin to make this work? Any help appreciated.
Posted by northcode in SWF Studio V3 on Feb 04 2011, 04:48 pm
What you're trying to do has nothing to do with SWF Studio, so what happens when you try that from a standalone Flash EXE?
If it works with a local HTTP server it should work with any HTTP server unless the remote site is asking for authentication, you're trying to access the wrong resource, there was a timeout, your network is blocking the traffic, basically it could be anything.
You're going to need to look at what comes back from your request, i.e. the HTTP response codes or the actual data returned from the script or the server.
You can use the HTTP plugin to do this, it's just like accessing any other resource. You ask for the resource and wait for a response from the server. We've posted plenty of examples here over the years...
Posted by tfair in SWF Studio V3 on Feb 04 2011, 05:17 pm
Thanks, its good to know that if it works locally it should work remotely. I did try one of the examples - it was called http1 but was in AS1 and didn't work. I'll do another search and find what I can about the http plugin.
Posted by northcode in SWF Studio V3 on Feb 04 2011, 05:29 pm
This example calls a perl script at northcode.com and writes the results to a file called result.html (the perl script returns HTML) and then it open the HTML file.
ssCore.init();
ssDefaults.synchronousCommands = true;
data = "data=" + escape("put whatever you want in here, but your server side script has to parse it!");
ssCore.HTTP.host({host:"northcode.com"});
ssCore.HTTP.method({method:"POST"});
ssCore.HTTP.port({port:"80"});
ssCore.HTTP.resource({resource:"/cgi-bin/test.pl"});
ssCore.HTTP.protocol({protocol:"HTTP"});
ssCore.HTTP.request({data:data});
ssCore.HTTP.headers({headers:"Content-Type: application/x-www-form-urlencoded\n\n"});
ssCore.HTTP.start({});
ssCore.HTTP.read({}, {callback:onRead});
function onRead(return_obj, callback_obj, error_obj)
{
var r = ssCore.HTTP.getBuffer({});
ssCore.FileSys.writeToFile({path:"startdir://result.html", data:r.result});
ssCore.Shell.open({path:"startdir://result.html"});
ssCore.HTTP.stop({});
}
The same code will work with PHP, just change the resource. If your script doesn't take URL encoded form data, change that header to whatever you need.
Error checking code is left as an exercise for the reader :evilgrin:
Posted by tfair in SWF Studio V3 on Feb 07 2011, 11:30 am
Thanks for this example. My apologies: our server firewall had been blocking the remote call. I tried this on my home laptop with success.