简体   繁体   English

从PHP中的发布请求写入xml数据时出错

[英]error writing xml data from post request in PHP

im trying to simulate a webservice post request via my local machine using PHP, and im getting some problems. 我试图通过使用PHP的本地计算机模拟Web服务发布请求,并且遇到一些问题。

First of all, I have a php file that is sending an xml file via Post, inside an array: 首先,我有一个php文件,该文件通过数组内部的Post发送xml文件:

<?php

$xml = file_get_contents('localstorage.xml');
$url = 'http://127.0.0.1/projects/My_webservice/rebreFitxer1.php';

$post_data = array('xml' => $xml, );

$stream_options = array(
'http' => array(
    'method'  => 'POST',
    'header'  => 'Content-type: application/x-www-form-urlencoded' . "\r\n",
    'content' =>  http_build_query($post_data)));

$context = stream_context_create($stream_options);
$response = file_get_contents($url, null, $context);

?>

Then in the other side i have another php file that loads the xml content and writes it on a xml file. 然后在另一侧,我有另一个php文件,该文件加载xml内容并将其写入xml文件中。

<?php

header('Content-type: text/xml');

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
$postText = file_get_contents('php://input');    // load the content loaded via POST

$postText = utf8_encode($postText);

$datetime = date('ymdHis');
$xmlfile = "myfile" . $datetime . ".xml";   //new file name

$FileHandle = fopen($xmlfile, 'w') or die("can't open file");
// open the new file in writing mode
fwrite($FileHandle, $postText);
fclose($FileHandle);

?>

What i get from this is a bad formed xml wich i tried to convert encoding but nothing seems to operate. 我从中得到的是格式错误的xml,但我尝试转换编码,但似乎没有任何操作。 Here's what i get: 这是我得到的:

xml=%3C%3Fxml+version%3D%221.0%22+encoding%3D%22utf-8%22%3F%3E%0A%3CXml...........etc ---^ xml =%3C%3Fxml + version%3D%221.0%22 + encoding%3D%22utf-8%22%3F%3E%0A%3CXml ..............等--- ^

It seems that symbols "< >" are not well written--> Nom%3ERetard%3C%2FNom%3E%0A++++++%3C but i dont know how to fix it 似乎符号“ <>”的书写方式不正确-> Nom%3ERetard%3C%2FNom%3E%0A ++++++%3C,但我不知道如何解决

I'm new on php and im sure that there's something i've done bad... 我是php的新手,我确定我做得不好...

Thanks in advance 提前致谢

Your XML should be stored in $_POST["xml"] variable. 您的XML应该存储在$_POST["xml"]变量中。 So you can try: 因此,您可以尝试:

<?php $postText = $_POST["xml"]; ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM