Vijhon
Vijhon

Reputation: 13

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.

First of all, I have a php file that is sending an xml file via Post, inside an array:

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

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. Here's what i get:

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

It seems that symbols "< >" are not well written--> Nom%3ERetard%3C%2FNom%3E%0A++++++%3C but i dont know how to fix it

I'm new on php and im sure that there's something i've done bad...

Thanks in advance

Upvotes: 1

Views: 286

Answers (1)

dkasipovic
dkasipovic

Reputation: 6120

Your XML should be stored in $_POST["xml"] variable. So you can try:

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

Upvotes: 1

Related Questions