HTTPService: call a php script passing parameters and receive a response from server

How to send an user name from Flex to the PHP file and receive its email address
(naturally you could make a query to the db to get these values)
<?xml version="1.0" encoding="utf-8"?>
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
layout="absolute"
creationComplete="init(event)" >


<mx:Script>
<![CDATA[

import mx.events.FlexEvent;

private const NOME_UTENTE:String = "Fabio Biondi";

private function init(evt:FlexEvent):void
{
phpRPC.url="assets/Es02_CheckUser.php";
phpRPC.send({ utente: NOME_UTENTE} )
}


private function viewEmail():void
{
outputArea_txt.text = phpRPC.lastResult.email
}

]]>
</mx:Script>


<mx:HTTPService
id="phpRPC"
resultFormat="flashvars"
result="viewEmail()"
/>


<mx:TextArea
id="outputArea_txt" y="20"/>


</mx:Application>


// PHP CODE
<?php

switch($_REQUEST['utente'])
{
case "Fabio Biondi":
echo "email=info@fabiobiondi.com";
break;

default:
echo "email=User not found";
break;
}

?>

HTTPService and basic authentication

suppose your HTTPService goes to a server that need basic authentication, you'll be prompted EACH time with a pop-up, fill with user and password. This can be very annoying specially in long dev/debug session.
setCredentials and setRemoteCredentials for different reasons, don't work with "simple" http auth.
Solution:
- add custom header and send it before any further request
how to implement:
- define your HTTPService as you desidred (mxml, as)
- add initialization code as shown below:
<?xml version="1.0"?>
<!--
/**
* @author jaco
*
* created on 17/10/2009 16.51.03
*/
-->
<mx:Application
xmlns:mx="http://www.adobe.com/2006/mxml"
creationComplete="init_app()">

<mx:Script>
<![CDATA[

import mx.utils.Base64Encoder;
import mx.rpc.events.FaultEvent;
import mx.rpc.events.ResultEvent;


[Bindable]
private var SERVER_APP_URL:String = "http://your.auth_server.com/your_app"; // accommodate with your real url

/**
* application initialization
*/
internal function init_app():void {

var b64:Base64Encoder = new Base64Encoder();
b64.insertNewLines = false;
b64.encode("youruser:yourpwd"); // credentials must be base64 encoded, change to fit your needs

abService.headers = {Authorization:"Basic " +b64.toString()};
abService.send();

// ...
}

/**
* event triggered on service error
* @param error event
*/
internal function onServiceFault(evt:FaultEvent):void {
/* TODO */
trace(evt.type, evt.fault);
// your code here
}

/**
* event triggered on service result
* @param result event
*/
internal function onServiceResult(evt:ResultEvent):void {
/* TODO */
// your code here
}
]]>
</mx:Script>

<mx:HTTPService id="abService"
url="{SERVER_APP_URL}"
method="POST"
resultFormat="e4x"
fault="onServiceFault(event)"
result="onServiceResult(event)" />

<mx:Panel title="" height="100%" width="100%"
paddingTop="5" paddingLeft="5" paddingRight="5" paddingBottom="5">

</mx:Panel>
</mx:Application>

Use a PHP PROXY to bypass security issues in Flex/Flash

Reading RSS feeds from Flex or Flash often generate some security issues when the origin domain doesn't contain a crossdomain.xml file to manage the policy settings.

A little workaround is create a PHP proxy to read the feeds and pass the content to the Flex script.

In few words, instead to call the original xml feed
<mx:HTTPService id="loadXML"
url="http://www.hwupgrade.it/rss_hwup.xml "
...

you'll call the php script passing the url property
<mx:HTTPService id="loadXML"
url="http://localhost/path/rssReader.php?url=http://www.hwupgrade..."
...
<?php
readfile($_REQUEST["url"]);
?>