Sending and Receiving Data from Flash As3 to PHP

Sending and receiving data from Flash AS3 to PHP

Your AS code seems to be right. So the problem might be in PHP. Please test first with this PHP file:

<?php
echo "test=1&done=true";
?>

This should then let your movie trace "true".
You then should debug your PHP. print_r($_POST); destroys your output of course. May be you did forget to remove this debugging statement :-)

@Jesse and @Ascension Systems, check the docs for URLVariables: http://livedocs.adobe.com/flash/9.0_de/ActionScriptLangRefV3/flash/net/URLVariables.html

using AS3 to send data to a php file

Your AS3 code is fine, but in your PHP code you should do like this :

<?php

$psPreRegEmail = $_POST['sEml'];
$FRM_ID = $_POST['sID'];
$psBD = $_POST['sBD'];

echo "email=".$psPreRegEmail."&id=".$FRM_ID."&db=".$psBD;

?>

About your question, why you don't get what you want when you open http://www.mysite.fr/login.php ? This is because your $_POST array is empty and then your vars are empty. You should just run your project and you will get this :

vars.email: steph4
vars.id: steph5
vars.db: steph6

Sending data from flash to php using post method as2

This is probably the best tutorial on HTTP requests in AS2: http://www.sephiroth.it/tutorials/flashPHP/loadVars/

Sending data from Flash to PHP using POST method

Not sure about how to convert your image into data and such, but here's a class I have lying around that you can use to transfer data to a PHP script (which can from there insert the data into a database).

package
{
import flash.net.URLRequest;
import flash.net.URLLoader;
import flash.net.URLVariables;
import flash.net.URLRequestMethod;
import flash.events.Event;

/**
* @author Marty Wallace
* @version 1.00
*/
public class PHPData extends Object
{
/**
* Sends data to a PHP script
* @param script A URL to the PHP script
*/
public function send(script:String, vars:URLVariables):void
{
var req:URLRequest = new URLRequest(script);

req.data = vars;
req.method = URLRequestMethod.POST;

var loader:URLLoader = new URLLoader();
loader.load(req);

// listeners
loader.addEventListener(Event.COMPLETE, _complete);
}

/**
* Called when a response has been received from a PHP script
* @param e Event.COMPLETE
*/
private function _complete(e:Event):void
{
var vars:URLVariables = new URLVariables(e.target.data);

var i:String;
for(i in vars)
{
trace(i + ": " + vars[i]);
}

e.target.removeEventListener(Event.COMPLETE, _complete);
}
}
}

Use:

var php:PHPData = new PHPData();
var vars:URLVariables = new URLVariables();

vars.imagedata = your_image_data;

php.send("your_php_script.php", vars);

How can I send a ByteArray (from Flash) and some form data to php?

it's a little tricky: You can do it one of two ways:

Arthem posted this in another thread:

This was of great help to me:
http://www.quietless.com/kitchen/upload-bitmapdata-snapshot-to-server-in-as3/

You need to modify the
URLRequestWrapper to insert field
names and file names where needed.
Here's what I've done:

bytes = 'Content-Disposition:
form-data; name="' + $fieldName + '";
filename="';

It does the most formatting of headers
so the server could understand it as a
file upload.

By the way, if you have a BitmapData
you might need to encode it to JPEG or
PNG first.

And I usually use this solution:

I haven't used the imageshack API, but you may want to try using adobe's JPGEncoder class - here's a quick example that passes a username and the JPG's byte array, it's really quite simple.

private function savePicToServer(bmpData:BitmapData):void
{
var jpgEncoder:JPGEncoder = new JPGEncoder(85);
var jpgStream:ByteArray = jpgEncoder.encode(bmpData);

var loader:URLLoader = new URLLoader();
configureListeners(loader);

var header:URLRequestHeader = new URLRequestHeader("Content-type", "application/octet-stream");
var request:URLRequest = new URLRequest(ModelLocator.BASE_URL + ModelLocator.UPLOAD_URL + "?user=" + ModelLocator.getInstance().username);
request.requestHeaders.push(header);
request.method = URLRequestMethod.POST;
request.data = jpgStream;
loader.load(request);
}

Note that the variables are passed as part of the query string. You can't use URLVariables, as several people have suggested, as the URLVariables would be stored in the request's data property, which we are already using to pass the byteArray.

navigateToURL sending data via POST to php page

in AS 3 the URLRequest class you use to specify your request has a method property which can be used to set he HTTP option for submission method, you'll need to set it to POST using URLRequestMethod constant POST for perfect form or you could use a "POST" string.

You can find a comprehensive example on snipplr

so in a nutshell:

var url:String = "http://localhost/myPostReceiver.php";
var request:URLRequest = new URLRequest(url);
var requestVars:URLVariables = new URLVariables();
requestVars.foo = "bar";
// ... fill in your data
request.data = requestVars;
request.method = URLRequestMethod.POST;
// after this load your url with an UrlLoader or navigateToUrl

When using Adobe Air You'd need to use the URLLoader class instead of navigateToURL() because of the following tidbit:

Parameters
request:URLRequest — A URLRequest object that specifies the URL to navigate to.

For content running in Adobe AIR, when using the navigateToURL() function, the runtime treats a URLRequest that uses the POST method (one that has its method property set to URLRequestMethod.POST) as using the GET method.

Basically whenever you want to use POST set method correctly, as also indicated by the documentation for navigateToUrl:

next in php you'd be receiving the variable in the superglobal $_POST array, where you could access it as such:

<?php
$foo = $_POST['foo'];
/* $foo now contains 'bar'
assignment to another value is not necessary to use $_POST['foo']
in any function or statement
*/


Related Topics



Leave a reply



Submit