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

时间:2023-01-14 10:41:26

I have two computers, comp 1 as branch 1 and comp 2 as main branch. in comp 1 i have generated an xml of my database query using php.

我有两台计算机,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;

?>

` which looks like this

“是这样的。

<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>

blah blah.. so on..

等等等等……等等. .

The thing is, I want that generated xml to be sent out to comp 2, looking like this in a table

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


|ID|Day |Watcher |Branch | Current Date | Time Opened | Time closed | PC No. | so on...

1 Friday garfield Branch1 29-03-13 8:00:00 23:00:00 1

1 .周五garfield Branch1 29-03-13 8:00:00 23:00:00 1。

THIS IS MY SEND CODE, BUT ITS NOT WORKING

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

<?
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;


?> 

MY RECEIVE CODE IN COMP 2 is this

我在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;
}

?>

PLEASE HELP

请帮助

2 个解决方案

#1


-1  

As far as I know, from the title. I will use frameworks to do this work. Like Apache Camel, Mule ESB. If its going to be a large scale implementation.

据我所知,从标题。我将使用框架来完成这项工作。与Apache Camel类似,Mule ESB。如果是大规模的实现。

If you can tell us the whole story, it could be easier to help you.

如果你能把整个故事讲给我们听,那就更容易帮助你了。

-Guru @gnanagurus

大师@gnanagurus

#2


-1  

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

#1


-1  

As far as I know, from the title. I will use frameworks to do this work. Like Apache Camel, Mule ESB. If its going to be a large scale implementation.

据我所知,从标题。我将使用框架来完成这项工作。与Apache Camel类似,Mule ESB。如果是大规模的实现。

If you can tell us the whole story, it could be easier to help you.

如果你能把整个故事讲给我们听,那就更容易帮助你了。

-Guru @gnanagurus

大师@gnanagurus

#2


-1  

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