2014-04-01 36 views
1

即時通訊嘗試通過我的本地機器使用PHP來模擬web服務發佈請求,並且即時通訊出現一些問題。錯誤從PHP中發佈請求中的XML數據

首先,我正在發送經由後的XML文件的PHP文件,陣列內:

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

?> 

然後在另一側我有加載XML內容並寫入另一個PHP文件它在一個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); 

?> 

我從這得到的是一個糟糕的形成xml至極我試圖轉換編碼,但似乎沒有任何操作。 這是我得到的:

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

看來符號 「<>」 寫得不好 - >喃%3ERetard%3C%2FNom%3E%0A ++++++%3C 但我不知道如何解決它

我在PHP新和IM確保有件事情我已經做了壞...

在此先感謝

回答

1

您的XML應存儲在$_POST["xml"]變量中。所以你可以試試:

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