PEAR PHP XML Serializer and Flex

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

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 DisplayWe would create a HTTP Request tag in our Flex application like this:


And create a TileList to display the friends data:



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:


    51930230
    Derrick May
    derrickmay
    Derrick May
    Techno / House / Club
    14536
    64247
    2006-10-11

http://myspace-728.vo.llnwd.net/01007/82/77/1007837728_s.jpg

    DETROIT
    Michigan
    US
    2006-01-27


    63740153
    Plastikman
    plastikman
    Plastikman
    Techno / Electronica / Ambient
    1
    12955
    52482
    2006-10-11

http://myspace-786.vo.llnwd.net/00577/68/77/577847786_s.jpg

    Windsor
    Ontario
    Canada
    2006-03-18


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.

Leave a comment