En iyi uygulamalar PHP, XML web servisleri verilerini oluştururken

1 Cevap php

Şu anda veri ekranı ve grafik görünümü için hem de belirli bir formatta XML gerektirir projemde FusionCharts Bedava kullanıyorum.

Ben aşağıda birçok benzer tekrarlar vardır webservice.php adında bir PHP dosyası vardır:

$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
}

Outputted XML şöyle görünebilir:

<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 belirli bir XML özellikleri gerektirir, gibi GERİSİ gibi - Bu standart bir format kullanarak veri aktarmak mümkün olmaz afaik bu kodu güncellemek için bir bakım kabus biraz oluyor, henüz ediyor. Yukarıdaki işlemek için daha iyi bir yolu var mı? Ben kolayca benim kod modülerlik ve dayanıklılığını artırabilir?

1 Cevap

Ben düşünebiliriz fikirlerin bir çift var.

#1
Using a template engine, like Smarty, would probably make this a little easier to maintain. It would at least get rid of the XML from your PHP code.

Örneğin, deftere XML parçacığı için bir şablon oluşturabilirsiniz:

<?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}

Ve PHP onu aramak

<?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
You could create objects to help you manage the code. For example, to generate the same XML output as before, you could do:

<?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;
    }
}
?>

Ve gibi ana kod çağrı:

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

#3
You could try using something like SimpleXML, but I doubt that would help much with the maintainability, as it would be just as verbose as the echo method

And #4
... nope, I'm all out :-) Hope that helps.