navigateToURL is not sending POST

3

I've been trying to send parameters from actionscript to php , I've checked the following page:

Send variables using POST

I made it right, but at the moment it runs navigateToURL(urlRequest, '_self'); , it always shows erro instead of the expected VALOR DE TESTE .

I've researched a lot of information but only shows results from 2007 down. Does anyone know how to pass a simple parameter from flash to php (using POST instead of GET ).

    
asked by anonymous 08.04.2015 / 19:48

1 answer

3

When you run script with Adobe Flash Professional IDE instead of the browser the program will convert POST to GET when using navigateToURL(...); , for this reason you will never get VALOR DE TESTE because it forces you to use GET

Using POST with navigateToURL

Note that to use POST is required (in addition to req.method = URLRequestMethod.POST; )

  • Change content-type to req.contentType = "application/x-www-form-urlencoded";
  • And encode the variables thus variables.valor = encodeURIComponent("VALOR DE TESTE");

Should be something like:

var url:String = "http://localhost/post-test.php";
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();

variables.valor = encodeURIComponent("VALOR DE TESTE");

req.method = URLRequestMethod.POST;
req.contentType = "application/x-www-form-urlencoded";
req.data = variables;

navigateToURL(req, "_blank");

So in order for navigateToURL to work with POST you will have to put swf embedded in an HTML (assuming the file name is example.html ):

<object type="application/x-shockwave-flash" data="meu-swf.swf"></object>

and run this HTML through a server like Apache, Ngnix or lighttpd by going to:

http://localhost/exemplo.html

A message will pop up stating that the pop-up has been blocked, but then release as requested by the browser to test.

Using POST with URLLoader.load

navigationToURL uses pop-up and this can be a problem, mainly because of the blockers, I recommend doing the process using only Flash and ActionScript , in other words we will not open a new window, a This is done using URLLoader.load , code example (do not mix with the other script):

var url:String = "http://localhost/post-test.php";
var req:URLRequest = new URLRequest(url);
var variables:URLVariables = new URLVariables();

variables.valor = encodeURIComponent("VALOR DE TESTE");

req.method = URLRequestMethod.POST;
req.contentType = "application/x-www-form-urlencoded";
req.data = variables;

var urlLoader:URLLoader = new URLLoader();
urlLoader.dataFormat = URLLoaderDataFormat.TEXT;
urlLoader.addEventListener(Event.COMPLETE, completo, false, 0, true);
urlLoader.addEventListener(IOErrorEvent.IO_ERROR, ioError, false, 0, true);
urlLoader.load(req);

function completo(e:Event):void
{
    trace(urlLoader.data); // output: test
}

function ioError(e:IOErrorEvent):void
{
    trace(e);
}
  

Note: The use of IOErrorEvent.IO_ERROR is required

In order to test this code (both with load and navigateToURL ), you can use var_dump or print_r in your PHP, it would be a simple and manual debugging type, follow PHP:

<?php
echo 'GET:', PHP_EOL;
print_r($_GET);

echo 'POST:', PHP_EOL;
print_r($_POST);
    
08.04.2015 / 21:13