我检查了类似的问题,但没有一个能解决我面临的问题。
我正在构建一个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 :(得分:6)
经过大量研究,我找到了问题的答案。我发现在从HTTP Post请求获取数据后,我没有将其存储为xml数据。
所以添加这行代码
$xml = simplexml_load_string($postData);
之后
$postData = file_get_contents('php://input');
解决了它。我想我应该分享它,因为它可能对某人有用。
答案 1 :(得分:3)
$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;
?>