p2p.wrox.com Forums

Need to download code?

View our list of code downloads.


  Return to Index  

beginning_php thread: PHP sending XML objects


Message #1 by "KayKay-GG \(E-mail\)" <kaykay@g...> on Thu, 12 Sep 2002 11:52:00 +0530
Hey there,
	This is the scenario: I send a few name-value pairs to a script, and it
replies with a few more name-value pairs. Everything works fine... But is
there anyway that I can get PHP to reply with an XML object?? Or better.. I
would like to completely stop using name-value pairs, and just pass
dynamically created XML objects around. Is that possible? Most of the info
on the web deals primarily with parsing an XML file off the webserver, but
that's not really what I'm looking for... I would appreciate any ideas that
you guys have... or if you could point me to a resource on the web it will
be great :)
Cheers and thanks in advance :)
-K

Message #2 by "Nikolai Devereaux" <yomama@u...> on Thu, 12 Sep 2002 10:29:39 -0700
Well, you can use PHP to generate any text you want, be it HTML or whatever.

If you want to generate XML, you can do that, too.  It's actually pretty
simple to write a class or some utility functions to generate XML for key
value pairs.

It all depends on the XML tag and attribute schema you want to use.

At it's simplest form:

function array_to_xml($array)
{
  foreach($array as $key => $value)
  {
    $xml .= "<$key>$value</$key>\n";
  }
  return $xml;
}


To handle nested arrays:

function array_to_xml($array, $indent = 0)
{
   foreach($array as $key => $value)
   {
      $xml .= str_repeat('  ', $indent);
      $xml .= "<$key>";

      if(is_array($value))
      {
        $xml .= "\n" . array_to_xml($value, $indent + 1);
      }
      else
      {
        $xml .= $value;
      }

      $xml .= "</$key>\n";
   }

   return $xml;
}


As far as passing XML objects around, where are you passing these objects
to?  If you're just going between PHP scripts, it really doesn't make a
whole lot of sense to add a bunch of unnecessary processing.

More plainly, why encode all your key value pairs in XML if in order to use
them, you need to create an XML parser and dump everything back into key
value pairs?


If I am missing the mark with what you intend, please let me know.  Be a bit
more specific about what you want to do -- for example, give me some sample
key/value pairs, what you want the xml object to look like, where you're
passing things from and where they're going, etc..


take care,

nik

Message #3 by "KayKay-GG \(E-mail\)" <kaykay@g...> on Fri, 13 Sep 2002 00:45:28 +0530
Hey there Nik,
	Thanks for all the information: As you rightly guessed mate, I'm having
trouble with passing XML objects around: I'm currently just testing XML out,
so I don't really have any concrete examples to present. But what I am
essentially trying to do is use Flash ActionScript to communicate with PHP
using XML... something like this:
1) I have a Flash document that user A enters details into
2) Flash constructs an XML object and calls a PHP script with it. The
inbuilt Flash XML function waits for a reply (in hte form of an XML object)
from the PHP script.
3) The PHP script parses the XML that it has received, processes it, forms a
reply/ack and sends it back to Flash.

I don't know how to handle the XML objects in PHP esp. how to send an XML
object back to Flash. I've tried forming an XML style string like "<?xml
version=\"1.0\"?><TEST name=\"asd\">testing</TEST>" and passing it back like
a name-value pair, but have had no luck so far - oh, and I've also switched
off the short tags option in the PHP.ini file to enable XML-style tags in
the script.

Thanks bud,
-K

>>>>-----Original Message-----
>>>>From: Nikolai Devereaux [mailto:yomama@u...]
>>>>Sent: 12 September 2002 23:00
>>>>To: beginning php
>>>>Subject: [beginning_php] RE: PHP sending XML objects
>>>>
>>>>
>>>>
>>>>Well, you can use PHP to generate any text you want, be it
>>>>HTML or whatever.
>>>>
>>>>If you want to generate XML, you can do that, too.  It's
>>>>actually pretty
>>>>simple to write a class or some utility functions to
>>>>generate XML for key
>>>>value pairs.
>>>>
>>>>It all depends on the XML tag and attribute schema you want to use.
>>>>
>>>>At it's simplest form:
>>>>
>>>>function array_to_xml($array)
>>>>{
>>>>  foreach($array as $key => $value)
>>>>  {
>>>>    $xml .= "<$key>$value</$key>\n";
>>>>  }
>>>>  return $xml;
>>>>}
>>>>
>>>>
>>>>To handle nested arrays:
>>>>
>>>>function array_to_xml($array, $indent = 0)
>>>>{
>>>>   foreach($array as $key => $value)
>>>>   {
>>>>      $xml .= str_repeat('  ', $indent);
>>>>      $xml .= "<$key>";
>>>>
>>>>      if(is_array($value))
>>>>      {
>>>>        $xml .= "\n" . array_to_xml($value, $indent + 1);
>>>>      }
>>>>      else
>>>>      {
>>>>        $xml .= $value;
>>>>      }
>>>>
>>>>      $xml .= "</$key>\n";
>>>>   }
>>>>
>>>>   return $xml;
>>>>}
>>>>
>>>>
>>>>As far as passing XML objects around, where are you passing
>>>>these objects
>>>>to?  If you're just going between PHP scripts, it really
>>>>doesn't make a
>>>>whole lot of sense to add a bunch of unnecessary processing.
>>>>
>>>>More plainly, why encode all your key value pairs in XML if
>>>>in order to use
>>>>them, you need to create an XML parser and dump everything
>>>>back into key
>>>>value pairs?
>>>>
>>>>
>>>>If I am missing the mark with what you intend, please let
>>>>me know.  Be a bit
>>>>more specific about what you want to do -- for example,
>>>>give me some sample
>>>>key/value pairs, what you want the xml object to look like,
>>>>where you're
>>>>passing things from and where they're going, etc..
>>>>
>>>>
>>>>take care,
>>>>
>>>>nik


  Return to Index