【问题标题】:How to send and receive a xml to another computer如何向另一台计算机发送和接收 xml
【发布时间】:2013-03-22 11:46:50
【问题描述】:

我有两台计算机,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|日|观察者|分支|当前日期 |营业时间 |关闭时间 |电脑编号 |以此类推...

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

?>

请帮忙

【问题讨论】:

    标签: php xml database xmlhttprequest


    【解决方案1】:

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

    如果你能把整个故事告诉我们,那么帮助你会更容易。

    -大师 @gnanagurus

    【讨论】:

      【解决方案2】:
      $file = 'http://localhost/thesis/xmldailyrep.php';
      $xml_builder = file_get_contents($file);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-06-17
        • 2013-02-10
        相关资源
        最近更新 更多