我有两台计算机,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, 在表格中看起来像这样
1 Friday garfield Branch1 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;
}
?>
请帮助
答案 0 :(得分:-1)
据我所知,从标题。我将使用框架来完成这项工作。像Apache Camel,Mule ESB。如果它将是一个大规模的实施。
如果您可以告诉我们整个故事,可以更方便地帮助您。
-Guru @gnanagurus
答案 1 :(得分:-1)
$file = 'http://localhost/thesis/xmldailyrep.php';
$xml_builder = file_get_contents($file);