How to send parameters via the URL to PHP from Flex HttpService

Ollie Jones picture Ollie Jones · Apr 25, 2012 · Viewed 7.1k times · Source

Forgive me if this has already been answered/ is extremely basic/ the question is worded incorrectly, I am very new to this and struggling.

Basically I have back end PHP which generates XML, the flash builder then inherits the data. Where I'm stuck is understanding how the flash builder can send a parameter to the PHP through an HttpService e.g

This is what it currently interprets:

http://..../file.php?action=getitems

What I would like the flash builder to send is

&class=fruit (<- the class would be dependant on what is selected from the drop down in the application)

to overall create this string

http://..../file.php?action=getitems&class=fruit

Thank you and apologies if this is nonsense. I'm using Flash Builder 4.

Answer

powelljf3 picture powelljf3 · Apr 25, 2012

Overall I would use the push method instead of passing a variable, lessens the chance of getting hacked from the middle.

My AS3 Code for the http call:

    public function someRequest() : void
    {
        var service : HTTPService = new HTTPService();
        service.url = "http://localhost/getData.php";
        service.useProxy = false;
        service.method = "POST";
        service.contentType = "application/xml";    // Pass XML data.
        service.request = "<ID>somevalue</ID>";     // The XML data.
        service.resultFormat = "xml";               // Recieve XML data.
        service.addEventListener(ResultEvent.RESULT, createFields);
        service.addEventListener(FaultEvent.FAULT, handleFault);
        service.send();
    }

    private function createFields(event : ResultEvent) : void
    {
        var result : String = event.result.toString();
        returnData = XML(result);
    }

    private function handleFault(event : FaultEvent) : void
    {
        var faultstring : String = event.fault.faultString;
        Alert.show(faultstring);
    }

As you see toward the middle, there is an XML space for entering a variable. I use this approach to pass data back and forth from the PHP to the AS3.

The PHP is:

<?php

define("DATABASE_SERVER", "localhost");
define("DATABASE_USERNAME", "root");
define("DATABASE_PASSWORD", "**");
define("DATABASE_NAME", "dbName");

//connect to the database.
$mysql = mysql_connect(DATABASE_SERVER, DATABASE_USERNAME, DATABASE_PASSWORD);

mysql_select_db(DATABASE_NAME);

$Query  = "SELECT * from data WHERE employeeID = '" . ($_POST['ID']) . "'";
$Result = mysql_query($Query);

$Return = "<data>";

while ($User = mysql_fetch_object($Result))
{
    $Return .= "<user><userid>" . $User->userid . "</userid><username>" . $User->username . "</username><emailaddress>" . $User->emailaddress . "</emailaddress></user>";
}
$Return .= "</data>";
mysql_free_result($Result);
print ($Return)
?>

Hope that helps you on your way.