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

Creating the Flex Request and Tile Display

We would create a HTTP Request tag in our Flex application like this:

<mx:HTTPService id=”friendslist”

And create a TileList to display the friends data:

<mx:Panel id=”FriendsListPanel” title=”MySpace Friends”>
<mx:TileList direction=”vertical”
itemRenderer=”FriendListItem” />

Where it comes together: the dataProvider

As you can see, the TitleList uses the following dataProvider:


This means the expected XML format would be something like:

<userid>Derrick May</userid>
<fullname>Derrick May</fullname>
<styles>Techno / House / Club</styles>
<styles>Techno / Electronica / Ambient</styles>

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(

//Instatiate the serializer object
$serializer = new XML_Serializer($options);

//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.



    Frank 發表在 痞客邦 留言(0) 人氣()