为PHP生成XML Web服务数据时的最佳实践

时间:2009-12-05 17:44:49

标签: php xml fusioncharts

我目前正在我的项目中使用Fusioncharts Free,它要求XML采用特定的格式,既可以显示数据,也可以显示图表的外观。

我有一个名为webservice.php的PHP文件,其中有许多类似的重复:

$address = $_SERVER['PHP_SELF'];

...

if ($address == 'webService.php/fcf/last5pricechanges/'){
//query MySQL DB and build up XML string in $output variable to output to browser
//building up each line of XML more or less line by line
}

输出的XML可能如下所示:

<graph caption="Active Items - Grouped by Site" showNames="1" decimalPrecision="0" bgcolor="eeeeee">
<set name="xyz.co.uk" value="1"/>
<set name="abc.com" value="5"/>
</graph>

更新此代码正在成为一个维护噩梦,但是由于Fusioncharts需要特定的XML属性,因此无法使用标准格式(例如REST)传输数据。是否有更好的方法来处理上述问题?我可以轻松地提高代码的模块性和可维护性吗?

1 个答案:

答案 0 :(得分:2)

我有几个你可以考虑的想法。

<强>#1
使用模板引擎(如Smarty)可能会使维护更容易一些。它至少会从PHP代码中删除XML。

例如,您可以为您发布的XML代码段创建一个模板:

<?xml version="1.0" encoding="UTF-8" ?>
{foreach from=$graphs item=graph}
    <graph caption="{$graph.caption}" showNames="{$graph.show_names}" decimalPrecision="{$graph.decimal_precision}" bgcolor="{$graph.bg_color}">
    {foreach from=$graph.set item=set}
        <set name="{$set.name}" value="{$set.value}"/>
    {/foreach}
    </graph>
{/foreach}

从PHP中调用它

<?php
$address = $_SERVER['PHP_SELF'];

$smart = new Smarty();
$graphs = array();

if ($address == 'webService.php/fcf/last5pricechanges/')
{
    $graph_result = mysql_query("SELECT caption, show_names, decimal_precision, bg_color
                                 FROM graph WHERE something='something else'");
    while($graph_row = mysql_fetch_assoc($graph_result))
    {
        $graph_row;
        $set_result = mysql_query("SELECT name, value FROM set WHERE graph_id = {$graph_row['id']}");
        while($set_row = mysql_fetch_assoc($set_result))
        {
            $graph_row['sets'][] = $set_row;
        }
        $graphs[] = $graph_row;
    }
}

$smarty->assign('graphs', $graphs);
$smarty->display('graph_template.tpl');
?>

<强>#2
您可以创建对象来帮助您管理代码。例如,要生成与以前相同的XML输出,您可以执行以下操作:

<?php
class Graph
{
    protected $caption;
    protected $show_names;
    protected $decimal_precision;
    protected $bg_color;

    protected $sets;

    public function __construct($graph_id)
    {
        $graph_result = mysql_query("SELECT caption, show_names, decimal_precision_bg_color
                                 FROM graph WHERE something='something else'");
        while($graph_row = mysql_fetch_assoc($graph_result))
        {
            list($this->caption, $this->show_names, $this->decimal_precision, $this->bg_color) = $graph_result;
            $set_result = mysql_query("SELECT name, value FROM set WHERE graph_id = {$graph_row['id']}");
            while($set_row = mysql_fetch_assoc($set_result))
            {
                $this->sets[] = $set_row;
            }
        }
    }

    public function fetch_xml()
    {
        $output  = '<?' . 'xml version="1.0" encoding="UTF-8" ?' . '>';
        $output .= "<graph caption=\"{$this->caption}\" showNames=\"{$this->show_names}\" decimalPrecision=\"{$this->decimal_precision}\" bgcolor=\"{$this->bg_color}\">\n";
        foreach($this->sets as $set)
        {
            $output .= "<set name=\"{$set->name}\" value=\"{$set->value}\"/>\n";
        }
        $output .= "</graph>";
        return $output;
    }
}
?>

并在主代码中调用它,如:

<?php
$address = $_SERVER['PHP_SELF'];
if ($address == 'webService.php/fcf/last5pricechanges/')
{
    $graph = new Graph(1);
    echo $graph->fetch_xml();
}
?>

<强>#3
您可以尝试使用类似SimpleXML的内容,但我怀疑这对可维护性有多大帮助,因为它与echo方法一样冗长

#4
......不,我全力以赴:-) 希望有所帮助。