1

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();
6
  • Have you tried setting the php header as xml? header( "content-type: application/xml; charset=ISO-8859-15" ); Commented Aug 12, 2015 at 17:23
  • Just tried this, as well as header( "content-type: text/xml"), both generated the same error as before :( Commented Aug 12, 2015 at 17:28
  • can you show the actuall output from the php action? Likely your getting something like this: <xml></xml>3, which is not valid xml and does not have an xml url parameter as AS3 is expecting something like this: xml=<xml></xml>&index=3 Commented Aug 12, 2015 at 17:36
  • There is no output in Flash from the php action except for the ReferenceError. When I use event.currentTarget.data, and xml is the only thing being echoed, the xml is outputted and well-formatted in flash. Commented Aug 12, 2015 at 17:55
  • @BadFeelingAboutThis: <?xml version="1.0"?> <presets> <colors>0xff0000</colors> <colors>0x00ff00</colors> <colors>0x0000ff</colors> </presets> 3' is what is outputted when I use .TEXT. When I create a new set of URLVariables inside my onComplete function, and access the .xml` property of the new URLVariables, I get null as my output. (Sorry, the xml in my comment is well-formed in the output) Commented Aug 12, 2015 at 18:32

1 Answer 1

1

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:

  1. Set the dataFormat property of the URLLoader to URLLoaderDataFormat.VARIABLES, so it knows how to parse the response as such.

  2. 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);

Sign up to request clarification or add additional context in comments.

6 Comments

So maybe I'm mis-interpreting the usage of loader.data in flash. If I have two variables that I want to send from PHP to Flash, one being an XML object (xml) and one being an integer (index), can I not access these elements separately via Flash? (i.e. event.currentTarget.data.xml, event.currentTarget.data.index). Am I only limited to accessing loader.data as a whole?
Thank you. I recall now seeing things like this. Would I echo this from PHP in a way similar to echo "$xml=$xml->saveXML()"; ?
yes, and then use the ampersand between any subsequent parameters. I haven't actually used PHP though in about a decade so I'm quite rusty there.
Thank you very much. It's still not working but I think this a step in the right direction, and I can hopefully figure out where to go from here.
It's working for me with the string: xml=<?xml version="1.0"?><presets><colors>0xff0000</colors><colors>0x00ff00</colors><colors>0x0000ff</colors></presets>&index=3 . Unfortunately I can't help too much with the PHP side
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.