如何向另一台计算机发送和接收xml


How to send and receive a xml to another computer

我有两台电脑,comp 1作为分支1,comp 2作为主分支。在comp 1中,我使用php生成了数据库查询的XML。

`<?php
header ("Content-Type:text/xml");
//database configuration
$config['mysql_host'] = "localhost";
$config['mysql_user'] = "root";
$config['mysql_pass'] = "donnaluz";
$config['db_name']    = "global89_branch1";
$config['table_name'] = "branchsales";
//connect to host
mysql_connect($config['mysql_host'],$config['mysql_user'],$config['mysql_pass']);
//select database
@mysql_select_db($config['db_name']) or die( "Unable to select database");
$xml          = "<?xml version='"1.0'" encoding='"UTF-8'"?>";
$root_element = $config['table_name'];
$xml         = "<$root_element>";
/*$title = $doc->createElement("branchsales");
$title = $root->appendChild($title);
$text = $doc->createTextNode("sales");
$text = $title->appendChild($text);
*/
//select all items in table
$sql = "SELECT        branch.branchname,branchadmin.username,branchcomp.*,branchsales.*,days.day
            FROM branchsales,branch,branchadmin,branchcomp,days
                WHERE
                    branchsales.comp_id = branchcomp.comp_id
                    AND branchsales.admin_id = branchadmin.admin_id
                    AND branchsales.branch_id = branch.branch_id
                    AND branchsales.day_id = days.day_id";
$result = mysql_query($sql);
if (!$result) {
die('Invalid query: ' . mysql_error());
}
if(mysql_num_rows($result)>0)
{
while($result_array = mysql_fetch_assoc($result))
{
     $xml .= "<".$config['table_name'].">";
  //loop through each key,value pair in row
  foreach($result_array as $key => $value)
  {
     //$key holds the table column name
     $xml .= "<$key>";
     //embed the SQL data in a CDATA element to avoid XML entity issues
     $xml .= "<![CDATA[$value]]>"; 
     //and close the element
     $xml .= "</$key>";
  }
  $xml.="</".$config['table_name'].">";
  }
 //close the root element
$xml .= "</$root_element>";
//send the xml header to the browser
header ("Content-Type:text/xml"); 
echo $xml;
?>

'看起来像这样

<branchsales>
<branchname>Branch1</branchname>
<username>garfield</username>
<comp_id>1</comp_id>
<admin_id>1</admin_id>
<pcnum>1</pcnum>
<starttime>09:00:00</starttime>
<endtime>10:00:00</endtime>
<totaltime>1:00:00</totaltime>
<compcharge>10.00</compcharge>
<id>1</id>
<branch_id>1</branch_id>
<day_id>5</day_id>
<timeopened>8:00:00</timeopened>
<timeclosed>23:00:00<timeclosed>

等等. .等等. .

问题是,我希望生成的xml发送到comp 2,在表

中看起来像这样

|ID|日期|观察员|分支|当前日期|时间开启|时间关闭| PC号| so on…

1星期五加菲猫树枝1 29-03-13 8:00:00 23:00:00 1

这是我的发送代码,但是它不工作

<?
php
$file = 'http://localhost/thesis/xmldailyrep.php';
$xml_builder = 'simplexml_load_file($file)';

$ch = curl_init("http://172.16.0.55/dailyrep1.php");
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type:text/xml'));
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_builder);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
curl_setopt($ch, CURLOPT_REFERER, "http://localhost/thesis/xmldailyrep.php");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$ch_result = curl_exec($ch);
curl_close($ch);
echo $ch_result;

?> 

我在COMP 2中的接收码是这个

<?php
/*
* XML Server.
*/
// We use php://input to get the raw $_POST results.
$xml_post = file_get_contents('xmldailyrep.php');
// If we receive data, save it.
if ($xml_post) {
$xml_file = 'received_xml_' . date('Y_m_d-H-i-s') . '.xml';
$fh       = fopen($xml_file, 'w') or die();
fwrite($fh, $xml_post);
fclose($fh);
// Return, as we don't want to cause a loop by processing the code below.
return;
}
?>

请帮

据我所知,从标题。我将使用框架来完成这项工作。像Apache Camel, Mule ESB。如果要大规模实施的话。

如果你能把事情的原原本本告诉我们,我们会更容易帮助你。

大师@gnanagurus

$file = 'http://localhost/thesis/xmldailyrep.php';
$xml_builder = file_get_contents($file);