13. 税费计算:XML 练习
在这个我们已经多次学习过的练习中,服务器以 XML 流的形式将结果返回给客户端。
13.1. 服务器 (impots_05B_web)
之前编写的税费计算 Web 服务采用分层架构:
![]() |
在前面的示例中,Web 服务会向其客户端发送以下两行中的任意一行:
这不是一个结构正确的 XML 文档,因为它缺少根标签。新的 Web 服务将以以下格式发送响应:
或
在这两种情况下,响应都是一个格式正确的 XML 文档,可由 [simpleXML] 模块进行处理。
在三层架构中,只需修改 [web] 层。其代码 [impots_05B_web] 变为如下所示:
<?php
// business layer
require_once "impots_05_metier.php";
// error management
ini_set("display_errors", "off");
// uTF-8 header
header("Content-Type: text/plain; charset=utf-8");
// ------------------------------------------------------------------------------
// tax web service
// definition of constants
$HOTE = "localhost";
$PORT = 3306;
$BASE = "dbimpots";
$USER = "root";
$PWD = "";
// the data required to calculate the tax has been placed in table mysqL $TABLE
// belonging to the $BASE database. The table has the following structure
// limits decimal(10,2), coeffR decimal(6,2), coeffN decimal(10,2)
// taxable person parameters (marital status, number of children, annual salary)
// are sent by the customer in the form params=marital status, number of children, annual salary
// results (marital status, number of children, annual salary, tax payable) are returned to the customer
// in the form <impot>impot</impot>
// or as <error>error</error>, if parameters are invalid
// retrieve the [business] layer in the session
session_start();
if (!isset($_SESSION['metier'])) {
// instantiation of the [dao] layer and the [business] layer
try {
$_SESSION['metier'] = new ImpotsMetier(new ImpotsDaoWithMySQL($HOTE, $PORT, $BASE, $USER, $PWD));
} catch (ImpotsException $ie) {
print "<reponse><erreur>Erreur : " . utf8_encode($ie->getMessage() . "</erreur></reponse>");
exit;
}
}
$metier = $_SESSION['metier'];
// retrieve the line sent by the client
$params = utf8_encode(htmlspecialchars(strtolower(trim($_POST['params']))));
//print "parameters received --> $params\n";
$items = explode(",", $params);
// there must be only 3 parameters
if (count($items) != 3) {
print "<reponse><erreur>[$params] : nombre de paramètres invalides</erreur></reponse>\n";
exit;
}//if
// first parameter (marital status) must be yes/no
$marié = trim($items[0]);
if ($marié != "oui" and $marié != "non") {
print "<reponse><erreur>[$params] : 1er paramètre invalide</erreur></reponse>\n";
exit;
}//if
// the second parameter (number of children) must be an integer
if (!preg_match("/^\s*(\d+)\s*$/", $items[1], $champs)) {
print "<reponse><erreur>[$params] : 2ième paramètre invalide</erreur></reponse>\n";
exit;
}//if
$enfants = $champs[1];
// the third parameter (salary) must be an integer
if (!preg_match("/^\s*(\d+)\s*$/", $items[2], $champs)) {
print "<reponse><erreur>[$params] : 3ième paramètre invalide</erreur></reponse>\n";
exit;
}//if
$salaire = $champs[1];
// tax calculation
$impôt = $metier->calculerImpot($marié, $enfants, $salaire);
// return the result
print "<reponse><impot>$impôt</impot></reponse>\n";
// end
exit;
Web 服务响应的格式在第 35、47、53、58、64 和 71 行进行了修改。
13.2. 客户端(client_impots_05b_web)
客户端已进行修改以适应新的响应格式。我们使用 [simpleXML] 来处理它:
<?php
// tax client
// error management
ini_set("display_errors", "off");
// ---------------------------------------------------------------------------------
// a class of utility functions
class Utilitaires {
...
}
// main -----------------------------------------------------
// definition of constants
$DATA = "data.txt";
$RESULTATS = "resultats.txt";
// server data
$HOTE = "localhost";
$PORT = 80;
$urlServeur = "/exemples-web/impots_05B_web.php";
...
exit;
function calculerImpot($HOTE, $PORT, $urlServeur, &$cookie, $params) {
// connects client to ($HOTE,$PORT,$urlServeur)
// sends the $cookie cookie if it is non-empty. $cookie is passed by reference
// sends $params to the server
// exploits the result line
...
// read line result
$ligne = fgets($connexion, 1000);
// close the connection
fclose($connexion);
// result calculation
$xml = new SimpleXMLElement($ligne);
$erreur = isset($xml->erreur) ? $xml->erreur : "";
$impôt = isset($xml->impot) ? $xml->impot : "";
// return
return array($erreur, $impôt);
}
- 第 32 行:读取服务器的响应。这是一个 XML 文档。
- 第 36 行:根据接收到的 XML 文档构建一个 SimpleXML 对象。
- 第 37 行:任何错误消息
- 第 38 行:税额(如有)
