Reputation: 131
I am sending some information from PHP to flash, and sending some different information from PHP back to flash. I'm sending a user ID to PHP, and sending an XML variable, as well as an index, back to Flash.
Flash code:
var myLoader:URLLoader = new URLLoader();
var myRequest:URLRequest = new URLRequest(/*private info*/);
myRequest.method = URLRequestMethod.POST;
var memberInfo:URLVariables = new URLVariables();
memberInfo.member_id = 1817;
myRequest.data = memberInfo;
myLoader.addEventListener(Event.COMPLETE, onXMLLoad);
myLoader.load(myRequest);
function onXMLLoad(event:Event)
{
var newXML:XML = new XML(event.currentTarget.data.xml);
var index:int = new int(event.currentTarget.data.index);
}
PHP:
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$presets = $xml->createElement("presets");
$red = $xml->createElement("colors", "0xff0000");
$green = $xml->createElement("colors", "0x00ff00");
$blue = $xml->createElement("colors", "0x0000ff");
$presets->appendChild($red);
$presets->appendChild($green);
$presets->appendChild($blue);
$xml->appendChild($presets);
$index = 3;
echo $xml->saveXML();
echo $index;
When I do this, I get this error:
ReferenceError: Error #1069: Property xml not found on String and there is no default value.
How can I fix this? I'm thinking it has something to do with the XML actually being a string, but I'm not quite sure.
Here is the output from PHP:
<?xml version="1.0"?>
<presets>
<colors>0xff0000</colors>
<colors>0x00ff00</colors>
<colors>0x0000ff</colors>
</presets> 3
Note: If I delete the index variable from PHP and just have "xml" as the only thing being sent to Flash, I can access it using "event.currentTarget.data" and it will work fine.
Flash:
var myLoader:URLLoader = new URLLoader();
var myRequest:URLRequest = new URLRequest(/*private info*/);
myRequest.method = URLRequestMethod.POST;
var memberInfo:URLVariables = new URLVariables();
memberInfo.member_id = 1817;
myRequest.data = memberInfo;
myLoader.addEventListener(Event.COMPLETE, onXMLLoad);
myLoader.load(myRequest);
function onXMLLoad(event:Event)
{
var newXML:XML = new XML(event.currentTarget.data);
}
PHP:
$xml = new DOMDocument("1.0");
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$presets = $xml->createElement("presets");
$red = $xml->createElement("colors", "0xff0000");
$green = $xml->createElement("colors", "0x00ff00");
$blue = $xml->createElement("colors", "0x0000ff");
$presets->appendChild($red);
$presets->appendChild($green);
$presets->appendChild($blue);
$xml->appendChild($presets);
echo $xml->saveXML();
Upvotes: 1
Views: 54
Reputation: 14406
The issue is with your output from PHP.
The output shown, is not valid xml (because of the 3
at the end).
That is why it works when you remove the index.
To rectify it, either put the index in the xml (as an attribute to the root presets
node perhaps), or parse the output prior to converting it to an XML object in AS3, like so:
var response:String = event.currentTarget.data;
var index:String = response.substr(response.lastIndexOf(">") + 1);
var newXML:XML = new XML(response.substring(0, response.lastIndexOf(">") + 1));
If you want to be able to access variables as objects on the data property (eg like event.currentTarget.data.xml
& event.currentTarget.data.index
, then you need to do the following:
Set the dataFormat
property of the URLLoader to URLLoaderDataFormat.VARIABLES
, so it knows how to parse the response as such.
Send from PHP a url variable formatted string. So your response from php would need to look like the following:
xml=<xml></xml>&index=3
Where <xml></xml>
is replaced with your full xml. Keep in mind, all data will need to follow the url variable formatting rules, so you may need to use escape
(or rawurlencode
in PHP) and unescape
(in AS3) on the values
here is what I think the php should look like:
echo "xml=".$xml->saveXML();
echo "&index=".$index;
Easier though, may be to just use JSON and create an object in PHP and do echo json_encode(obj)
, then in flash do var response:Object = JSON.parse(event.currentTarget.data);
Upvotes: 1