嘛,因为你还没有提供细节生病放下基础:
- cron作业,用户交互或其他触发器调用远程服务器(RS)上的请求。
- php scrip t建立一个查询发送到托管您的站点/应用程序的应用程序服务器(AS)
- AS解析请求变量并为数据存储构建查询。
- 的AS使得对数据存储查询,并将结果转换为XML格式
- 的AS发送响应为XML的RS
- 的RS解析XML和做什么它需要与做数据包含
所以给了这些步骤,一些示例脚本:
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;
当然你也可以使用现有的库,以进一步simplicty东西...例如,而不是使用curl
你可以使用Zend_Http_Client
(我肯定会,因为它推荐不仅可以使用curl
,而且还可以使用fopen
和直接插座)。或者你可以使用Zend_Dom_Query
这个基本上允许你以类似于jQuery(css选择器和什么不是xPath)的方式处理xml响应。
如果您试图摆脱窗体并希望远程服务器查询不同的服务器,那么您确实需要提供有关提交窗体时现在在您的服务器上实际发生的更多详细信息。如果你设置了表单提交给url来处理标准请求,或者你需要发送xml请求,并且如果是的话,那么xml需要的样子,我们不能告诉你。 – prodigitalson 2010-09-26 23:16:29