我的表格看起来像这样。现在,客户端已要求将其转换为以XML格式查询和发送响应的格式。有人可以在PHP中使用适当的教程或示例。似乎有很多方法可以做到这一点
<form action="" method='post'>
<table>
<tr><td>User Number </td><td><input type='text' name='task_user_no' value='<?=$task_user_no?>'></td></tr>
<tr><td>Date </td><td><input type='text' name='task_date' value='<?=$task_date?>'> (YYYYMMDD)</td></tr>
<tr><td>From Time </td><td><input type='text' name='task_from_time' value='<?=$task_from_time?>'>(HHMM)</td></tr>
<tr><td>To Time </td><td><input type='text' name='task_to_time' value='<?=$task_to_time?>'>(HHMM)</td></tr>
<tr><td>Message </td><td><input type='text' name='task_message' value='<?=$task_message?>'></td></tr>
<tr><td> </td><td><input type='submit' value='submit' name='submit' ></td></tr>
</form>
答案 0 :(得分:2)
好吧,因为你没有提供详细信息,奠定了基础:
所以给这些步骤一些示例脚本:
RS服务器脚本
// set up params for query:
$params = array(
'task_no' => '0000000'
'task_date' => 'YYYYMMDD',
'task_from_time' => 'HHMM',
'task_to_time' => 'HHMM',
'taks_message' => 'The Message'
);
$client = curl_init('http://remote-server.com/task.php');
// return the response instead of outputting it
curl_setopt($client, CURLOPT_RETURNTRANSFER, true);
// make it a POST request, use CURLOPT_GET for Get requests
curl_setopt($client, CURLOPT_POST, true);
// set the data to send.. if using get then intstead use http_build_query($params) and append the resuult to the URL used in curl_init
curl_setopt($client, CURLOPT_POSTFIELDS, $params);
$response = curl_exec($client);
// load the response as xml
try
{
$responseXml = new SimpleXmlElement($response);
// do stuff here with the result see SimpleXml documentation for working with the xml nodes
exit;
}
catch(Exception $e)
{
// log message from exception
// exit with a non-zero status code may be important for cron or a shell invocation
exit($e->getCode());
}
AS上的task.php脚本
// Im going to use PDO for simplicity sake
$db = new PDO($dsn, $user, $pass);
$query = 'SELECT * from table_name'
.'WHERE task_user_no = :task_user_no'
.'AND task_date = :task_date'
.'AND task_from_time = :task_from_time'
.'AND task_to_time = :task_to_time';
$stmt = $db->prepare($query);
$params = $_POST; // make a copy for binding
$xml = new DOMDocument('1.0', 'UTF-8');
// create some basic elements
$response = $xml->createElement('response');
$info = $xml->createElement('info');
$results = $xml->createElement('results');
// set up an array we can append later if there are errors
$errors = array();
foreach($params as $field => $value)
{
$paramName = ':' . $field;
switch($field)
{
case 'task_user_no':
$paramType = PDO::PARAM_INT; // assuming an int pk/fk
break;
default:
$paramType = PDO::PARAM_STR; // assuming string for all others
break;
}
if(!$stmt->bindParam($paramName, $param[$field], $paramType))
{
$errors[] = $xml->createElement('error', sprintf(
'Value for (%s) does not exist or is not of the proper type (%s).'
$field,
$paramType
));
}
}
if(!$stmt->execute() && ($pdoError = $stmt->errorCode()))
{
$errors[] = sprintf(
'There was an error retrieving your data, Error (%s)',
$pdoError
);
}
while(false !== ($record = $stmt->fetch(PDO::FETCH_ASSOC)))
{
$task = $xml->createElement('task');
foreach($record as $col => $val)
{
$task->appendChild($xml->createElement($col, $val));
$results->appendChild($task);
}
}
if(!empty($errors))
{
$errorsElement = $xml->createElement('errors');
foreach($errors as $error)
{
$errorsElement->appendChild($xml->createElement('error', $error));
}
$info->appendChild($errorsElement);
}
$response->appendChild($info);
$response->appendChild($results);
$xml->appendChild($response);
$responseStr = $xml->saveXml();
header('Content-type: text/xml');
header('Content-length: '. strlen($responseStr));
header('HTTP/1.1 200 Ok');
print $responseStr;
exit;
当然你可以使用现有的库来进一步简化......比如使用curl
而不是Zend_Http_Client
你可以使用curl
(我肯定会推荐它,因为它不仅允许你使用{ {1}}还有fopen
和直接套接字)。或者,对于AS上的xml响应解析,您可以使用Zend_Dom_Query
,这基本上允许您以类似于jQuery(css选择器和不是xPath)的方式处理xml响应。
答案 1 :(得分:0)
看一下Jquery Ajax函数