2014-11-04 132 views
2

我查看了類似的問題,但沒有解決我遇到的問題。使用php從HTTP POST請求檢索XML

我正在構建Web服務,我想從HTTP POST請求中檢索XML數據,操作數據並返回響應。而寫劇本要考慮下面的信息應該:

The communication mode is HTTP POST (not SOAP) 
The content type is text/xml. 
The POST request will contain only XML 
The request will be a RAW POST directly to stream and NOT in a parameter. 

我試過,但我的腳本不捕獲從HTTP POST請求的數據。

我的腳本:

$postData = file_get_contents('php://input'); 

    if(!empty($xml->MerchantReference)){ 
    $merchRef = (string)$xml->MerchantReference; 
    $custRef = (int)$xml->CustReference; 
    $username = (string)$xml->ServiceUsername; 
    $password = (string)$xml->ServicePassword; 
    $db->setQuery(Check if customer exists in database); 
    if($db->countResultset() == 0) 
    { 
     header("Content-type: text/xml"); 
     echo "<?xml version='1.0' encoding='UTF-8'?>"; 
     echo "<CustomerInformationResponse>"; 
      echo "<MerchantReference>".$merchRef."</MerchantReference>"; 
      echo "<Customers>"; 
       echo "<Customer>"; 
        echo "<Status>0</Status>"; 
        echo "<CustReference>".$custRef."</CustReference>"; 
        echo "<FirstName/>"; 
        echo "<LastName/>"; 
        echo "<OtherName/>"; 
        echo "<Email/>"; 
        echo "<Phone/>"; 
        echo "<ThirdPartyCode/>"; 
        echo "<StatusMessage>Customer is valid</StatusMessage>"; 
       echo "</Customer>"; 
      echo "</Customers>"; 
     echo "</CustomerInformationResponse>"; 
     exit; 
    } 

這是HTTP POST請求:

<CustomerInformationRequest xmlns:ns2="http://test.url.com/" xmlns:ns3="http://www.w3.org/2003/05/soap-envelope"> 
    <ServiceUrl>MY URL</ServiceUrl> 
    <ServiceUsername>12345</ServiceUsername> 
    <ServicePassword>abcdef</ServicePassword> 
    <RouteId>HTTPGENERICv31</RouteId> 
    <Service>bill</Service> 
    <MerchantReference>123456</MerchantReference> 
    <CustReference>abcdef</CustReference> 
    <PaymentItemCategoryCode/> 
    <RequestReference/> 
    <TerminalId/> 
    <Amount>0</Amount> 
    <FtpUsername/> 
    <FtpPassword/> 
</CustomerInformationRequest> 

檢索和操縱數據,我的腳本了返回響應後。 這是響應應該如何:

 <CustomerInformationResponse> 
     <MerchantReference>3527</MerchantReference > 
      <Customers> 
       <Customer> 
       <Status>0</Status>      
       <CustReference>4565</CustReference> 
       <FirstName></FirstName>           
       <LastName></LastName> 
       <OtherName></OtherName> 
       <Email></Email> 
       <Phone></Phone> 
       <ThirdPartyCode/>              
       <StatusMessage>Customer is Valid</StatusMessage> 
       </Customer> 
      </Customers> 
      </CustomerInformationResponse> 
+0

'$ xml'將是一個字符串。你爲什麼試圖調用它的方法? – Quentin 2014-11-05 09:27:24

+0

@Quentin我不是在$ xml上調用方法;我試圖從xml數據中獲取一些元素 – fidazik 2014-11-05 09:32:23

+0

我不太喜歡PHP-OO,但據我所知'$ xml-> MerchantReference'時不允許使用'$ xml'字符串。 – Quentin 2014-11-05 09:39:28

回答

1

經過大量的研究,我已經找到了答案,我的問題。我發現從HTTP Post請求獲取數據後,我沒有將其作爲xml數據存儲。

所以加入這一行的代碼

$xml = simplexml_load_string($postData); 

$postData = file_get_contents('php://input'); 

後解決了這個問題。我認爲我應該分享它,因爲它可能對某人有用。

2
 
$merchRef = (string)$xml->MerchantReference; 
//This did not work for me. However check how I formatted the code to get the xml nodes correctly
<?php 
$postData = file_get_contents('php://input'); 
$xml=simplexml_load_string($postData); 
//////////////// 
$merchRef=$xml->Customers[0]->Customer[0]->MerchantReference; 
$custRef=$xml->Customers[0]->Customer[0]->CustReference; 
?> 
+1

請添加一些解釋。 – gofr1 2016-05-07 10:36:21