加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

使用php从HTTP POST请求中检索XML

发布时间:2020-12-13 21:52:24 所属栏目:PHP教程 来源:网络整理
导读:我检查了类似的问题但没有解决我面临的问题. 我正在构建一个Web服务,我想从HTTP POST请求中检索XML数据,操纵数据并返回响应.编写脚本时应考虑以下信息: The communication mode is HTTP POST (not SOAP) The content type is text/xml. The POST request wi
我检查了类似的问题但没有解决我面临的问题.

我正在构建一个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>

解决方法

经过大量研究,我找到了问题的答案.我发现在从HTTP Post请求获取数据后,我没有将其存储为xml数据.

所以添加这行代码

$xml = simplexml_load_string($postData);

在这之后

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

解决了它.我想我应该分享它,因为它可能对某人有用.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读