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>http://myspace-728.vo.llnwd.net/01007/82/77/1007837728_s.jpg</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>http://myspace-786.vo.llnwd.net/00577/68/77/577847786_s.jpg</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.

原文取自: http://active6.wordpress.com/2006/11/06/using-the-pear-php-xml-serializer-class-with-flex/ 


arrow
arrow
    全站熱搜
    創作者介紹
    創作者 Frank 的頭像
    Frank

    經驗交流分享與備忘

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