Using the PEAR PHP XML Serializer class with Flex

Written by Bob de Wit. Posted in Developer Blog, PEAR

PEAR is a framework and distribution system for reusable PHP components. The code in PEAR is partitioned in “packages”. Each package is a separate project with its own development team, version number, release cycle, documentation and a defined relation to other packages (including dependencies). Packages are distributed as gzipped tar files with a description file inside, and installed on your local system using the PEAR installer.PEAR contains PHP classes that are perfect for serializing data to be passed to a Flex application. Unfortunately, there is currently no package that would allow automatic installation for a Flex/PHP developer that wants to use the PEAR XML Serializer functionality. In this article, I am going to describe how to do a local tweak and installation of the PEAR XML classes on a server that does not have PEAR pre-installed. Even if your server has PEAR installed, this approach will work.

Getting the Necessary PEAR Files

The PEAR files you will need for Flex are:
  • PEAR.php
  • XML_Serializer.php & XML_Unserializer.php
  • XML_Util.php
You can obtain these from the PEAR Package listings, but I have attached the modified files for you here.

Modifying the Include Statements

If you do not wish to reproduce the PEAR Package folder structure, you must edit the XML_Serializer.php and XML_Unserializer.php files for the path of the PEAR library: /** * uses PEAR error management */ require_once ‘PEAR.php’; /** * uses XML_Util to create XML tags */ require_once ‘XML_Util.php’;

Introducing the Serializer Class

The XML_Serializer.php contains a class definition that allows for a wide range of XML files to be generated from PHP variables, arrays and objects. The generation mode is controlled by passing an array of parameters that is passed to the constructor of the class. The most important parameters you should include are the overall container tag of the XML data and the item tag name.

Application Example

Let’s take an example. I’ve been working on something like this for the MySpace Page of a record label. Let’s say you want to display a list of your MySpace friends in your Flex application. You have a MySQL table with all your friends data, such as:
  • Friend Number
  • Friend Name
  • Image
  • etc

Getting the Friends data From MySQL

We would load this data into an array of objects in a PHP script called getfriends.php using the following code: //Connect to MySQL, assuming we have already //defined the Database Login Parameters as constants $dbLink = mysql_pconnect(DB_HOST, DB_USER, DB_PW); //Create an array to hold the friends query result $friendsList = array(); //Execute the MySQL Query and process into an array $sql = “SELECT * FROM friends”; $result = mysql_query($sql, $dbLink); if ($result) { while ($row = mysql_fetch_object($result)) { array_push($friendsList, $row); } mysql_free_result($result); }

Creating the Flex Request and Tile Display

We would create a HTTP Request tag in our Flex application like this: <mx:HTTPService id=”friendslist” url=”http://localhost/MySpace/php/getfriends.php” useProxy=”false” method=”GET” result=”friendHandler(event)”> </mx:HTTPService> And create a TileList to display the friends data: <mx:Panel id=”FriendsListPanel” title=”MySpace Friends”> <mx:TileList direction=”vertical” columnCount=”1″ rowHeight=”120″ dataProvider=”{friendslist.lastResult.friends.friend}” id=”FriendsTileList” itemRenderer=”FriendListItem” /> </mx:Panel>

Where it comes together: the dataProvider

As you can see, the TitleList uses the following dataProvider: friendslist.lastResult.friends.friend This means the expected XML format would be something like: <friends> <friend> <id>51930230</id> <userid>Derrick May</userid> <fullid>derrickmay</fullid> <fullname>Derrick May</fullname> <styles>Techno / House / Club</styles> <friendcount>14536</friendcount> <visitcount>64247</visitcount> <lastlogin>2006-10-11</lastlogin> <image></image> <city>DETROIT</city> <state>Michigan</state> <country>US</country> <joined>2006-01-27</joined> </friend> <friend> <id>63740153</id> <userid>Plastikman</userid> <fullid>plastikman</fullid> <fullname>Plastikman</fullname> <styles>Techno / Electronica / Ambient</styles> <friend>1</friend> <friendcount>12955</friendcount> <visitcount>52482</visitcount> <lastlogin>2006-10-11</lastlogin> <image></image> <city>Windsor</city> <state>Ontario</state> <country>Canada</country> <joined>2006-03-18</joined> </friend> </friends>

