2014-11-04 34 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