以下是我在网上找的例子,请问是不是这样做
因为有两个php文件,我该怎样测试呢?
打开sendXML.php没点反应,也没有生成xml.txt
打开第二个直接fail了//sendXML.php   <!--发送XML的页面-->
<?
$xml_data = '<xml>...</xml>';//发送的xml
$url = 'http://localhost/getXML.php';//接收XML地址$header = "Content-type: text/xml";//定义content-type为xml
$ch = curl_init(); //初始化curl
curl_setopt($ch, CURLOPT_URL, $url);//设置链接
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据
$response = curl_exec($ch);//接收返回信息
if(curl_errno($ch)){//出错则显示错误信息
    print curl_error($ch);
}
curl_close($ch); //关闭curl链接
echo $response;//显示返回信息
?>
//getXML.php   <!--接收XML的页面-->
<?
$xml = file_get_contents('php://input');//监听是否有数据传入
if($xml!=''){//如果有数据传入,则将传入的数据写入到xml.txt文件
 $fp = fopen('xml.txt','w');
 fwrite($fp,$xml);
 fclose($fp);
 exit('OK');//写入成功后返回成功信息
}
die('Fail');//没有数据则直接返回失败信息
?>

解决方案 »

  1.   

    <?php
    $xml_data = '
    <xml>
    <breakfast_menu>
    <food>
    <name>Belgian Waffles</name>
    <price>$5.95</price>
    <description>
    two of our famous Belgian Waffles with plenty of real maple syrup
    </description>
    <calories>650</calories>
    </food>
    <food>
    <name>Strawberry Belgian Waffles</name>
    <price>$7.95</price>
    <description>
    light Belgian waffles covered with strawberries and whipped cream
    </description>
    <calories>900</calories>
    </food>
    <food>
    <name>Berry-Berry Belgian Waffles</name>
    <price>$8.95</price>
    <description>
    light Belgian waffles covered with an assortment of fresh berries and whipped cream
    </description>
    <calories>900</calories>
    </food>
    <food>
    <name>French Toast</name>
    <price>$4.50</price>
    <description>
    thick slices made from our homemade sourdough bread
    </description>
    <calories>600</calories>
    </food>
    <food>
    <name>Homestyle Breakfast</name>
    <price>$6.95</price>
    <description>
    two eggs, bacon or sausage, toast, and our ever-popular hash browns
    </description>
    <calories>950</calories>
    </food>
    </breakfast_menu>
    </xml>';//发送的xml$url = 'http://localhost:8081/getXML.php';//接收XML地址$header[] = "Content-type: text/xml";//定义content-type为xml
    $ch = curl_init(); //初始化curl
    curl_setopt($ch, CURLOPT_URL, $url);//设置链接
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);//设置是否返回信息
    curl_setopt($ch, CURLOPT_HTTPHEADER, $header);//设置HTTP头
    curl_setopt($ch, CURLOPT_POST, 1);//设置为POST方式
    curl_setopt($ch, CURLOPT_POSTFIELDS, $xml_data);//POST数据
    $response = curl_exec($ch);//接收返回信息
    if(curl_errno($ch)){//出错则显示错误信息
        print curl_error($ch);
    }
    curl_close($ch); //关闭curl链接
    echo $response;//显示返回信息
    ?>
      

  2.   

    <?php
    $xml = file_get_contents('php://input');//监听是否有数据传入
    if($xml!=''){//如果有数据传入,则将传入的数据写入到xml.txt文件
     $fp = fopen('xml.txt','w');
     fwrite($fp,$xml);
     fclose($fp);
     exit('xml文件已成功保存到文件');//写入成功后返回成功信息
    }
    die('Fail');//没有数据则直接返回失败信息
    ?>