2012-11-09 29 views
-1

我需要一些幫助來找出爲什麼我不能在simplexml_load_string中使用$ _POST ['status']。

我將一些數據發佈到一個php頁面,然後閱讀$ _POST ['status']。位於$ _POST ['status']內的xml需要讀入php的simplexml_load_string中,以便我可以將xml解析爲一個對象。

如果我硬編碼simplexml_load_string()這樣的:

$xml = simplexml_load_string('<?xml version="1.0"?> 
           <BackgroundReports userId="" password="" databaseset=""> 
            <BackgroundReportPackage> 
            <ReferenceId>1|9</ReferenceId> 
            <OrderId>107284</OrderId> 
            <ScreeningStatus> 
             <OrderStatus flag="FALSE">x:partial</OrderStatus> 
            </ScreeningStatus> 
            <Screenings> 
             <Screening type="credit"> 
             <ScreeningResults type="result" mediaType="html" resultType="report"> 
              <InternetWebAddress><![CDATA[https://somewhere.com]]></InternetWebAddress> 
             </ScreeningResults> 
             </Screening> 
            </Screenings> 
            </BackgroundReportPackage> 
           </BackgroundReports>'); 

而且隨着後續代碼var_dump($ XML)轉儲它的輸出是:

XML 

object(SimpleXMLElement)#3 (2) { 
    ["@attributes"]=> 
    array(3) { 
    ["userId"]=> 
    string(0) "" 
    ["password"]=> 
    string(0) "" 
    ["databaseset"]=> 
    string(0) "" 
    } 
    ["BackgroundReportPackage"]=> 
    object(SimpleXMLElement)#4 (4) { 
    ["ReferenceId"]=> 
    string(3) "1|9" 
    ["OrderId"]=> 
    string(6) "107284" 
    ["ScreeningStatus"]=> 
    object(SimpleXMLElement)#5 (1) { 
     ["OrderStatus"]=> 
     string(9) "x:partial" 
    } 
    ["Screenings"]=> 
    object(SimpleXMLElement)#6 (1) { 
     ["Screening"]=> 
     object(SimpleXMLElement)#7 (2) { 
     ["@attributes"]=> 
     array(1) { 
      ["type"]=> 
      string(6) "credit" 
     } 
     ["ScreeningResults"]=> 
     object(SimpleXMLElement)#8 (2) { 
      ["@attributes"]=> 
      array(3) { 
      ["type"]=> 
      string(6) "result" 
      ["mediaType"]=> 
      string(4) "html" 
      ["resultType"]=> 
      string(6) "report" 
      } 
      ["InternetWebAddress"]=> 
      object(SimpleXMLElement)#9 (0) { 
      } 
     } 
     } 
    } 
    } 
} 

但是如果我使用$ _ POST [ '狀態']像這個$xml = simplexml_load_string($_POST['status']);而不是硬編碼,然後simplexml_load_string不起作用。我知道$ _POST ['status']具有所有相同的xml ...我將它拋出,它與我將其作爲參數硬編碼爲simple_xml_string時完全相同。

輸出現在變成這樣,當我嘗試使用$ _ POST [「狀態」]:

XML 

bool(false) 

我需要弄清楚如何能夠與$ _ POST使用[「狀態」]作爲該會員將只會發回我的PHP頁面。

+0

簡單回答:'$ _POST ['status']'是不一樣的。找出原因,這是更難的部分,不能從這裏做到這一點,嘗試'var_dump'而不是回聲它。 – Wrikken

+0

我var_dumped ...我比較了var_dump的輸出和剪切'n直接粘​​貼到代碼中,所以它是硬編碼...它的工作原理......但問題是我不能硬編碼它。 ..我需要從帖子變量...這仍然無法正常工作。 – Ronedog

+0

您是從網頁的_source_還是僅從瀏覽器中的輸出複製/粘貼? – Wrikken

回答

0

也許你在你的主機上啓用了魔術引號。你可以做一個簡單的測試表單和POST來查看。

<?php 
if($_SERVER['REQUEST_METHOD']=='POST'){ 
    if(get_magic_quotes_gpc()) { 
     echo 'Magic quotes enabled, so GET,POST,REQUEST,COOKIE Strings are escaped'; 
     $status = stripslashes($_POST['status']); 
    }else{ 
     $status = $_POST['status']; 
    } 

    print_r(simplexml_load_string($status)); 
}else{ 
?> 
<h1>Test XML POST</h1> 
<form method="POST" action=""> 
    <p><textarea rows="9" name="status" cols="36"></textarea></p> 
    <p><input type="submit" value="Submit"></p> 
</form> 
<?php } ?> 

還加入error_reporting(E_ALL);不會傷害。

0

感謝所有幫助我的人。這是我發現的。當我在「發佈」有XML中的一個額外的行這樣

" 

<?xml version="1.0"?>bla bla 
" 

當我刪除空行這樣它的所有工作。

"<?xml version="1.0"?>bla bla 
"