Getting the PEAR XML Serializer to generate the expected XML

In order to have the XML class generate code from our getfriends.php result array we initialize a Serializer with the following options and have it generate the XML based on the $friendsList array: $options = array( XML_SERIALIZER_OPTION_XML_DECL_ENABLED => false, XML_SERIALIZER_OPTION_DOCTYPE_ENABLED => false, XML_SERIALIZER_OPTION_INDENT => ” “, XML_SERIALIZER_OPTION_LINEBREAKS => “\n”, XML_SERIALIZER_OPTION_TYPEHINTS => false, XML_SERIALIZER_OPTION_XML_ENCODING => “UTF-8″, XML_SERIALIZER_OPTION_ROOT_NAME => “friends”, XML_SERIALIZER_OPTION_DEFAULT_TAG => “friend” ); //Instatiate the serializer object $serializer = new XML_Serializer($options); $serializer->setErrorHandling(PEAR_ERROR_DIE); //Serialze the data $result = $serializer->serialize($array, ‘friend’); $xml = $serializer->getSerializedData(); //Return the xml to the Flex application echo( $xml ); And that’s all there’s to it.

Trackback from your site.

Comments (10)

  • Chris Charlton


    Modified PEAR files are missing; please update the link. Thanks.


  • Kevin


    Can you please describe the friends table? I would like an efficient way to store friends.

    It appears that the table above has, and id, userid, fullid … joined, but how would you create a table that has users with the id of 2 that has 4 friends.

    Would the rows be user_id and friend_id ?

    And the table would look like this:

    |user_id | friend_id |
    1 2
    1 3
    1 4
    2 3
    2 4
    2 5
    6 1
    6 3

    Then the select would be:

    SELECT friend_id, user_id from friends
    WHERE user_id = 2 OR friend_id = 2;

    I would want the answer to be: User # 2′s friends are 1, 3, 4, and 5.

    Maybe I need to add “DISTINCT” to the select so I wont get duplicate ids?

    I think that would work, BUT is that how sites like myspace do it?



  • Kevin


    oh, that example above wont work because the user_id will have more that one instance and that should be the primary key. I don’t know how to create the proper table.


  • Kevin


    I figured it out.

    CREATE TABLE `friends` (
    `pri_key` int(11) NOT NULL auto_increment,
    `user_id` int(11) NOT NULL default ’0′,
    `friend_id` int(11) NOT NULL default ’0′,
    `updated` timestamp NOT NULL default CURRENT_TIMESTAMP,
    PRIMARY KEY (`pri_key`)

    SELECT friend_id, user_id from friends WHERE user_id = 86 OR friend_id = 86;

    | friend_id | user_id |
    | 53 | 86 |
    | 17 | 86 |
    | 58 | 86 |
    | 86 | 90 |


  • Wayne Scholar



    Thanks for this. I was wondering if you’d be interested in doing some consulting for us – we are working on porting our app over to ec2.

    Please contact me via email ( or phone (412-855-8765)




  • Convert PHP Array to XML | diamondTearz


    [...] heavy traffic.  Also would like to note that there is a Pear XML Serializer class as pointed out in this post that does the same conversion but with the peace of mind  of being an actual part of the library. [...]


  • diamondTearz » Convert PHP Array to XML


    [...] heavy traffic.  Also would like to note that there is a Pear XML Serializer class as pointed out in this post that does the same conversion but with the peace of mind  of being an actual part of the [...]


  • Convert PHP Array to XML


    [...] Also would like to note that there is a Pear XML Serializer class as pointed out in this post that does the same conversion but with the peace of mind  of being [...]


  • George Harris


    The Zune concentrates on being a Portable Media Player. Not a web browser. Not a game machine. Maybe in the future it’ll do even better in those areas, but for now it’s a fantastic way to organize and listen to your music and videos, and is without peer in that regard. The iPod’s strengths are its web browsing and apps. If those sound more compelling, perhaps it is your best choice. Webmaster of the php framework


Leave a comment